Initialize variables to prevent uninitialized use in libctf.

Message ID 20210202095542.GA102238@gmail.com
State New
Headers show
Series
  • Initialize variables to prevent uninitialized use in libctf.
Related show

Commit Message

Frederic Cambus Feb. 2, 2021, 9:55 a.m.
libctf/ChangeLog:

	* ctf-create.c (ctf_serialize): Initialize 'err' to zero to
	prevent uninitialized use.
	* ctf-lookup.c (refresh_pptrtab): Initialize 'updated' to zero
	to prevent uninitialized use.
---
 libctf/ChangeLog    | 7 +++++++
 libctf/ctf-create.c | 2 +-
 libctf/ctf-lookup.c | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)

-- 
2.29.2

Comments

H.J. Lu via Binutils Feb. 2, 2021, 12:55 p.m. | #1
On 2 Feb 2021, Frederic Cambus uttered the following:

> libctf/ChangeLog:

>

> 	* ctf-create.c (ctf_serialize): Initialize 'err' to zero to

> 	prevent uninitialized use.

> 	* ctf-lookup.c (refresh_pptrtab): Initialize 'updated' to zero

> 	to prevent uninitialized use.


Thanks! But, er, no thanks? I already have different fixes for these (in
a series not yet applied because it has two lines of ld changes in it,
so Alan needs to look at those quickly).

(It's nice to know someone else is building libctf with clang! The more
independent warning detectors the better!)

> diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c

> index 50f48eb1bb..7d5dac5e69 100644

> --- a/libctf/ctf-create.c

> +++ b/libctf/ctf-create.c

> @@ -688,7 +688,7 @@ ctf_serialize (ctf_dict_t *fp)

>    size_t ndynsyms = 0;

>    const char **sym_name_order = NULL;

>    unsigned char *buf = NULL, *newbuf;

> -  int err;

> +  int err = 0;

>  

>    if (!(fp->ctf_flags & LCTF_RDWR))

>      return (ctf_set_errno (fp, ECTF_RDONLY));


I was very confused by this one and thought it might even be a clang
bug, but it's not, and doing what you do here doesn't actually fix it:
the underlying problem is shadowing (we have *another* 'err', at an
inner scope, which is getting set and then thrown away as we goto the
error handler at an outer scope). See
<20210129192913.416549-5-nick.alcock@oracle.com>.

> diff --git a/libctf/ctf-lookup.c b/libctf/ctf-lookup.c

> index 6d4e085838..d80247d8bd 100644

> --- a/libctf/ctf-lookup.c

> +++ b/libctf/ctf-lookup.c

> @@ -51,7 +51,7 @@ refresh_pptrtab (ctf_dict_t *fp, ctf_dict_t *pfp)

>      {

>        ctf_id_t type = LCTF_INDEX_TO_TYPE (fp, i, 1);

>        ctf_id_t reffed_type;

> -      int updated;

> +      int updated = 0;

>  

>        if (ctf_type_kind (fp, type) != CTF_K_POINTER)

>  	continue;


I fixed this one by ripping out the code that 'updated' applies to, on
the grounds that it's testing for something that is almost certainly a
workaround for a truly ancient misfeature in the Solaris stabs
generator. See <20210129192913.416549-3-nick.alcock@oracle.com>. (I'm
not at all surprised you didn't try for this solution, since it took
several hours of crusty software archaeology to figure out that the code
in question is unnecessary, digging through source files with enticing
leading comments like /* This file is a sewer. */. But it was worth
doing, because the code we can remove as a result is fairly complicated
and arcane.)

Patch

diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index 3a7f6ab1ce..82e696335d 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,10 @@ 
+2021-02-02  Frederic Cambus  <fred@statdns.com>
+
+	* ctf-create.c (ctf_serialize): Initialize 'err' to zero to
+	prevent uninitialized use.
+	* ctf-lookup.c (refresh_pptrtab): Initialize 'updated' to zero
+	to prevent uninitialized use.
+
 2021-01-19  Nick Alcock  <nick.alcock@oracle.com>
 
 	* ctf-create.c (membadd): Transform ""-named members into
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index 50f48eb1bb..7d5dac5e69 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -688,7 +688,7 @@  ctf_serialize (ctf_dict_t *fp)
   size_t ndynsyms = 0;
   const char **sym_name_order = NULL;
   unsigned char *buf = NULL, *newbuf;
-  int err;
+  int err = 0;
 
   if (!(fp->ctf_flags & LCTF_RDWR))
     return (ctf_set_errno (fp, ECTF_RDONLY));
diff --git a/libctf/ctf-lookup.c b/libctf/ctf-lookup.c
index 6d4e085838..d80247d8bd 100644
--- a/libctf/ctf-lookup.c
+++ b/libctf/ctf-lookup.c
@@ -51,7 +51,7 @@  refresh_pptrtab (ctf_dict_t *fp, ctf_dict_t *pfp)
     {
       ctf_id_t type = LCTF_INDEX_TO_TYPE (fp, i, 1);
       ctf_id_t reffed_type;
-      int updated;
+      int updated = 0;
 
       if (ctf_type_kind (fp, type) != CTF_K_POINTER)
 	continue;