From owner-freebsd-standards Thu Jun 13 18:10:23 2002 Delivered-To: freebsd-standards@hub.freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id B7C8937B413 for ; Thu, 13 Jun 2002 18:10:01 -0700 (PDT) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.6/8.11.6) id g5E1A1A32669; Thu, 13 Jun 2002 18:10:01 -0700 (PDT) (envelope-from gnats) Received: from nwww.freebsd.org (www.FreeBSD.org [216.136.204.117]) by hub.freebsd.org (Postfix) with ESMTP id 7B1CF37B437 for ; Thu, 13 Jun 2002 18:00:14 -0700 (PDT) Received: from www.freebsd.org (localhost [127.0.0.1]) by nwww.freebsd.org (8.12.2/8.12.2) with ESMTP id g5E0twhG015125 for ; Thu, 13 Jun 2002 17:55:58 -0700 (PDT) (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.12.2/8.12.2/Submit) id g5E0twZ8015124; Thu, 13 Jun 2002 17:55:58 -0700 (PDT) Message-Id: <200206140055.g5E0twZ8015124@www.freebsd.org> Date: Thu, 13 Jun 2002 17:55:58 -0700 (PDT) From: Jonathan Lennox To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-1.0 Subject: standards/39256: [v]snprintf aren't POSIX-conformant for strings longer than INT_MAX Sender: owner-freebsd-standards@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG >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 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