[PATCH] strip: Add check for elf_begin() result

Mark Wielaard mark@klomp.org
Fri Apr 5 19:04:52 GMT 2024


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
> 


More information about the Elfutils-devel mailing list