瀏覽代碼

fix: 修复了struct的部分bug

makeStructValue使用MEM_CPY做判空检查
SongZihuan 4 年之前
父節點
當前提交
09d86c353f
共有 3 個文件被更改,包括 8 次插入6 次删除
  1. 4 1
      vmcore/include/macro.h
  2. 1 1
      vmcore/include/mem.h
  3. 3 4
      vmcore/src/value.c

+ 4 - 1
vmcore/include/macro.h

@@ -21,7 +21,6 @@
 #include <locale.h>
 #include <getopt.h>
 #include <unistd.h>
-#include "direct.h"
 #include <dlfcn.h>
 #include <ffi.h>
 
@@ -32,6 +31,10 @@
 #define NUL ((char)0)
 #define WNUL ((wchar_t)0)
 
+#ifndef __linux
+#include <direct.h>  // linux系统不需要包含该头文件
+#endif
+
 #ifdef NDEBUG
 #define err_asert(e) ((void)0)
 #else

+ 1 - 1
vmcore/include/mem.h

@@ -33,6 +33,6 @@ wchar_t *memStrToWcs(char *str, bool free_old);
 
 #define MACRO_CALLOC(var, n, size) ((((var) = (typeof(var))calloc(n, size)) == NULL) ? (memError()) : (var))
 
-#define MEM_CPY(to, from, size) (((to) = memCalloc(1, size)), memcpy(to, from, size))
+#define MEM_CPY(to, from, size) (((size) != 0) ? ((to) = memCalloc(1, size)), memcpy(to, from, size) : NULL)
 
 #endif // VIRTUALMATH_MEM_H

+ 3 - 4
vmcore/src/value.c

@@ -58,11 +58,10 @@ Value *makeStructValue(void *data, vint len, fline line, char *file, FUNC_NT) {
     } else
         setResultOperation(result, structCore(belong, inter->data.base_obj[B_STRUCT], inter));
     tmp = result->value->value;
-    if (len != 0 || data != NULL)
+    if (data != NULL) {
         tmp->data.struct_.data = MEM_CPY(tmp->data.struct_.data, data, len * sizeof(int8_t));
-    else
-        tmp->data.struct_.data = NULL;
-    tmp->data.struct_.len = len;
+        tmp->data.struct_.len = len;
+    }
     return tmp;
 }