The build of glibc for i686-gnu has been failing for a while with GCC
mainline / GCC 13:
../sysdeps/mach/hurd/getcwd.c: In function '__hurd_canonicalize_directory_name_internal':
../sysdeps/mach/hurd/getcwd.c:242:48: error: pointer 'file_name' may be used after 'realloc' [-Werror=use-after-free]
242 | file_namep = &buf[file_namep - file_name + size / 2];
| ~~~~~~~~~~~^~~~~~~~~~~
../sysdeps/mach/hurd/getcwd.c:236:25: note: call to 'realloc' here
236 | buf = realloc (file_name, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
Fix by doing the subtraction before the reallocation.
Tested with build-many-glibcs.py for i686-gnu.
[samuel.thibault@ens-lyon.rg: Removed mention of this being a bug]
Message-Id: <
18587337-7815-4056-ebd0-
724df262d591@codesourcery.com>
found:
{
/* Prepend the directory name just discovered. */
+ size_t offset = file_namep - file_name;
- if (file_namep - file_name < d->d_namlen + 1)
+ if (offset < d->d_namlen + 1)
{
if (orig_size > 0)
{
free (file_name);
return NULL;
}
- file_namep = &buf[file_namep - file_name + size / 2];
+ file_namep = &buf[offset + size / 2];
file_name = buf;
/* Move current contents up to the end of the buffer.
This is guaranteed to be non-overlapping. */