[quickjs-devel] Re: segfault when accidently specifying directory path in import statement

  • From: Mario Gliewe <mag@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Wed, 21 Aug 2019 14:49:10 +0200

patch attached, hope the stat() stuff will work on WIN32 as well...

lg maG

--snip--

diff --git a/quickjs-libc.c b/quickjs-libc.c
index a4a4c1c..5b76620 100644
--- a/quickjs-libc.c
+++ b/quickjs-libc.c
@@ -34,6 +34,11 @@
 #include <sys/time.h>
 #include <time.h>
 #include <signal.h>
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #if defined(_WIN32)
 #include <windows.h>
 #include <conio.h>
@@ -300,13 +305,18 @@ uint8_t *js_load_file(JSContext *ctx, size_t
*pbuf_len, const char *filename)
     FILE *f;
     uint8_t *buf;
     size_t buf_len;
+    struct stat statbuf;
+
+    if (stat(filename, &statbuf) < 0)
+        return NULL;
+    if ( (statbuf.st_mode & S_IFMT) != __S_IFREG)
+        return NULL;
+
+    buf_len = statbuf.st_size;
 
     f = fopen(filename, "rb");
     if (!f)
         return NULL;
-    fseek(f, 0, SEEK_END);
-    buf_len = ftell(f);
-    fseek(f, 0, SEEK_SET);
     if (ctx)
         buf = js_malloc(ctx, buf_len + 1);
     else



Other related posts: