浏览代码

Use temporary buffer for realloc return value

tbeu 10 年之前
父节点
当前提交
fb4b8c8ffb
共有 2 个文件被更改,包括 8 次插入4 次删除
  1. 4 1
      src/utarray.h
  2. 4 3
      src/utstring.h

+ 4 - 1
src/utarray.h

@@ -86,8 +86,11 @@ typedef struct {
 
 #define utarray_reserve(a,by) do {                                            \
   if (((a)->i+(by)) > ((a)->n)) {                                             \
+    char *tmp;                                                                \
     while(((a)->i+(by)) > ((a)->n)) { (a)->n = ((a)->n ? (2*(a)->n) : 8); }   \
-    if ( ((a)->d=(char*)realloc((a)->d, (a)->n*(a)->icd.sz)) == NULL) oom();  \
+    tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz);                           \
+    if (tmp == NULL) oom();                                                   \
+    (a)->d=tmp                                                                \
   }                                                                           \
 } while(0)
 

+ 4 - 3
src/utstring.h

@@ -49,9 +49,10 @@ typedef struct {
 #define utstring_reserve(s,amt)                            \
 do {                                                       \
   if (((s)->n - (s)->i) < (size_t)(amt)) {                 \
-     (s)->d = (char*)realloc((s)->d, (s)->n + (amt));      \
-     if ((s)->d == NULL) oom();                            \
-     (s)->n += (amt);                                      \
+    char *tmp = (char*)realloc((s)->d, (s)->n + (amt));    \
+    if (tmp == NULL) oom();                                \
+    (s)->d = tmp;                                          \
+    (s)->n += (amt);                                       \
   }                                                        \
 } while(0)