From 14ba5e14d9226cb4dd1b2a72781ee02e9f2bf8a2 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Thu, 15 Mar 2007 18:40:48 +0000 Subject: 2007-03-15 Eric Blake * libc/stdio/local.h (cantwrite, FREEUB, FREELB): Make reentrant. (__smakebuf): Rename... (__smakebuf_r): to this. * libc/stdio/fvwrite.h (__swsetup_r): Rename, from __swsetup. * libc/stdio/makebuf.c (__smakebuf): Detect failed asprint allocation, then rename... (__smakebuf_r): ...to this and fix reentrancy. * libc/stdio/wsetup.c (__swsetup): Detect failed asprintf allocation, then rename... (__swsetup_r): ...to this and fix reentrancy. * libc/stdio/fseek.c (_fseek_r): Fix reentrancy. * libc/stdio/refill.c (__srefill_r): Likewise. * libc/stdio/fclose.c (_fclose_r): Likewise. * libc/stdio/fread.c (_fread_r): Likewise. * libc/stdio/freopen.c (_freopen_r): Likewise. * libc/stdio/wbuf.c (__swbuf_r): Likewise. * libc/stdio64/fseeko64.c (_fseeko64_r): Likewise. * libc/stdio/fvwrite.c (__sfvwrite_r): Set errno properly on failed asprintf allocation, and fix reentrancy. * libc/stdio/snprintf.c (snprintf, _snprintf_r): Report overflow, as required by POSIX. * libc/stdio/sniprintf.c (sniprintf, _sniprintf_r): Likewise. * libc/stdio/vsnprintf.c (vsnprintf, _vsnprintf_r): Likewise. * libc/stdio/vsniprintf.c (vsniprintf, _vsniprintf_r): Likewise. --- newlib/libc/stdio/sniprintf.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'newlib/libc/stdio/sniprintf.c') diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c index 7650cd080..8059b53f3 100644 --- a/newlib/libc/stdio/sniprintf.c +++ b/newlib/libc/stdio/sniprintf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1990 The Regents of the University of California. + * Copyright (c) 1990, 2007 The Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted @@ -26,6 +26,7 @@ #include #endif #include +#include #include "local.h" int @@ -48,6 +49,11 @@ _sniprintf_r (ptr, str, size, fmt, va_alist) va_list ap; FILE f; + if (size > INT_MAX) + { + ptr->_errno = EOVERFLOW; + return EOF; + } f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; f._bf._size = f._w = (size > 0 ? size - 1 : 0); @@ -59,6 +65,8 @@ _sniprintf_r (ptr, str, size, fmt, va_alist) #endif ret = _vfiprintf_r (ptr, &f, fmt, ap); va_end (ap); + if (ret < EOF) + ptr->_errno = EOVERFLOW; if (size > 0) *f._p = 0; return (ret); @@ -83,7 +91,13 @@ sniprintf (str, size, fmt, va_alist) int ret; va_list ap; FILE f; + struct _reent *ptr = _REENT; + if (size > INT_MAX) + { + ptr->_errno = EOVERFLOW; + return EOF; + } f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; f._bf._size = f._w = (size > 0 ? size - 1 : 0); @@ -93,8 +107,10 @@ sniprintf (str, size, fmt, va_alist) #else va_start (ap); #endif - ret = _vfiprintf_r (_REENT, &f, fmt, ap); + ret = _vfiprintf_r (ptr, &f, fmt, ap); va_end (ap); + if (ret < EOF) + ptr->_errno = EOVERFLOW; if (size > 0) *f._p = 0; return (ret); -- cgit v1.2.3