[PATCH] canonicalize: Initialize 'dest' pointer in 'realpath_stk' [BZ #32577]

Xi Ruoyao xry111@xry111.site
Mon Jan 20 09:54:56 GMT 2025


On Mon, 2025-01-20 at 10:24 +0100, Nikhil R wrote:
> From: Nikhil R <nikhilr5@kpit.com>
> 
> Fixes a GCC warning when compiling with the '-Os' optimization flag:
> > canonicalize.c: In function 'realpath_stk':
> > canonicalize.c:424:50: error: 'dest' may be used uninitialized in
> > this function [-Werror=maybe-uninitialized]
> >   424 |   return scratch_buffer_dupfree (rname_buf, dest - rname);

This line does not exist in the latest Glibc code base.  Please recheck
with the master branch.

> The `dest` pointer is now explicitly initialized to ensure
> it is not used uninitialized, preventing potential undefined
> behavior and eliminating the warning.

And in the latest code base dest just cannot be used uninitialized.  The
code now reads like:

if (...)
  {
    while (...)
      {
        if (...)
          {
            dest = rname;
            goto error;
          }
        if (...)
          return NULL;
        ... ...
      }
    dest = strchr (rname, '\0');
    ... ...
  }
else
  {
    dest = __mempcpy (rname, name, prefix_len);
    ... ...
  }

It should be obvious that the execution cannot leave the if statement
w/o initializing dest.  Thus there is no potential undefined behavior. 
So even if you can reproduce the warning with the latest code base, you
will still need to reword this part of the commit message to make it
clear that this is a false warning.

And if the false warning exists, it should be reported as a compiler bug
(like what I've done for https://gcc.gnu.org/PR118216).

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Libc-alpha mailing list