From owner-freebsd-bugs@FreeBSD.ORG Sun Sep 14 05:20:01 2008 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 E9371106566C for ; Sun, 14 Sep 2008 05:20:01 +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 CD97A8FC15 for ; Sun, 14 Sep 2008 05:20:01 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id m8E5K1nW010824 for ; Sun, 14 Sep 2008 05:20:01 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id m8E5K10N010823; Sun, 14 Sep 2008 05:20:01 GMT (envelope-from gnats) Resent-Date: Sun, 14 Sep 2008 05:20:01 GMT Resent-Message-Id: <200809140520.m8E5K10N010823@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, Andrei Lavreniyuk Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C5BA2106564A for ; Sun, 14 Sep 2008 05:16:45 +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 BC2158FC13 for ; Sun, 14 Sep 2008 05:16:45 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m8E5GiwL066530 for ; Sun, 14 Sep 2008 05:16:44 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.2/8.14.1/Submit) id m8E5GiXZ066529; Sun, 14 Sep 2008 05:16:44 GMT (envelope-from nobody) Message-Id: <200809140516.m8E5GiXZ066529@www.freebsd.org> Date: Sun, 14 Sep 2008 05:16:44 GMT From: Andrei Lavreniyuk To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: misc/127370: sh -c 'exit -1' fails with "Illegal number: -1", instead of exiting with a code of 255 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: Sun, 14 Sep 2008 05:20:02 -0000 >Number: 127370 >Category: misc >Synopsis: sh -c 'exit -1' fails with "Illegal number: -1", instead of exiting with a code of 255 >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Sun Sep 14 05:20:01 UTC 2008 >Closed-Date: >Last-Modified: >Originator: Andrei Lavreniyuk >Release: 7.1-PRERELEASE >Organization: Technica-03, Inc. >Environment: FreeBSD datacenter.technica-03.local 7.1-PRERELEASE FreeBSD 7.1-PRERELEASE #0: Fri Sep 12 21:28:43 UTC 2008 root@datacenter.technica-03.local:/usr/obj/usr/src/sys/SMP-DATACENTER i386 >Description: (c) Garrett Cooper (misc_124748) ---------------------------------------- Bourne shell (/bin/sh), as it stands, doesn't parse numbers properly, even though bash does: [gcooper@optimus /devel/ncvs/src/bin/sh]$ sh -c 'exit -1' exit: Illegal number: -1 [gcooper@optimus /devel/ncvs/src/bin/sh]$ sh -c 'exit +1' exit: Illegal number: +1 These are valid numbers though, which are merely red herrings; the patch attached does fix this behavior though, according to the following gamut of tests: [gcooper@optimus /devel/ncvs/src/bin/sh]$ cat number_test.sh #!/bin/sh for shell in sh $@; do for i in -1 1 +1; do echo "$shell -c \"exit $i\"" $shell -c "exit $i"; echo $? done; done # End of test [gcooper@optimus /devel/ncvs/src/bin/sh]$ ./number_test.sh bash ./sh sh -c "exit -1" exit: Illegal number: -1 2 sh -c "exit 1" 1 sh -c "exit +1" exit: Illegal number: +1 2 bash -c "exit -1" 255 bash -c "exit 1" 1 bash -c "exit +1" 1 ./sh -c "exit -1" 255 ./sh -c "exit 1" 1 ./sh -c "exit +1" 1 >How-To-Repeat: (c) Garrett Cooper (misc_124748) ---------------------- /bin/sh -c "exit -1" /bin/sh -c "exit +1" >Fix: (c) Garrett Cooper (misc_124748) ----------------- Patch attached (also fixes histcmd.c and trap.c -- strange thing is that I can't seem to test that builtin cmd), and maintains proper compatibility with builtin jobs: [gcooper@optimus /devel/ncvs/src/bin/sh]$ sh -c 'builtin histcmd' histcmd: not found Patch attached with submission follows: Index: exec.c =================================================================== RCS file: /home/ncvs/src/bin/sh/exec.c,v retrieving revision 1.31 diff -r1.31 exec.c 40a41 > #include 361c362 < if (prefix("builtin", pathopt)) { --- > if (strncmp(BUILTIN, pathopt, strlen(BUILTIN)) == 0) { Index: histedit.c =================================================================== RCS file: /home/ncvs/src/bin/sh/histedit.c,v retrieving revision 1.29 diff -r1.29 histedit.c 425,426d424 < if (*s == '-') < s++; Index: jobs.c =================================================================== RCS file: /home/ncvs/src/bin/sh/jobs.c,v retrieving revision 1.72 diff -r1.72 jobs.c 534c534 < return (jp); --- > return jp; 540c540,544 < jobno = number(name + 1); --- > /* > * Don't use number() here. This breaks existing > * jobs(1) compatibility. > */ > jobno = atoi(name + 1); 560c564 < if (found) --- > if (found != NULL) 571,572c575,576 < && prefix(name + 1, jp->ps[0].cmd)) { < if (found) --- > && strncmp(name+1, jp->ps[0].cmd, strlen(name+1))) { > if (found != NULL) 577,578c581,582 < if (found) < return found; --- > if (found != NULL) > return (found); 581c585,586 < pid = (pid_t)number(name); --- > /* no need to run is_number() again with number() */ > pid = (pid_t)atoi(name); Index: main.h =================================================================== RCS file: /home/ncvs/src/bin/sh/main.h,v retrieving revision 1.8 diff -r1.8 main.h 38a39,40 > #define BUILTIN "builtin" > Index: mystring.c =================================================================== RCS file: /home/ncvs/src/bin/sh/mystring.c,v retrieving revision 1.13 diff -r1.13 mystring.c 56a57,59 > #if DEBUG > #include > #endif 123a127,135 > /* Is this the first index? */ > int iter = 0; > > #if DEBUG > #define PRINT_DEBUG fprintf(stderr, "%d: %c\n", iter, *p) > #else > #define PRINT_DEBUG > #endif > 125c137,154 < if (! is_digit(*p)) --- > /* > * Account for signs in front of numbers. > */ > > /* > * XXX: does POSIX bourne shell allow for '+' prefixed > * numbers? > */ > > /* > * The string defined by *p isn't a number, unless: > * 1. It's a digit. > * 2. The 0'th index is either a + or -. > */ > if (!(is_digit(*p) || > (iter == 0 && (*p == '-' || *p == '+'))) > ) { > PRINT_DEBUG; 126a156,163 > } > > PRINT_DEBUG; > > #undef PRINT_DEBUG > > iter++; > ------------------------------ (c) Garrett Cooper (misc_124748) >Release-Note: >Audit-Trail: >Unformatted: