Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: invalid use of flexible array member in sound/soc/sof/topology.c #180

Closed
GustavoARSilva opened this issue Feb 15, 2022 · 4 comments
Closed
Assignees
Labels
[PATCH] Accepted A submitted patch has been accepted upstream [PATCH] Submitted A patch has been submitted upstream [Refactor] 0-element array Conversion away from zero-length array

Comments

@GustavoARSilva
Copy link
Collaborator

GustavoARSilva commented Feb 15, 2022

The following patch causes an 'invalid use of flexible array member' error:

diff --git a/include/sound/sof/topology.h b/include/sound/sof/topology.h
index d12736e14b69..adee6afd1490 100644
--- a/include/sound/sof/topology.h
+++ b/include/sound/sof/topology.h
@@ -237,7 +237,7 @@ struct sof_ipc_comp_process {
        /* reserved for future use */
        uint32_t reserved[7];
 
-       uint8_t data[0];
+       uint8_t data[];
 } __packed;
 
 /* frees components, buffers and pipelines

error: invalid use of flexible array member:

sound/soc/sof/topology.c: In function ‘sof_process_load’:
sound/soc/sof/topology.c:2167:47: error: invalid use of flexible array member
 2167 |                         memcpy(&process->data + offset,
      |                                               ^
./include/linux/fortify-string.h:360:44: note: in definition of macro ‘__fortify_memcpy_chk’
  360 |         fortify_memcpy_chk(__fortify_size, p_size, q_size,              \
      |                                            ^~~~~~
sound/soc/sof/topology.c:2167:25: note: in expansion of macro ‘memcpy’
 2167 |                         memcpy(&process->data + offset,
      |                         ^~~~~~
sound/soc/sof/topology.c:2167:47: error: invalid use of flexible array member
 2167 |                         memcpy(&process->data + offset,
      |                                               ^
./include/linux/fortify-string.h:361:28: note: in definition of macro ‘__fortify_memcpy_chk’
  361 |                            p_size_field, q_size_field, #op);            \
      |                            ^~~~~~~~~~~~
sound/soc/sof/topology.c:2167:25: note: in expansion of macro ‘memcpy’
 2167 |                         memcpy(&process->data + offset,
      |                         ^~~~~~
sound/soc/sof/topology.c:2167:47: error: invalid use of flexible array member
 2167 |                         memcpy(&process->data + offset,
      |                                               ^
./include/linux/fortify-string.h:362:27: note: in definition of macro ‘__fortify_memcpy_chk’
  362 |         __underlying_##op(p, q, __fortify_size);                        \
      |                           ^
sound/soc/sof/topology.c:2167:25: note: in expansion of macro ‘memcpy’
 2167 |                         memcpy(&process->data + offset,
      |                         ^~~~~~
@GustavoARSilva GustavoARSilva self-assigned this Feb 15, 2022
@GustavoARSilva GustavoARSilva added the [Refactor] 0-element array Conversion away from zero-length array label Feb 15, 2022
@GustavoARSilva GustavoARSilva changed the title Fix error: invalid use of flexible array member in sound/soc/sof/topology.c error: invalid use of flexible array member in sound/soc/sof/topology.c Feb 15, 2022
@skitt
Copy link

skitt commented Feb 15, 2022

Yes, data can no longer be used as a pointer in that way:

diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
index e72dcae5e7ee..1d119d1dd69d 100644
--- a/sound/soc/sof/topology.c
+++ b/sound/soc/sof/topology.c
@@ -2164,7 +2164,7 @@ static int sof_process_load(struct snd_soc_component *scomp, int index,
         */
        if (ipc_data_size) {
                for (i = 0; i < widget->num_kcontrols; i++) {
-                       memcpy(&process->data + offset,
+                       memcpy(&process->data[offset],
                               wdata[i].pdata->data,
                               wdata[i].pdata->size);
                        offset += wdata[i].pdata->size;

@GustavoARSilva
Copy link
Collaborator Author

Yes, data can no longer be used as a pointer in that way:

diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
index e72dcae5e7ee..1d119d1dd69d 100644
--- a/sound/soc/sof/topology.c
+++ b/sound/soc/sof/topology.c
@@ -2164,7 +2164,7 @@ static int sof_process_load(struct snd_soc_component *scomp, int index,
         */
        if (ipc_data_size) {
                for (i = 0; i < widget->num_kcontrols; i++) {
-                       memcpy(&process->data + offset,
+                       memcpy(&process->data[offset],
                               wdata[i].pdata->data,
                               wdata[i].pdata->size);
                        offset += wdata[i].pdata->size;

Yep; please, feel free to send a proper patch for this if you wish to (and add a link to this issue in the changelog text). :)

@skitt
Copy link

skitt commented Feb 16, 2022

Thanks, I’ll take care of it in the next day or two.

fengguang pushed a commit to 0day-ci/linux that referenced this issue Feb 17, 2022
There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use "flexible array members"[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

This helps with the ongoing efforts to globally enable -Warray-bounds
and get us closer to being able to tighten the FORTIFY_SOURCE routines
on memcpy().

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Link: KSPP#78
Link: KSPP#180
Suggested-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Stephen Kitt <steve@sk2.org>
@GustavoARSilva GustavoARSilva added [PATCH] Submitted A patch has been submitted upstream [PATCH] Accepted A submitted patch has been accepted upstream labels Feb 17, 2022
@GustavoARSilva
Copy link
Collaborator Author

GustavoARSilva commented Feb 17, 2022

Mark Brown just took a fix for this issue in his -next tree: https://lore.kernel.org/lkml/164512273904.3993052.15786188783009967860.b4-ty@kernel.org/

ammarfaizi2 pushed a commit to ammarfaizi2/linux-block that referenced this issue Feb 17, 2022
There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use "flexible array members"[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

This helps with the ongoing efforts to globally enable -Warray-bounds
and get us closer to being able to tighten the FORTIFY_SOURCE routines
on memcpy().

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Link: KSPP/linux#78
Link: KSPP/linux#180
Suggested-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Stephen Kitt <steve@sk2.org>
Acked-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20220217132755.1786130-1-steve@sk2.org
Signed-off-by: Mark Brown <broonie@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[PATCH] Accepted A submitted patch has been accepted upstream [PATCH] Submitted A patch has been submitted upstream [Refactor] 0-element array Conversion away from zero-length array
Projects
None yet
Development

No branches or pull requests

2 participants