Parcourir la source

fix idiom for setting stdio stream orientation to wide

the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
setting byte orientation, f->mode |= f->mode-1, but the adaptation was
incorrect. unless the stream was alreasdy set byte-oriented, this code
incremented f->mode each time it was executed, which would eventually
lead to overflow. it could be fixed by changing it to f->mode |= 1,
but upcoming changes will require slightly more work at the time of
wide orientation, so it makes sense to just call fwide. as an
optimization in the single-character functions, fwide is only called
if the stream is not already wide-oriented.
Rich Felker il y a 9 ans
Parent
commit
536c6d5a42
6 fichiers modifiés avec 6 ajouts et 6 suppressions
  1. 1 1
      src/stdio/fgetwc.c
  2. 1 1
      src/stdio/fputwc.c
  3. 1 1
      src/stdio/fputws.c
  4. 1 1
      src/stdio/ungetwc.c
  5. 1 1
      src/stdio/vfwprintf.c
  6. 1 1
      src/stdio/vfwscanf.c

+ 1 - 1
src/stdio/fgetwc.c

@@ -10,7 +10,7 @@ wint_t __fgetwc_unlocked(FILE *f)
 	unsigned char b;
 	size_t l;
 
-	f->mode |= f->mode+1;
+	if (f->mode <= 0) fwide(f, 1);
 
 	/* Convert character from buffer if possible */
 	if (f->rpos < f->rend) {

+ 1 - 1
src/stdio/fputwc.c

@@ -8,7 +8,7 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
 	char mbc[MB_LEN_MAX];
 	int l;
 
-	f->mode |= f->mode+1;
+	if (f->mode <= 0) fwide(f, 1);
 
 	if (isascii(c)) {
 		c = putc_unlocked(c, f);

+ 1 - 1
src/stdio/fputws.c

@@ -8,7 +8,7 @@ int fputws(const wchar_t *restrict ws, FILE *restrict f)
 
 	FLOCK(f);
 
-	f->mode |= f->mode+1;
+	fwide(f, 1);
 
 	while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
 		if (__fwritex(buf, l, f) < l) {

+ 1 - 1
src/stdio/ungetwc.c

@@ -11,7 +11,7 @@ wint_t ungetwc(wint_t c, FILE *f)
 
 	FLOCK(f);
 
-	f->mode |= f->mode+1;
+	if (f->mode <= 0) fwide(f, 1);
 
 	if (!f->rpos) __toread(f);
 	if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||

+ 1 - 1
src/stdio/vfwprintf.c

@@ -359,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
 	}
 
 	FLOCK(f);
-	f->mode |= f->mode+1;
+	fwide(f, 1);
 	olderr = f->flags & F_ERR;
 	f->flags &= ~F_ERR;
 	ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);

+ 1 - 1
src/stdio/vfwscanf.c

@@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
 
 	FLOCK(f);
 
-	f->mode |= f->mode+1;
+	fwide(f, 1);
 
 	for (p=fmt; *p; p++) {