From d6ec8c114e882a8f9068b9bc14e07718956edec5 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sun, 22 Feb 2015 10:22:21 -0800 Subject: [PATCH] Don't crash if script path has no path separator. Fix #181. --- src/main.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index db80902e..4ff4f4e8 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ // This is the source file for the standalone command line interpreter. It is // not needed if you are embedding Wren in an application. -char* rootDirectory = NULL; +char* rootDirectory = ""; static void failIf(bool condition, int exitCode, const char* format, ...) { @@ -107,9 +107,12 @@ static int runFile(WrenVM* vm, const char* path) // Use the directory where the file is as the root to resolve imports // relative to. char* lastSlash = strrchr(path, '/'); - rootDirectory = (char*)malloc(lastSlash - path + 2); - memcpy(rootDirectory, path, lastSlash - path + 1); - rootDirectory[lastSlash - path + 1] = '\0'; + if (lastSlash != NULL) + { + rootDirectory = (char*)malloc(lastSlash - path + 2); + memcpy(rootDirectory, path, lastSlash - path + 1); + rootDirectory[lastSlash - path + 1] = '\0'; + } char* source = readFile(path);