Browse Source

Merge branch 'master' of https://github.com/marcobambini/gravity

Marco Bambini 6 years ago
parent
commit
79f7a2fa3b
4 changed files with 25 additions and 9 deletions
  1. 7 3
      src/cli/gravity.c
  2. 12 3
      src/runtime/gravity_vm.c
  3. 4 2
      src/utils/gravity_utils.c
  4. 2 1
      src/utils/gravity_utils.h

+ 7 - 3
src/cli/gravity.c

@@ -170,12 +170,16 @@ static const char *unittest_read (const char *path, size_t *size, uint32_t *file
 static void unittest_scan (const char *folder_path, unittest_data *data) {
     DIRREF dir = directory_init(folder_path);
     if (!dir) return;
-    
+#ifdef WIN32
+    char outbuffer[MAX_PATH];
+#else
+    char * outbuffer = NULL;
+#endif
     const char *target_file;
-    while ((target_file = directory_read(dir))) {
+    while ((target_file = directory_read(dir, outbuffer))) {
         
         #ifdef WIN32
-        const char winbuffer[MAX_PATH];
+        char winbuffer[MAX_PATH];
         WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)target_file, -1, winbuffer, sizeof(winbuffer), NULL, NULL );
         target_file = (const char *)winbuffer;
         #endif

+ 12 - 3
src/runtime/gravity_vm.c

@@ -901,10 +901,19 @@ static bool gravity_vm_exec (gravity_vm *vm) {
 
                 // check fast math operation first  (only in case of int and float)
                 // a special check macro is added in order to check for divide by zero cases
-                #pragma clang diagnostic push
-                #pragma clang diagnostic ignored "-Wdivision-by-zero"
+                #if defined(__clang__)
+                    #pragma clang diagnostic push
+                    #pragma clang diagnostic ignored "-Wdivision-by-zero"
+                #elif defined(__GNUC__)
+                    #pragma GCC diagnostic push
+                    #pragma GCC diagnostic ignored "-Wdiv-by-zero"
+                #endif
                 CHECK_FAST_BINARY_MATH(r1, r2, r3, v2, v3, /, CHECK_ZERO(v3));
-                 #pragma clang diagnostic pop
+                #if defined(__clang__)
+                    #pragma clang diagnostic pop
+                #elif defined(__GNUC__)
+                    #pragma GCC diagnostic pop
+                #endif
 
                 // prepare function call for binary operation
                 PREPARE_FUNC_CALL2(closure, v2, v3, GRAVITY_DIV_INDEX, rwin);

+ 4 - 2
src/utils/gravity_utils.c

@@ -261,7 +261,7 @@ DIRREF directory_init (const char *dirpath) {
 	#endif
 }
 
-const char *directory_read (DIRREF ref) {
+const char *directory_read (DIRREF ref, char *out) {
 	if (ref == NULL) return NULL;
 	
 	while (1) {
@@ -275,7 +275,9 @@ const char *directory_read (DIRREF ref) {
 		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
 		if (findData.cFileName[0] == '\0') continue;
 		if (findData.cFileName[0] == '.') continue;
-		return (const char*)findData.cFileName;
+		// cFileName from WIN32_FIND_DATAA is a fixed size array, and findData is local
+		// This line of code is under the assumption that `out` is at least MAX_PATH in size!
+		return !out ? NULL : memcpy(out, findData.cFileName, sizeof(findData.cFileName));
 		#else
 		struct dirent *d;
 		if ((d = readdir(ref)) == NULL) {

+ 2 - 1
src/utils/gravity_utils.h

@@ -40,7 +40,8 @@ bool        file_write (const char *path, const char *buffer, size_t len);
 // DIRECTORY
 bool        is_directory (const char *path);
 DIRREF      directory_init (const char *path);
-const char  *directory_read (DIRREF ref);
+// On Windows, you are expected to provied an output buffer of at least MAX_PATH in length
+const char  *directory_read (DIRREF ref, char *out);
 
 // STRING
 int         string_nocasencmp (const char *s1, const char *s2, size_t n);