Skip to content

Commit

Permalink
expr: Fix refcount issue converting invalid boolean
Browse files Browse the repository at this point in the history
Reported-by: Ryan Whitworth <me@ryanwhitworth.com>
Signed-off-by: Steve Bennett <steveb@workware.net.au>
  • Loading branch information
msteveb committed Aug 7, 2017
1 parent 028dd5d commit bbe51b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
18 changes: 12 additions & 6 deletions jim.c
Expand Up @@ -8240,17 +8240,23 @@ static int ExprBool(Jim_Interp *interp, Jim_Obj *obj)
long l;
double d;
int b;
int ret = -1;

/* In case the object is interp->result with refcount 1*/
Jim_IncrRefCount(obj);

if (Jim_GetLong(interp, obj, &l) == JIM_OK) {
return l != 0;
ret = (l != 0);
}
if (Jim_GetDouble(interp, obj, &d) == JIM_OK) {
return d != 0;
else if (Jim_GetDouble(interp, obj, &d) == JIM_OK) {
ret = (d != 0);
}
if (Jim_GetBoolean(interp, obj, &b) == JIM_OK) {
return b != 0;
else if (Jim_GetBoolean(interp, obj, &b) == JIM_OK) {
ret = (b != 0);
}
return -1;

Jim_DecrRefCount(interp, obj);
return ret;
}

static int JimExprOpAnd(Jim_Interp *interp, struct JimExprNode *node)
Expand Down
5 changes: 5 additions & 0 deletions regtest.tcl
Expand Up @@ -345,6 +345,11 @@ puts "TEST 49 PASSED"
catch {expr {>>-$x}}
puts "TEST 50 PASSED"

# REGTEST 51
# expr convert invalid value to boolean
catch {expr {2 && "abc$"}}
puts "TEST 51 PASSED"

# TAKE THE FOLLOWING puts AS LAST LINE

puts "--- ALL TESTS PASSED ---"

0 comments on commit bbe51b6

Please sign in to comment.