Date: Thu, 13 Jun 2002 17:55:58 -0700 (PDT) From: Jonathan Lennox <lennox@cs.columbia.edu> To: freebsd-gnats-submit@FreeBSD.org Subject: standards/39256: [v]snprintf aren't POSIX-conformant for strings longer than INT_MAX Message-ID: <200206140055.g5E0twZ8015124@www.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 39256 >Category: standards >Synopsis: [v]snprintf aren't POSIX-conformant for strings longer than INT_MAX >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-standards >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Jun 13 18:10:01 PDT 2002 >Closed-Date: >Last-Modified: >Originator: Jonathan Lennox >Release: 4.3, but verified in -CURRENT sources using cvsweb >Organization: Columbia University >Environment: >Description: POSIX 2001's specification of snprintf <http://www.opengroup.org/onlinepubs/007904975/functions/snprintf.html> specifies that if either the value of n, or the number of bytes required for the format, are greater than INT_MAX, then snprintf should return -1 and set errno to EOVERFLOW. FreeBSD, by contrast, just "clamps" the value of n at INT_MAX. On overflow of the bytes to be converted, it does return -1, but doesn't set errno. Returning EOVERFLOW is an XSI extension, but, I think, a good one. >How-To-Repeat: char buf[80]; int ret = snprintf(buf, (size_t)INT_MAX + 1, "Hello, world!\n"); Expected return: -1, errno = EOVERFLOW. Actual return: snprintf fills in the string "properly". (I didn't actually allocate a buffer of size INT_MAX+1, so this can be demonstrated on mere mortals' computers.) Repeating the problem of the actual buffer is harder, since you need a machine with more than 2 gigabytes of memory. Assuming you have one, though: size_t bufsiz = INT_MAX + 1; char * buf = malloc(bufsiz); int ret; memset(buf, 'a', bufsiz - 1); buf[bufsiz] = '\0'; ret = snprintf(NULL, 0, "%s", buf); Expected result: -1, errno = EOVERFLOW. Actual return: -1, errno undefined. >Fix: Change the test n > INT_MAX in snprintf.c and vsnprintf.c to set errno = EOVERFLOW and return, rather than setting n = INT_MAX. Change __vfprintf to set errno = EOVERFLOW, in addition to returning EOF, if ret + prsize is greater than INT_MAX. >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200206140055.g5E0twZ8015124>