[PATCH 6/6] Cygwin: Suppress false positive use-after-free warnings in __set_lc_time_from_win()

Corinna Vinschen corinna-cygwin@cygwin.com
Wed Aug 7 14:18:28 GMT 2024


On Aug  6 20:03, Jon Turney wrote:
> On 04/08/2024 22:48, Jon Turney wrote:
> > Supress new use-after-free warnings about realloc(), seen with gcc 12, e.g.:
> > 
> > > In function ‘void rebase_locale_buf(const void*, const void*, const char*, const char*, const char*)’,
> > >      inlined from ‘int __set_lc_time_from_win(const char*, const lc_time_T*, lc_time_T*, char**, wctomb_p, const char*)’ at ../../../../src/winsup/cygwin/nlsfuncs.cc:705:25:
> > > ../../../../src/winsup/cygwin/nlsfuncs.cc:338:24: error: pointer ‘new_lc_time_buf’ may be used after ‘void* realloc(void*, size_t)’ [-Werror=use-after-free]
> > >    338 |       *ptrs += newbase - oldbase;
> > >        |                ~~~~~~~~^~~~~~~~~
> > > ../../../../src/winsup/cygwin/nlsfuncs.cc: In function ‘int __set_lc_time_from_win(const char*, const lc_time_T*, lc_time_T*, char**, wctomb_p, const char*)’:
> > > ../../../../src/winsup/cygwin/nlsfuncs.cc:699:44: note: call to ‘void* realloc(void*, size_t)’ here
> > >    699 |               char *tmp = (char *) realloc (new_lc_time_buf, len);
> > 
> > We do some calculations using the pointer passed to realloc(), but do
> > not not dereference it, so this seems safe?
>  Since this is less than ideal, here's the version where we explicitly
> malloc() the new buffer, adjust things, then free() the old buffer.
> 
> This is all quite hairy, though, and I have no idea how to begin to test
> this, so if you have some pointers to share, that would be good.

No pointers as such, but tcsh uses it's own allocator, while bash
doesn't. So testing involves running bash and tcsh and then changing
LC_ALL to any odd (but existing) locale, e.g.

  en_US, de_DE.utf8, fa_IR, fa_IR.utf8, zh_HK, zh_HK.utf8, you name it

  bash$ export LC_ALL=foo
  tcsh$ setenv LC_ALL foo

This shoudn't crash bash nor tcsh.

FWIW, your patch looks right to me.


Thanks,
Corinna


More information about the Cygwin-patches mailing list