From owner-svn-src-stable@FreeBSD.ORG Tue Nov 9 20:56:56 2010 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1EEE61065674; Tue, 9 Nov 2010 20:56:56 +0000 (UTC) (envelope-from dumbbell@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 0BBFA8FC0A; Tue, 9 Nov 2010 20:56:56 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id oA9Kutsq044438; Tue, 9 Nov 2010 20:56:55 GMT (envelope-from dumbbell@svn.freebsd.org) Received: (from dumbbell@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id oA9KutqY044436; Tue, 9 Nov 2010 20:56:55 GMT (envelope-from dumbbell@svn.freebsd.org) Message-Id: <201011092056.oA9KutqY044436@svn.freebsd.org> From: Jean-Sebastien Pedron Date: Tue, 9 Nov 2010 20:56:55 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-7@freebsd.org X-SVN-Group: stable-7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r215055 - stable/7/usr.bin/fold X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Nov 2010 20:56:56 -0000 Author: dumbbell Date: Tue Nov 9 20:56:55 2010 New Revision: 215055 URL: http://svn.freebsd.org/changeset/base/215055 Log: MFC r214893: Fix a segmentation fault in argument processing. The crash was caused by a command line such as this one: # fold -b1 PR: bin/151592 Reported by: Marcus Reid Tested by: Marcus Reid Modified: stable/7/usr.bin/fold/fold.c Directory Properties: stable/7/usr.bin/fold/ (props changed) Modified: stable/7/usr.bin/fold/fold.c ============================================================================== --- stable/7/usr.bin/fold/fold.c Tue Nov 9 20:46:41 2010 (r215054) +++ stable/7/usr.bin/fold/fold.c Tue Nov 9 20:56:55 2010 (r215055) @@ -71,14 +71,14 @@ int sflag; /* Split on word boundaries int main(int argc, char **argv) { - int ch; + int ch, previous_ch; int rval, width; - char *p; (void) setlocale(LC_CTYPE, ""); width = -1; - while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) + previous_ch = 0; + while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) { switch (ch) { case 'b': bflag = 1; @@ -93,17 +93,33 @@ main(int argc, char **argv) break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - if (width == -1) { - p = argv[optind - 1]; - if (p[0] == '-' && p[1] == ch && !p[2]) - width = atoi(++p); - else - width = atoi(argv[optind] + 1); + /* Accept a width as eg. -30. Note that a width + * specified using the -w option is always used prior + * to this undocumented option. */ + switch (previous_ch) { + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + /* The width is a number with multiple digits: + * add the last one. */ + width = width * 10 + (ch - '0'); + break; + default: + /* Set the width, unless it was previously + * set. For instance, the following options + * would all give a width of 5 and not 10: + * -10 -w5 + * -5b10 + * -5 -10b */ + if (width == -1) + width = ch - '0'; + break; } break; default: usage(); } + previous_ch = ch; + } argv += optind; argc -= optind;