4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-21 00:07:36 +08:00

stdio: Fix bugs in commit 3d94e07c49b5

The commit 3d94e07c49b5 has a few bugs which cause testsuite failure
in libstdc++. This is due to excess orientation check in __srefill_r()
and _ungetc_r(). Further, sscanf() family also calls ssvfscanf() family
with fp->_file == -1. This causes undesired orientation set/check for
sscanf() family. This patch fixes these problems.

Also, as in GLibC, do not set orientation in ungetc, and only set, but
do not check orientation in ungetwc.

Fixes: 3d94e07c49b5 ("newlib: libc: Fix crash on fprintf to a wide-oriented stream.")
Reported-by: Christophe Lyon <christophe.lyon@linaro.org>
Reported-by: Joel Sherrill <joel@rtems.org>
Co-developed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Takashi Yano 2023-11-11 01:09:47 +09:00 committed by Corinna Vinschen
parent d36b4f3f06
commit bc986b7ff6
27 changed files with 43 additions and 12 deletions

View File

@ -35,6 +35,7 @@ _asiprintf_r (struct _reent *ptr,
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
@ -61,6 +62,7 @@ asiprintf (char **strp,
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */

View File

@ -36,6 +36,7 @@ _asniprintf_r (struct _reent *ptr,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t
@ -82,6 +83,7 @@ asniprintf (char *buf,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t

View File

@ -36,6 +36,7 @@ _asnprintf_r (struct _reent *__restrict ptr,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t
@ -88,6 +89,7 @@ asnprintf (char *__restrict buf,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t

View File

@ -35,6 +35,7 @@ _asprintf_r (struct _reent *ptr,
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
@ -67,6 +68,7 @@ asprintf (char **__restrict strp,
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */

View File

@ -81,6 +81,9 @@ _getc_r (struct _reent *ptr,
{
int result;
CHECK_INIT (ptr, fp);
if (ORIENT (fp, -1) != -1)
return EOF;
_newlib_flockfile_start (fp);
result = __sgetc_r (ptr, fp);
_newlib_flockfile_end (fp);
@ -96,6 +99,9 @@ getc (register FILE *fp)
struct _reent *reent = _REENT;
CHECK_INIT (reent, fp);
if (ORIENT (fp, -1) != -1)
return EOF;
_newlib_flockfile_start (fp);
result = __sgetc_r (reent, fp);
_newlib_flockfile_end (fp);

View File

@ -43,9 +43,6 @@ __srefill_r (struct _reent * ptr,
CHECK_INIT (ptr, fp);
if (ORIENT (fp, -1) != -1)
return EOF;
fp->_r = 0; /* largely a convenience for callers */
/* SysV does not make this test; take it out for compatibility */

View File

@ -108,6 +108,7 @@ _siprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */
@ -129,6 +130,7 @@ siprintf (char *str,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */

View File

@ -90,6 +90,7 @@ siscanf (const char *str,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;
@ -114,6 +115,7 @@ _siscanf_r (struct _reent *ptr,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;

View File

@ -42,6 +42,7 @@ _sniprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */
@ -73,6 +74,7 @@ sniprintf (char *str,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */

View File

@ -41,6 +41,7 @@ _snprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */
@ -78,6 +79,7 @@ snprintf (char *__restrict str,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */

View File

@ -584,6 +584,7 @@ _sprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */
@ -611,6 +612,7 @@ sprintf (char *__restrict str,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */

View File

@ -429,6 +429,7 @@ sscanf (const char *__restrict str,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;
@ -459,6 +460,7 @@ _sscanf_r (struct _reent *ptr,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;

View File

@ -568,6 +568,7 @@ _swprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? (size - 1) * sizeof (wchar_t) : 0);
f._file = -1; /* No file. */
@ -608,6 +609,7 @@ swprintf (wchar_t *__restrict str,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? (size - 1) * sizeof (wchar_t) : 0);
f._file = -1; /* No file. */

View File

@ -422,6 +422,7 @@ swscanf (const wchar_t *__restrict str, const wchar_t *__restrict fmt, ...)
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = wcslen (str) * sizeof (wchar_t);
f._read = __seofread;
@ -446,6 +447,7 @@ _swscanf_r (struct _reent *ptr, const wchar_t *str, const wchar_t *fmt, ...)
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = wcslen (str) * sizeof (wchar_t);
f._read = __seofread;

View File

@ -125,12 +125,6 @@ _ungetc_r (struct _reent *rptr,
_newlib_flockfile_start (fp);
if (ORIENT (fp, -1) != -1)
{
_newlib_flockfile_exit (fp);
return EOF;
}
/* After ungetc, we won't be at eof anymore */
fp->_flags &= ~__SEOF;

View File

@ -82,9 +82,8 @@ _ungetwc_r (struct _reent *ptr,
size_t len;
_newlib_flockfile_start (fp);
if (ORIENT (fp, 1) != 1)
wc = WEOF;
else if (wc == WEOF)
ORIENT (fp, 1);
if (wc == WEOF)
wc = WEOF;
else if ((len = _wcrtomb_r(ptr, buf, wc, &fp->_mbstate)) == (size_t)-1)
{

View File

@ -50,6 +50,7 @@ _vasiprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR | __SMBF ;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */

View File

@ -36,6 +36,7 @@ _vasniprintf_r (struct _reent *ptr,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t

View File

@ -36,6 +36,7 @@ _vasnprintf_r (struct _reent *ptr,
len = 0;
buf = NULL;
}
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t

View File

@ -56,6 +56,7 @@ _vasprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR | __SMBF ;
f._flags2 = 0;
f._bf._base = f._p = NULL;
f._bf._size = f._w = 0;
f._file = -1; /* No file. */

View File

@ -50,6 +50,7 @@ _vsiprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */

View File

@ -49,6 +49,7 @@ _vsiscanf_r (struct _reent *ptr,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;

View File

@ -58,6 +58,7 @@ _vsniprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */

View File

@ -64,6 +64,7 @@ _vsnprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
f._file = -1; /* No file. */

View File

@ -56,6 +56,7 @@ _vsprintf_r (struct _reent *ptr,
FILE f;
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = INT_MAX;
f._file = -1; /* No file. */

View File

@ -55,6 +55,7 @@ _vsscanf_r (struct _reent *ptr,
FILE f;
f._flags = __SRD | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._r = strlen (str);
f._read = __seofread;

View File

@ -46,6 +46,7 @@ _vswprintf_r (struct _reent *ptr,
return EOF;
}
f._flags = __SWR | __SSTR;
f._flags2 = 0;
f._bf._base = f._p = (unsigned char *) str;
f._bf._size = f._w = (size > 0 ? (size - 1) * sizeof (wchar_t) : 0);
f._file = -1; /* No file. */