From owner-freebsd-bugs@FreeBSD.ORG Mon Nov 2 00:30:02 2009 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6F73F1065670 for ; Mon, 2 Nov 2009 00:30:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 4CBB88FC16 for ; Mon, 2 Nov 2009 00:30:02 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id nA20U2vW040404 for ; Mon, 2 Nov 2009 00:30:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id nA20U2m0040398; Mon, 2 Nov 2009 00:30:02 GMT (envelope-from gnats) Resent-Date: Mon, 2 Nov 2009 00:30:02 GMT Resent-Message-Id: <200911020030.nA20U2m0040398@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Mikko Tyolajarvi Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 57803106566C for ; Mon, 2 Nov 2009 00:28:11 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id 46AB58FC14 for ; Mon, 2 Nov 2009 00:28:11 +0000 (UTC) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.3/8.14.3) with ESMTP id nA20SBDB014990 for ; Mon, 2 Nov 2009 00:28:11 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.3/8.14.3/Submit) id nA20SAZ7014989; Mon, 2 Nov 2009 00:28:11 GMT (envelope-from nobody) Message-Id: <200911020028.nA20SAZ7014989@www.freebsd.org> Date: Mon, 2 Nov 2009 00:28:11 GMT From: Mikko Tyolajarvi To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/140185: [patch] expand_number() does not detect overflow in numeric part X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Nov 2009 00:30:02 -0000 >Number: 140185 >Category: bin >Synopsis: [patch] expand_number() does not detect overflow in numeric part >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Mon Nov 02 00:30:01 UTC 2009 >Closed-Date: >Last-Modified: >Originator: Mikko Tyolajarvi >Release: 7.2-STABLE >Organization: >Environment: FreeBSD antec.home 7.2-STABLE FreeBSD 7.2-STABLE #1: Fri Sep 4 19:36:49 PDT 2009 mikko@antec.home:/usr/obj/usr/src/sys/GENERIC i386 >Description: The expand_number() function will silently truncate the numeric part to the size of a maxint_t and if there is no suffix, no error is returned. Overflow in strings that include a suffix is detected (e.g. "8E") The patch is against -CURRENT. >How-To-Repeat: Compile and run this program with no arguments. It should print "ok". #include #include #include #include #include int main(int argc, const char *argv[]) { int64_t num = 0; const char *s; int rc; s = (argc > 1) ? argv[1] : "9223372036854775808"; /* 2^63 */ rc = expand_number(s, &num); if (rc < 0 && errno == ERANGE) { printf("ok\n"); return 0; } printf("nope. rc = %d, num = %lld\n", rc, num); return 1; } >Fix: Check return value and errno from strtoimax(). Patch attached. Patch attached with submission follows: --- expand_number.c.orig 2009-11-01 16:10:42.000000000 -0800 +++ expand_number.c 2009-11-01 16:12:54.000000000 -0800 @@ -52,10 +52,17 @@ static const char unit[] = "bkmgtpe"; char *endptr, s; int64_t number; - int i; + int i, oerrno; + oerrno = errno; + errno = 0; number = strtoimax(buf, &endptr, 0); + if ((number == INTMAX_MIN || number == INTMAX_MAX) && errno == ERANGE) + return (-1); + + errno = oerrno; + if (endptr == buf) { /* No valid digits. */ errno = EINVAL; >Release-Note: >Audit-Trail: >Unformatted: