mirror of
git://git.suckless.org/sbase
synced 2025-01-03 05:22:16 +00:00
expr: tonum: handle case where result was previously calculated
As pointed out in a mail to dev expr was segfaulting when multiple
math operations were specified on the command line: eg. 'expr 3 \*
2 + 1'. This happens because the tonum(), introduced in e50d533
,
assumed that v->str was always non null. parse() guarantees this
for user input but this is not the case when doop() is called with
the result of a previous calculation. However in that case we know
that v->num is already valid so we can simply return.
This commit is contained in:
parent
6b9da17eb4
commit
d335c366f7
8
expr.c
8
expr.c
@ -21,7 +21,13 @@ static void
|
|||||||
tonum(struct val *v)
|
tonum(struct val *v)
|
||||||
{
|
{
|
||||||
const char *errstr;
|
const char *errstr;
|
||||||
long long d = strtonum(v->str, LLONG_MIN, LLONG_MAX, &errstr);
|
long long d;
|
||||||
|
|
||||||
|
/* check if val is the result of an earlier calculation */
|
||||||
|
if (!v->str)
|
||||||
|
return;
|
||||||
|
|
||||||
|
d = strtonum(v->str, LLONG_MIN, LLONG_MAX, &errstr);
|
||||||
if (errstr)
|
if (errstr)
|
||||||
enprintf(2, "error: expected integer, got %s\n", v->str);
|
enprintf(2, "error: expected integer, got %s\n", v->str);
|
||||||
v->num = d;
|
v->num = d;
|
||||||
|
Loading…
Reference in New Issue
Block a user