From 593c9d498be2510d286349134537e3bf89401c4a Mon Sep 17 00:00:00 2001 From: Kewde Date: Tue, 9 Mar 2021 22:54:46 +0000 Subject: [PATCH] bug: fix segfault of invalid toString() object (#1450) * bug: verify toString() returns valid data * test: faulty toString test --- src/statement.cc | 8 +++++++- test/other_objects.test.js | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/statement.cc b/src/statement.cc index 23842e1dc..9f6a9cd1e 100644 --- a/src/statement.cc +++ b/src/statement.cc @@ -205,7 +205,13 @@ template 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 { diff --git a/test/other_objects.test.js b/test/other_objects.test.js index 155951791..718598768 100644 --- a/test/other_objects.test.js +++ b/test/other_objects.test.js @@ -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(); + }); + }); + });