public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: Maks Mishin <maks.mishinfz@gmail.com>
Cc: elfutils-devel@sourceware.org
Subject: Re: [PATCH] strip: Add check for elf_begin() result
Date: Fri, 5 Apr 2024 21:04:52 +0200	[thread overview]
Message-ID: <20240405190452.GH1292@gnu.wildebeest.org> (raw)
In-Reply-To: <20240405185304.13584-1-maks.mishinFZ@gmail.com>

Hi Maks,

On Fri, Apr 05, 2024 at 09:53:03PM +0300, Maks Mishin wrote:
> Return value of a function 'elf_begin' is dereferenced at strip.c:1166
> without checking for NULL, but it is usually checked for this function.

Please stop sending these "patches" till you actually understand the
code.

In this case if debugelf == NULL then gelf_newehdr will also return
NULL to indicate the earlier error.

This is a common pattern in the libelf code.

Thanks,

Mark

> Found by RASU JSC.
> 
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
> ---
>  src/strip.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/src/strip.c b/src/strip.c
> index 6436443d..ebab4866 100644
> --- a/src/strip.c
> +++ b/src/strip.c
> @@ -1153,7 +1153,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
>      {
>        /* Also create an ELF descriptor for the debug file */
>        debugelf = elf_begin (debug_fd, ELF_C_WRITE, NULL);
> -      if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
> +      ELF_CHECK (debugelf != NULL, _("cannot create ELF descriptor: %s"));
> +
> +	  if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
>  	{
>  	  error (0, 0, _("cannot create new ehdr for file '%s': %s"),
>  		 debug_fname, elf_errmsg (-1));
> -- 
> 2.30.2
> 

      reply	other threads:[~2024-04-05 19:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 18:53 Maks Mishin
2024-04-05 19:04 ` Mark Wielaard [this message]

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=20240405190452.GH1292@gnu.wildebeest.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=maks.mishinfz@gmail.com \
    /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).