Skip to content

Commit

Permalink
bug: fix segfault of invalid toString() object (#1450)
Browse files Browse the repository at this point in the history
* bug: verify toString() returns valid data
* test: faulty toString test
  • Loading branch information
kewde committed Mar 9, 2021
1 parent 3fb3715 commit 593c9d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ template <class T> Values::Field*
return new Values::Float(pos, source.ToNumber().DoubleValue());
}
else if (source.IsObject()) {
std::string val = source.ToString().Utf8Value();
Napi::String napiVal = source.ToString();
// Check whether toString returned a value that is not undefined.
if(napiVal.Type() == 0) {
return NULL;
}

std::string val = napiVal.Utf8Value();
return new Values::Text(pos, val.length(), val.c_str());
}
else {
Expand Down
9 changes: 9 additions & 0 deletions test/other_objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ describe('data types', function() {
});
});
});

it('should ignore faulty toString', function(done) {
const faulty = { toString: 23 };
db.run("INSERT INTO txt_table VALUES(?)", faulty, function (err) {
assert.notEqual(err, undefined);
done();
});
});

});

0 comments on commit 593c9d4

Please sign in to comment.