Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Jeremy Townshend Newsgroups: gnu.bash.bug Subject: Arithmetic evaluation of negative numbers with base prefix Date: Fri, 14 Jun 2019 15:19:48 +0100 Lines: 32 Approved: bug-bash@gnu.org Message-ID: References: <20190614141947.GB27242@tower> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1560521997 27283 209.51.188.17 (14 Jun 2019 14:19:57 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org, bash@packages.debian.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=adohMaUNOQcamlbaK47L0Zqawc5u/mr3XlGNDNPCkLY=; b=dyhp5GdCBd+pWJ20bT9qDGMBXBMquC9KuE8NbOLk4cTmMBISO1XI6KLhRITsZ7mPix TeGpxqetgYZdaabziSZVt7UbcivdR18Td38szNf5siFfzc18bGRXoZ1uq2oYWDsufVdF nbi1EEYpwCIouoN+U3QJP4v+t5WGPbz0NxPhIKYS56bMPZoQVDzbDiQX6j3OTr0KPz0W V2yhun3Ce77S1p/UnqtPxxnsvT46NONyga/4DwBRmAjzG3kvBRuG/XcR94y4GxozN4Bv T8gDkVmVzy064MmF+GSzuZcvT69zms6xcgAIBoGD/9BLv22RPr+tzKE9YAbhV+wwBkGQ 57AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=adohMaUNOQcamlbaK47L0Zqawc5u/mr3XlGNDNPCkLY=; b=DjGTWOEW/GGZvKSNPKFY5J+Lw/wfl+71dgOn7ozOqExOL7MQ1gODfP/hshJzb53q9m Ju//0i3CDbYQY2u1bDPupIHkzhGulc13wchYxrnwIL+L6ZaHeM5mdfewUOdcU5kw9AjF Y3G8Nr6ndDg7VaSD9/aDp47xrqHKtTbJOsDu1V7OYd8nmJD1mxI5VfjaumKX49Xt3eNi plND2C8h/YTalwlJxOUeuNhjKMtAogFsSpbhlWtj9hRS4PB6qoWkx7gfIeA2PqTSwsPz MGPxxlvYeVSU2cRMGrjnkWV2XKWsAs7Np0EI4OgV/ys4qQtNH10dddmYMtkYgfMZFK2c zQ3w== X-Gm-Message-State: APjAAAWStZX6KDoIjuM0HWgCqiXyCYoq4N0sdgzTJxxx6ujxBCdHxyDn ZARRrygmdKTezEZIYYVfmqhSiKbt X-Google-Smtp-Source: APXvYqxVid2OOddRBQ5UP09GZqPoxz7Nm+HUQShZAKTLAghRqeaMPfrTSTvaBW3t/TJAmFbTi+nASA== X-Received: by 2002:a05:600c:2253:: with SMTP id a19mr8347347wmm.156.1560521991527; Fri, 14 Jun 2019 07:19:51 -0700 (PDT) Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::331 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20190614141947.GB27242@tower> Xref: csiph.com gnu.bash.bug:15048 Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../. -I.././include -I.././lib -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -fdebug-prefix-map=/build/bash-vEMnMR/bash-4.4.18=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wno-parentheses -Wno-format-security uname output: Linux tower 4.15.0-48-generic #51-Ubuntu SMP Wed Apr 3 08:28:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-pc-linux-gnu Bash Version: 4.4 Patch Level: 19 Release Status: release Description: Unexpected and undocumented behaviour for arithmetic evaluation of negative numbers when prefixed with the optional "base#" (e.g. 10#${i}). The base prefix may be needed if the variable has a decimal integer value but might be zero-padded, otherwise it is at risk of being misinterpreted as an octal. Where the variable holds a negative value, results are as you would expect (e.g. i=-1; echo $((10#${i})), returns -1) until you subtract (or unary minus) the variable. This unexpected behaviour occurs even when numbers are used directly (as in the first part of the Repeat-By section to simplify) but in real world examples the number would be hidden in a variable requiring the optional "base#" prefix to ensure correct interpretation of its value. Repeat-By: echo $((10#-1)) # -1 as expected echo $((0-10#1)) # -1 as expected echo $((0+10#-1)) # -1 as expected echo $((0-10#-1)) # -1 UNEXPECTED. Would expect 1. echo $((0--1)) # 1 as expected # Real world example: i=001 echo $((3-10#${i})) # 2 as expected i=$((10#${i}-2)) # i's value decremented by 2 to -1 echo $((3-10#${i})) # 2 UNEXPECTED. Would expect 4. echo $((3+10#${i})) # 2 as expected # Certainly wouldn't expect the last two expressions to have the same # result.