Description: fix segfault of invalid toString() object
Author: Kewde <kewde@particl.io>
Origin: upstream, https://github.com/TryGhost/node-sqlite3/commit/593c9d49
Bug: https://github.com/advisories/GHSA-9qrh-qjmc-5w2p
Forwarded: not-needed
Reviewed-By: Yadd <yadd@debian.org>
Last-Update: 2022-05-01

--- a/src/statement.cc
+++ b/src/statement.cc
@@ -210,7 +210,13 @@
         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 {
--- a/test/other_objects.test.js
+++ b/test/other_objects.test.js
@@ -86,4 +86,13 @@
             });
         });
     });
+
+    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();
+        });
+    });
+
 });
