Bug 23301 - free on unitialized value
Summary: free on unitialized value
Status: RESOLVED FIXED
Alias: None
Product: elfutils
Classification: Unclassified
Component: libdw (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Mark Wielaard
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-06-17 08:44 UTC by Luiz Angelo Daros de Luca
Modified: 2018-06-18 11:32 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2018-06-17 00:00:00


Attachments
patch: dwarf_getsrclines.c: Fix unitialized usage of filelist (1.04 KB, patch)
2018-06-17 08:44 UTC, Luiz Angelo Daros de Luca
Details | Diff
libdw: Initialize filelist earlier in dwarf_getsrclines.c read_srclines. (819 bytes, patch)
2018-06-17 09:44 UTC, Mark Wielaard
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Luiz Angelo Daros de Luca 2018-06-17 08:44:58 UTC
Created attachment 11075 [details]
patch: dwarf_getsrclines.c: Fix unitialized usage of filelist

I'm getting this error with 0.172:

dwarf_getsrclines.c: In function 'read_srclines':
dwarf_getsrclines.c:1074:7: error: 'filelist' may be used uninitialized in this function [-Werror=maybe-uninitialized]
       free (filelist);
       ^~~~~~~~~~~~~~~

It seems that gcc is right here as there is "ifs" that go to "out" (where filelist is freed) before freelist is initialized. I fixed by just moving filelist initialization to before the first usage of "goto out;". Maybe there is a more elegant approach.
Comment 1 Mark Wielaard 2018-06-17 09:44:10 UTC
Created attachment 11076 [details]
libdw: Initialize filelist earlier in dwarf_getsrclines.c read_srclines.

Hi,

gcc is wrong, filelist will not be used/freed if nfilelist == 0, and filelist will be initialized if nfilelist != 0. But this might be hard to see for the compiler. BTW which gcc version is this, any special CFLAGS used?

Instead of moving the whole block of code around I think just explicitly setting filelist to NULL when we initialize nfilelist = 0 should do the trick.

Could you test the slightly changed patch attached?

Also would it me OK to add a Signed-off-by: Luiz Angelo Daros de Luca  <luizluca@gmail.com> line as described in https://sourceware.org/git/?p=elfutils.git;a=blob_plain;f=CONTRIBUTING;hb=HEAD

Thanks,

Mark
Comment 2 Luiz Angelo Daros de Luca 2018-06-18 07:45:13 UTC
Mark,

It works. Gcc does not blame me anymore.

Thanks.
Comment 3 Mark Wielaard 2018-06-18 11:32:00 UTC
commit 9e16a100bf8e0d43415253fe8cfd3ba1d8e637d1
Author: Luiz Angelo Daros de Luca <luizluca@gmail.com>
Date:   Sun Jun 17 11:34:08 2018 +0200

    libdw: Initialize filelist earlier in dwarf_getsrclines.c read_srclines.
    
    I'm getting this error with 0.172:
    
    dwarf_getsrclines.c: In function 'read_srclines':
    dwarf_getsrclines.c:1074:7: error: 'filelist' may be used uninitialized in t
           free (filelist);
           ^~~~~~~~~~~~~~~
    
    It seems that gcc is right here as there is "ifs" that go to "out"
    (where filelist is freed) before freelist is initialized.
    
    Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>