Re: [hackers] [sbase][PATCH] expr: tonum: handle case where result was previously calculated

2024-01-30 Thread Roberto E. Vargas Caballero
Hi,

On Mon, Jan 22, 2024 at 02:18:10PM -0700, Randy Palamar wrote:
> 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.
> ---


Applied, thanks.



[hackers] [sbase] expr: tonum: handle case where result was previously calculated || Randy Palamar

2024-01-30 Thread git
commit d335c366f7a2ef74ab8da19b721707110ec821c8
Author: Randy Palamar 
AuthorDate: Mon Jan 22 14:18:10 2024 -0700
Commit: Roberto E. Vargas Caballero 
CommitDate: Wed Jan 31 05:00:51 2024 +0100

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.

diff --git a/expr.c b/expr.c
index 3afb94b..044c6c1 100644
--- a/expr.c
+++ b/expr.c
@@ -21,7 +21,13 @@ static void
 tonum(struct val *v)
 {
const char *errstr;
-   long long d = strtonum(v->str, LLONG_MIN, LLONG_MAX, );
+   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, );
if (errstr)
enprintf(2, "error: expected integer, got %s\n", v->str);
v->num = d;