ASoC: SOF: Use struct_size() in kmemdup()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Thu, 23 May 2019 15:58:00 +0000 (10:58 -0500)
committerMark Brown <broonie@kernel.org>
Tue, 28 May 2019 14:51:46 +0000 (15:51 +0100)
Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes, in particular in the
context in which this code is being used.

So, replace code of the following form:

sizeof(*w) + sizeof(struct sof_ipc_window_elem) * w->num_windows

with:

struct_size(w, window, w->num_windows)

Notice that variable size is unnecessary, hence it is removed.

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/sof/loader.c

index 628fae5524424a6f05c8c54985f75d1866cd99f5..16b016b76fd80d45f9c3ddc6b09ec84f7564a75b 100644 (file)
@@ -19,15 +19,13 @@ static int get_ext_windows(struct snd_sof_dev *sdev,
 {
        struct sof_ipc_window *w =
                container_of(ext_hdr, struct sof_ipc_window, ext_hdr);
-       size_t size;
 
        if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
                return -EINVAL;
 
-       size = sizeof(*w) + sizeof(struct sof_ipc_window_elem) * w->num_windows;
-
        /* keep a local copy of the data */
-       sdev->info_window = kmemdup(w, size, GFP_KERNEL);
+       sdev->info_window = kmemdup(w, struct_size(w, window, w->num_windows),
+                                   GFP_KERNEL);
        if (!sdev->info_window)
                return -ENOMEM;