[gjs: 1/2] system: Fix missing error check in dumpHeap()



commit 2fb9c04371983df521d1f79f7cd8da18774b0a0a
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Mar 9 21:23:49 2019 -0800

    system: Fix missing error check in dumpHeap()
    
    The fopen() call was missing an error check, causing a crash if the path
    did not exist, for example.
    
    Closes: #134

 installed-tests/js/testSystem.js | 6 ++++++
 modules/system.cpp               | 7 +++++++
 2 files changed, 13 insertions(+)
---
diff --git a/installed-tests/js/testSystem.js b/installed-tests/js/testSystem.js
index 1211de48..89a65b5f 100644
--- a/installed-tests/js/testSystem.js
+++ b/installed-tests/js/testSystem.js
@@ -28,3 +28,9 @@ describe('System.gc()', function () {
         expect(System.gc).not.toThrow();
     });
 });
+
+describe('System.dumpHeap()', function () {
+    it('throws but does not crash when given a nonexistent path', function () {
+        expect(() => System.dumpHeap('/does/not/exist')).toThrow();
+    });
+});
\ No newline at end of file
diff --git a/modules/system.cpp b/modules/system.cpp
index a5edd109..430b0288 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -24,6 +24,8 @@
 
 #include <config.h>
 
+#include <errno.h>
+#include <string.h>
 #include <sys/types.h>
 #include <time.h>
 
@@ -106,6 +108,11 @@ gjs_dump_heap(JSContext *cx,
 
     if (filename) {
         FILE *fp = fopen(filename, "a");
+        if (!fp) {
+            gjs_throw(cx, "Cannot dump heap to %s: %s", filename.get(),
+                      strerror(errno));
+            return false;
+        }
         js::DumpHeap(cx, fp, js::IgnoreNurseryObjects);
         fclose(fp);
     } else {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]