dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Greenberg <Michael.Greenberg@pomona.edu>
To: "dash@vger.kernel.org" <dash@vger.kernel.org>
Cc: Austin John Blatt <ajb22014@MyMail.pomona.edu>
Subject: [PATCH] unset/null variables should expand to 0 during arithmetic expansion
Date: Thu, 19 Jan 2017 21:06:51 +0000	[thread overview]
Message-ID: <A7A04C3C-65D2-404B-B65F-0053A4D2E78C@pomona.edu> (raw)

Hi,

It seems like dash has a subtly incorrect arithmetic expansion for variables that are unset. For example, consider the following program:

unset x ; echo $((x+=2))

Running bash on this program echoes the number 2 to standard out and sets x to 2. Running dash (git HEAD/release 0.5.9.1) yields an error:

src/dash: 1: Illegal number: 

Now, bash and dash disagreeing isn’t such a big deal. But it seems like the standard indicates bash’s defaulting is correct for unset variables. According to <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04>, “the arithmetic expression shall be processed according to the rules given in Arithmetic Precision and Operations”, which says “all variables shall be initialized to zero if they are not otherwise assigned by the input to the application” <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_01_02_01>.

The standard is a little bit unclear about what to do for variables that are set to null (or non-numeric values). I’d interpret the standard as defaulting for unset variables, having null and other non-numeric strings cause some kind of error. bash, as usual, goes well beyond what the standard indicates: any non-numerical value is defaulted to 0. So:

x="" ; echo $((x+=2))

x=“yo” ; echo $((x+=2))

both yield 2! If you ask me, that’s a bridge too far—and asking for bugs.

I should add that my student (Austin Blatt, CC-ed) noticed this issue; I wrote the attached, single-line fix.

Cheers,
Michael

Signed-off-by: Michael Greenberg <michael.greenberg@pomona.edu>
—
diff --git a/src/var.c b/src/var.c
index cc6f7f2..e34f9cf 100644
--- a/src/var.c
+++ b/src/var.c
@@ -353,7 +353,7 @@ lookupvar(const char *name)
 
 intmax_t lookupvarint(const char *name)
 {
-	return atomax(lookupvar(name) ?: nullstr, 0);
+	return atomax(lookupvar(name) ?: "0", 0);
 }
 
 

 



             reply	other threads:[~2017-01-19 21:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-19 21:06 Michael Greenberg [this message]
2017-01-30  0:13 ` [PATCH] unset/null variables should expand to 0 during arithmetic expansion Martijn Dekker
2017-02-01 18:28   ` Michael Greenberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A7A04C3C-65D2-404B-B65F-0053A4D2E78C@pomona.edu \
    --to=michael.greenberg@pomona.edu \
    --cc=ajb22014@MyMail.pomona.edu \
    --cc=dash@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).