소스 검색

refactor & feat: 使用safeFree函数取代free函数

SongZihuan 3 년 전
부모
커밋
69dd25554b
14개의 변경된 파일32개의 추가작업 그리고 32개의 파일을 삭제
  1. 1 1
      src/core/code.cpp
  2. 4 4
      src/core/lexical.cpp
  3. 1 1
      src/core/reader.cpp
  4. 1 1
      src/tool/byte.cpp
  5. 1 1
      src/tool/dlc.cpp
  6. 4 4
      src/tool/file.cpp
  7. 4 4
      src/tool/log.cpp
  8. 1 1
      src/tool/md5.cpp
  9. 4 4
      src/tool/stdio_.cpp
  10. 4 4
      src/tool/string.cpp
  11. 3 3
      src/tool/time.cpp
  12. 1 1
      test/src/core-reader.cpp
  13. 1 1
      test/src/tool-md5.cpp
  14. 2 2
      test/src/tool-mem.cpp

+ 1 - 1
src/core/code.cpp

@@ -328,7 +328,7 @@ RETURN_FALSE:
 
     Code::ByteCode::~ByteCode(){
         if (type == code_element)
-            free(data.element);
+            aFuntool::safeFree(data.element);
     }
 
     /**

+ 4 - 4
src/core/lexical.cpp

@@ -395,7 +395,7 @@ namespace aFuncore {
                 break;
             } else if (re == DEL_TOKEN) {  // 删除该token, 继续执行
                 char *word = reader.readWord(lexical.last);
-                free(word);
+                aFuntool::safeFree(word);
                 lexical.status = lex_begin;
                 lexical.last = 0;
                 continue;
@@ -430,10 +430,10 @@ namespace aFuncore {
                     }
 
                     text = new_str;
-                    free(word);
-                    free(new_str);
+                    aFuntool::safeFree(word);
+                    aFuntool::safeFree(new_str);
                 } else
-                    free(word);
+                    aFuntool::safeFree(word);
 
                 if (tt == TK_SPACE || tt == TK_COMMENT) {
                     lexical.status = lex_begin;

+ 1 - 1
src/core/reader.cpp

@@ -41,7 +41,7 @@ namespace aFuncore {
         }
 
         if (!aFuntool::isCharUTF8(re)) {
-            free(re);
+            aFuntool::safeFree(re);
             errorLog(aFunCoreLogger, "Is not utf-8");
             return nullptr;
         }

+ 1 - 1
src/tool/byte.cpp

@@ -80,7 +80,7 @@ namespace aFuntool {
         char *tmp = safeCalloc<char>(len + 1);
         size_t ret = fread(tmp, sizeof(char), len, file);
         str = tmp;
-        free(tmp);
+        safeFree(tmp);
         return ret == len;
     }
 

+ 1 - 1
src/tool/dlc.cpp

@@ -68,7 +68,7 @@ namespace aFuntool {
     void DlcHandle::dlcExit(){
         while (dlc != nullptr) {
             auto next = dlc->next_;
-            free(dlc);
+            safeFree(dlc);
             dlc = next;
         }
     }

+ 4 - 4
src/tool/file.cpp

@@ -54,7 +54,7 @@ namespace aFuntool {
         if (convertWideByte(&tmp, path_.c_str(), CP_UTF8) == 0)
             return -1;
         re = _wstat64(tmp, &stat_);
-        free(tmp);  // 如果 path 为nullptr, 则释放最新生成的 wchat_t
+        safeFree(tmp);  // 如果 path 为nullptr, 则释放最新生成的 wchat_t
 #else
         re = stat(path_.c_str(), &stat_);
 #endif
@@ -174,7 +174,7 @@ namespace aFuntool {
             if (!isalnum(*tmp) &&'_' != *tmp)
                 *tmp = '_';
         std::string ret = var;
-        free(var);
+        safeFree(var);
         return ret;
     }
 
@@ -208,7 +208,7 @@ namespace aFuntool {
         if (convertFromWideByte(&path, exepath, CP_UTF8) == 0)
             return "";
         std::string re = getFilePath(path, dep + 1);
-        free(path);
+        safeFree(path);
         return re;
 #else
         ssize_t ret =  readlink("/proc/self/exe", exepath, 217);  // 预留一位给NUL
@@ -290,7 +290,7 @@ namespace aFuntool {
             mode[i] = (wchar_t)mode_[i];  // ascii字符转换
 
         _wfopen_s(&file, path, mode);
-        free(path);
+        safeFree(path);
         return file;
 #else
         return fopen(path_, mode_);

+ 4 - 4
src/tool/log.cpp

@@ -97,7 +97,7 @@ namespace aFuntool {
         char *ti = getTime(nullptr, (char *) "%Y-%m-%d%z");
         snprintf(log_path, 218, "%s-%s.log", path.c_str(), ti);
         snprintf(csv_path, 218, "%s-%s.csv", path.c_str(), ti);
-        free(ti);
+        safeFree(ti);
 
         uintmax_t log_size = getFileSize(log_path);
         uintmax_t csv_size = getFileSize(csv_path);
@@ -308,8 +308,8 @@ namespace aFuntool {
 #define D(i) tmp->i
             data->factor.writeLog(D(level), D(id), D(tid), D(date), D(time), D(file), D(line), D(func), D(info));
 #undef D
-            free(tmp->date);
-            free(tmp->info);
+            safeFree(tmp->date);
+            safeFree(tmp->info);
             delete tmp;
         }
         delete data;
@@ -361,7 +361,7 @@ namespace aFuntool {
         ul.unlock();
         if (asyn_)
             cond_.notify_all();
-        free(ti);
+        safeFree(ti);
         return 0;
     }
 

+ 1 - 1
src/tool/md5.cpp

@@ -80,7 +80,7 @@ namespace aFuntool {
         MD5Update(context, PADDING, pad_len);
         MD5Update(context, bits, 8);
         MD5Encode(digest, context->state, 16);
-        free(context);
+        safeFree(context);
     }
 
     static void MD5Encode(unsigned char *output, const unsigned int *input, unsigned int len){

+ 4 - 4
src/tool/stdio_.cpp

@@ -341,7 +341,7 @@ namespace aFuntool {
         *dest = safeCalloc<char>(dest_len + 1);
         int re = WideCharToMultiByte(to, 0, tmp, -1, *dest, dest_len, nullptr, nullptr);
 
-        free(tmp);
+        safeFree(tmp);
         return re;
     }
 
@@ -375,7 +375,7 @@ namespace aFuntool {
             *dest = safeCalloc<char>(len + 1);
             re = fgets(*dest, len, stdin) != nullptr;
             if (!re)
-                free(*dest);
+                safeFree(*dest);
             return re;
         }
 
@@ -435,7 +435,7 @@ namespace aFuntool {
         int re = EOF;
         if (convertMultiByte(&wstr, str, CP_UTF8, code_page) == 0 || wstr != nullptr) {
             re = fputs(wstr, std);
-            free(wstr);
+            safeFree(wstr);
         }
         return re;
     }
@@ -454,7 +454,7 @@ namespace aFuntool {
         size_t re = vsnprintf(buf, buf_len, format, ap);
         if (fputs_std_(buf, std) == EOF)
             re = 0;
-        free(buf);
+        safeFree(buf);
         return re;
     }
 }

+ 4 - 4
src/tool/string.cpp

@@ -55,12 +55,12 @@ namespace aFuntool {
 
         if (free_first) {
             auto free_ = const_cast<char *>(first);
-            free(free_);
+            safeFree(free_);
         }
 
         if (free_last) {
             auto free_ = const_cast<char *>(second);
-            free(free_);
+            safeFree(free_);
         }
         return new_str;
     }
@@ -75,7 +75,7 @@ namespace aFuntool {
 
         if (free_old) {
             auto free_ = const_cast<char *>(str);
-            free(free_);
+            safeFree(free_);
         }
         return tmp;
     }
@@ -90,7 +90,7 @@ namespace aFuntool {
 
         if (free_old) {
             auto free_ = const_cast<wchar_t *>(wstr);
-            free(free_);
+            safeFree(free_);
         }
         return tmp;
     }

+ 3 - 3
src/tool/time.cpp

@@ -43,7 +43,7 @@ namespace aFuntool {
         if (convertWideByte(&format_, format, CP_UTF8) == 0)
             return nullptr;
         wcsftime(time_str, 100, format_, &lt);
-        free(format_);
+        safeFree(format_);
 
         char *re = nullptr;
         if (convertFromWideByte(&re, time_str, CP_UTF8) == 0)
@@ -72,7 +72,7 @@ namespace aFuntool {
         if (convertWideByte(&format_, format.c_str(), CP_UTF8) == 0)
             return "";
         wcsftime(time_str, 100, format_, &lt);
-        free(format_);
+        safeFree(format_);
 
         char *tmp_ch = nullptr;
         if (convertFromWideByte(&tmp_ch, time_str, CP_UTF8) == 0)
@@ -84,7 +84,7 @@ namespace aFuntool {
         char *tmp_ch = strCopy(time_str);;
 #endif
         std::string ret = tmp_ch;
-        free(tmp_ch);
+        safeFree(tmp_ch);
         return ret;
     }
 }

+ 1 - 1
test/src/core-reader.cpp

@@ -47,7 +47,7 @@ int main() {
             printf("%zu\t\tch = %c\n", i, ch);
         }
         char *new_word = test.readWord(100);  // 提取前面100个值
-        free(new_word);
+        aFuntool::safeFree(new_word);
 
         size_t count = 0;
         do {

+ 1 - 1
test/src/tool-md5.cpp

@@ -10,7 +10,7 @@ int main(int argc, char **argv) {
     char *my_md5 = getFileMd5(file_path);
 
     bool ret = !strcmp(my_md5, md5_answer);
-    free(my_md5);
+    aFuntool::safeFree(my_md5);
 
     if (ret)
         return 0;

+ 2 - 2
test/src/tool-mem.cpp

@@ -3,10 +3,10 @@
 int main() {
     int *p = aFuntool::safeCalloc<int>();
     *p = 10;
-    free(p);
+    aFuntool::safeFree(p);
 
     p = aFuntool::safeCalloc<int>(1);
     *p = 10;
-    free(p);
+    aFuntool::safeFree(p);
     return 0;
 }