public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Sergei Trofimovich <slyich@gmail.com>
To: elfutils-devel@sourceware.org
Cc: Sergei Trofimovich <slyich@gmail.com>
Subject: [PATCH] libelf/elf_newscn.c: fix build failure against gcc-14 (-Walloc-size)
Date: Thu,  2 Nov 2023 19:58:46 +0000	[thread overview]
Message-ID: <20231102195846.1414311-1-slyich@gmail.com> (raw)

`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(-)

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);
-- 
2.42.0


             reply	other threads:[~2023-11-02 19:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-02 19:58 Sergei Trofimovich [this message]
2023-11-02 23:23 ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231102195846.1414311-1-slyich@gmail.com \
    --to=slyich@gmail.com \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).