[patch] libc/stdio/findfp.c: Avoid infinite recursion

Corinna Vinschen vinschen@redhat.com
Tue Jun 19 12:54:00 GMT 2012


I just applied the below patch.  It avoids an infinite recursion on
_REENT_SMALL targets.  This problem has been introduced by the patch
which I applied as aresult of the discussion in

The problem with this patch is that __sinit calls __sfp when running on
_REENT_SMALL targets.  It does so while __sdidinit is still 0.  But
__sfp starts with

  if (!_GLOBAL_REENT->__sdidinit)
    __sinit (_GLOBAL_REENT);

so it calls __sinit again, which calls __sfp again, ad infinitum.  The
applied patch handles this issue by setting __sdidinit before calling
__sfp, if the current reent pointer points to _GLOBAL_REENT.

	* libc/stdio/findfp.c (__sinit): Avoid infinite recursion on
	_REENT_SMALL targets.  Add comment to explain.

Index: libc/stdio/findfp.c
RCS file: /cvs/src/src/newlib/libc/stdio/findfp.c,v
retrieving revision 1.23
diff -u -p -r1.23 findfp.c
--- libc/stdio/findfp.c	30 May 2012 08:58:42 -0000	1.23
+++ libc/stdio/findfp.c	5 Jun 2012 16:16:42 -0000
@@ -200,6 +200,11 @@ _DEFUN(__sinit, (s),
   s->__sglue._niobs = 0;
   s->__sglue._iobs = NULL;
+  /* Avoid infinite recursion when calling __sfp  for _GLOBAL_REENT.  The
+     problem is that __sfp checks for _GLOBAL_REENT->__sdidinit and calls
+     __sinit if it's 0. */
+  if (s == _GLOBAL_REENT)
+    s->__sdidinit = 1;
   s->_stdin = __sfp(s);
   s->_stdout = __sfp(s);
   s->_stderr = __sfp(s);


Corinna Vinschen
Cygwin Project Co-Leader
Red Hat

More information about the Newlib mailing list