[PATCH] Fix potential reent issue

Federico Terraneo fede.tft@hotmail.it
Sun Jun 23 11:15:00 GMT 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoting from the comments in include/reent.h:

Dynamic reentrancy is specified by the __DYNAMIC_REENT__ flag.  This
flag denotes setting up a macro to replace _REENT with a function call
to __getreent().

And in fact, the definition of _REENT in include/sys/reent.h is

#if defined(__DYNAMIC_REENT__) && !defined(__SINGLE_THREAD__)
[...]
# define _REENT (__getreent())
#else
# define _REENT _impure_ptr
#endif

However, this works only if the rest of newlib never uses _impure_ptr
directly. A quick grep on the sources found two uses of _impure_ptr, one
in the definition of stdin, stdout, stderr in stdio.h, and one in the
definition of getwchar/putwchar in wchar.h.

These uses occur only if _REENT_ONLY is defined. So, my understanding is
that if both __DYNAMIC_REENT__ and _REENT_ONLY are defined, those direct
uses of _impure_ptr leak out the wrong reentrancy structure.

I'm also wondering why these even exist when _REENT_ONLY is defined,
shouldn't one use only _stdin_r, _stdout_r, _stderr_r, _getwchar_r and
_putwchar_r in this case, according to the rule that non _r functions
aren't defined when _REENT_ONLY is? Anyway, these are visible in public
headers, so users may well have used those.

Attached is a patch that implements them by calling __getreent() if both
__DYNAMIC_REENT__ and _REENT_ONLY are defined. Note that this came out
of a code review, I don't have such a setup to test this change, so
please look at the patch carefully.

newlib/ChangeLog
2013-06-23  Terraneo Federico  <fede.tft@hotmail.it>

	* libc/include/stdio.h (stdin): Use __getreent when required
          (stdout): Ditto
	  (stderr): Ditto
        * libc/include/wchar.h (getwchar): Ditto
          (putwchar): Ditto
        * libc/include/sys/reent.h: Make __getreent available even if
          _REENT_ONLY is defined.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJRxtivAAoJECkLFtN5Xr9fHC4H/29YzTexscZv2Ez96b/UWexG
kBqyL80OSLiJXX3Vi4B+YqvRS4dANVMXihXu5LCGp/zoqpBk+gpFrBEuwwPtxWyr
mmwBAZ3OV9DlA2YwCbqWbrs2RPHzSaTG6JT0xRCUiIyDK0INPuTgnApj29Lq6A+B
GZeVhOPjRGWMglx1G6oaQYpmYU7bn04TGsRR5GfY6O38ih3fDx8yhW9/pjKP+uFg
jCieTQhv8ETZMaDxhuO/ldMCPokclEC2MNEGnYJ5moP8YYrt3utgrmp/6gVLkl0g
v3NmpmQ2xU89n8gLdYhrkFaSX6Ut+/XuLXdydZf3x1+hut35BspY64NHRPxgcZc=
=wANw
-----END PGP SIGNATURE-----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dynamic_reent.patch
Type: text/x-patch
Size: 2466 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20130623/f66a4bba/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dynamic_reent.patch.sig
Type: application/octet-stream
Size: 287 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20130623/f66a4bba/attachment.obj>


More information about the Newlib mailing list