From patchwork Mon Mar 5 00:32:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Dekker X-Patchwork-Id: 10257923 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DA161602BD for ; Mon, 5 Mar 2018 00:32:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BBC5528809 for ; Mon, 5 Mar 2018 00:32:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE73728858; Mon, 5 Mar 2018 00:32:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E83A28809 for ; Mon, 5 Mar 2018 00:32:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932346AbeCEAcM (ORCPT ); Sun, 4 Mar 2018 19:32:12 -0500 Received: from kahlil.inlv.org ([37.59.109.123]:56336 "EHLO kahlil.inlv.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932345AbeCEAcL (ORCPT ); Sun, 4 Mar 2018 19:32:11 -0500 Received: from [192.168.1.81] (host86-129-6-191.range86-129.btcentralplus.com [86.129.6.191]) (authenticated bits=0) by kahlil.inlv.org (8.14.9/8.14.4) with ESMTP id w250W9VR013006 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO) for ; Mon, 5 Mar 2018 01:32:10 +0100 To: dash@vger.kernel.org From: Martijn Dekker Subject: [PATCH] fix "Illegal number" on FreeBSD & macOS for x=; echo $((x)) Message-ID: <62c16598-2d44-f4c9-991d-9880a8839786@inlv.org> Date: Mon, 5 Mar 2018 00:32:09 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Language: en-GB Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP dash compiled on Mac OS X (macOS) and FreeBSD manifests the following bug: $ dash -c 'x=; echo $((x))' dash: 1: Illegal number: This error is printed instead of the expected default "0" that should be substituted for an empty variable in an arithmetic expression. The bug does not occur on Linux, NetBSD, OpenBSD or Solaris. I traced the problem to the fact that strtoimax(3) on macOS and FreeBSD returns EINVAL in errno for an empty string -- unlike those other systems, which return 0 in errno for an empty string. POSIX says of strtoimax(3): http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoimax.html | These functions may fail if: | | [EINVAL] | No conversion could be performed. It seems reasonable to consider that "no conversion could be performed" if the string to convert is empty. Returning EINVAL if no conversion could be performed is optional ("may fail"). So it seems to me that both the FreeBSD/macOS behaviour and that of the other systems is POSIX compliant. The following patch should eliminate the bug on FreeBSD, macOS and any other POSIX system which may act the same, without affecting behaviour on other systems. - M. --- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/src/mystring.c b/src/mystring.c index 0106bd2..c7df41f 100644 --- a/src/mystring.c +++ b/src/mystring.c @@ -125,7 +125,7 @@ intmax_t atomax(const char *s, int base) errno = 0; r = strtoimax(s, &p, base); - if (errno != 0) + if (errno != 0 && !(errno == EINVAL && p == s)) badnum(s); /*