libelf/elf_newscn.c: fix build failure against gcc-14 (-Walloc-size)

Message ID 20231102195846.1414311-1-slyich@gmail.com
State Committed
Headers
Series libelf/elf_newscn.c: fix build failure against gcc-14 (-Walloc-size) |

Commit Message

Sergei Trofimovich Nov. 2, 2023, 7:58 p.m. UTC
  `gcc-14` adde a new -Walloc-size warning that makes sure that size of an
individual element matches size of a pointed type:

    https://gcc.gnu.org/PR71219

`elfutils` triggers is on `calloc()` call where member size is sued as
`1`.

    elf_newscn.c: In function `elf_newscn`:
    elf_newscn.c:97:12: error: allocation of insufficient size «1» for type «Elf_ScnList» with size «16» [-Werror=alloc-size]
       97 |       newp = calloc (sizeof (Elf_ScnList)
          |            ^

The change swaps arguments to pass larger value as a member size.

Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
---
 libelf/elf_newscn.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Mark Wielaard Nov. 2, 2023, 11:23 p.m. UTC | #1
Hi Sergei,

On Thu, Nov 02, 2023 at 07:58:46PM +0000, Sergei Trofimovich wrote:
> `gcc-14` adde a new -Walloc-size warning that makes sure that size of an
> individual element matches size of a pointed type:
> 
>     https://gcc.gnu.org/PR71219
> 
> `elfutils` triggers is on `calloc()` call where member size is sued as
> `1`.
> 
>     elf_newscn.c: In function `elf_newscn`:
>     elf_newscn.c:97:12: error: allocation of insufficient size «1» for type «Elf_ScnList» with size «16» [-Werror=alloc-size]
>        97 |       newp = calloc (sizeof (Elf_ScnList)
>           |            ^
> 
> The change swaps arguments to pass larger value as a member size.

Very nice. Patch applied.

Thanks,

Mark
  

Patch

diff --git a/libelf/elf_newscn.c b/libelf/elf_newscn.c
index d6bdf153..ec731f75 100644
--- a/libelf/elf_newscn.c
+++ b/libelf/elf_newscn.c
@@ -94,9 +94,9 @@  elf_newscn (Elf *elf)
 	  1
 #endif
 	  )
-      newp = calloc (sizeof (Elf_ScnList)
-		     + ((elf->state.elf.scnincr *= 2)
-			* sizeof (Elf_Scn)), 1);
+      newp = calloc (1, sizeof (Elf_ScnList)
+			+ ((elf->state.elf.scnincr *= 2)
+			   * sizeof (Elf_Scn)));
       if (newp == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);