stacktrace: Init elf_fd in sysprof_init_dwfl

Message ID 20241024090608.391906-1-mark@klomp.org
State Committed
Headers
Series stacktrace: Init elf_fd in sysprof_init_dwfl |

Commit Message

Mark Wielaard Oct. 24, 2024, 9:06 a.m. UTC
  When building with LTO gcc believes elf_fd can be used uninitialized:

In function ‘sysprof_init_dwfl’,
    inlined from ‘sysprof_unwind_cb’ at stacktrace.c:1235:16:
stacktrace.c:1087:7: error: ‘elf_fd’ may be used uninitialized [-Werror=maybe-uninitialized]
 1087 |       close (elf_fd);
      |       ^

This code won't be reached because if find_procfile doesn't initialize
elf_fd, it will return an error. But help the compiler by initializing
elf_fd to -1.

	* src/stacktrace.c (sysprof_init_dwfl): Init elf_fd to -1.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/stacktrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Aaron Merey Oct. 24, 2024, 2:22 p.m. UTC | #1
On Thu, Oct 24, 2024 at 5:08 AM Mark Wielaard <mark@klomp.org> wrote:
>
> When building with LTO gcc believes elf_fd can be used uninitialized:
>
> In function ‘sysprof_init_dwfl’,
>     inlined from ‘sysprof_unwind_cb’ at stacktrace.c:1235:16:
> stacktrace.c:1087:7: error: ‘elf_fd’ may be used uninitialized [-Werror=maybe-uninitialized]
>  1087 |       close (elf_fd);
>       |       ^
>
> This code won't be reached because if find_procfile doesn't initialize
> elf_fd, it will return an error. But help the compiler by initializing
> elf_fd to -1.
>
>         * src/stacktrace.c (sysprof_init_dwfl): Init elf_fd to -1.
>
> Signed-off-by: Mark Wielaard <mark@klomp.org>
> ---
>  src/stacktrace.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/stacktrace.c b/src/stacktrace.c
> index 438cb1dd0d38..b912ca5de502 100644
> --- a/src/stacktrace.c
> +++ b/src/stacktrace.c
> @@ -1033,7 +1033,7 @@ sysprof_init_dwfl (struct sysprof_unwind_info *sui,
>      }
>
>    Elf *elf = NULL;
> -  int elf_fd;
> +  int elf_fd = -1;
>    err = find_procfile (dwfl, &pid, &elf, &elf_fd);
>    if (err < 0)
>      {
> --
> 2.47.0
>

LGTM

Thanks,
Aaron
  
Mark Wielaard Oct. 24, 2024, 4:40 p.m. UTC | #2
Hi Aaron,

On Thu, 2024-10-24 at 10:22 -0400, Aaron Merey wrote:
> On Thu, Oct 24, 2024 at 5:08 AM Mark Wielaard <mark@klomp.org> wrote:
> > 
> > When building with LTO gcc believes elf_fd can be used uninitialized:
> 
> LGTM

Thanks, pushed.

Cheers,

Mark
  

Patch

diff --git a/src/stacktrace.c b/src/stacktrace.c
index 438cb1dd0d38..b912ca5de502 100644
--- a/src/stacktrace.c
+++ b/src/stacktrace.c
@@ -1033,7 +1033,7 @@  sysprof_init_dwfl (struct sysprof_unwind_info *sui,
     }
 
   Elf *elf = NULL;
-  int elf_fd;
+  int elf_fd = -1;
   err = find_procfile (dwfl, &pid, &elf, &elf_fd);
   if (err < 0)
     {