add MEM_recallocN(), so bytes are ensured to be zero'd when growing.
authorCampbell Barton <ideasman42@gmail.com>
Sat, 11 Aug 2012 12:16:09 +0000 (12:16 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 11 Aug 2012 12:16:09 +0000 (12:16 +0000)
intern/guardedalloc/MEM_guardedalloc.h
intern/guardedalloc/intern/mallocn.c
source/blender/avi/intern/avi.c

index ae8115a337e1d07cdf8adb6b36f6840b5c61a6ae..149483e719f82e00aa2e34fefdee67d31f51c6ff 100644 (file)
@@ -110,6 +110,16 @@ extern "C" {
 #endif
        ;
 
+       /**
+        * A variant of realloc which zeros new bytes
+        */
+       void *MEM_recallocN(void *vmemh, size_t len)
+#if MEM_GNU_ATTRIBUTES
+       __attribute__((warn_unused_result))
+       __attribute__((alloc_size(2)))
+#endif
+       ;
+
        /**
         * Allocate a block of memory of size len, with tag name str. The
         * memory is cleared. The name must be static, because only a
index c4902e6aa5a6f8a6e197be9cc96d399eff0ea0e3..df6f4d59c7f5de08262cb261fb8601c23f848793 100644 (file)
@@ -302,10 +302,45 @@ void *MEM_reallocN(void *vmemh, size_t len)
 
                newp = MEM_mallocN(len, memh->name);
                if (newp) {
-                       if (len < memh->len)
+                       if (len < memh->len) {
+                               /* shrink */
                                memcpy(newp, vmemh, len);
-                       else
+                       }
+                       else {
+                               /* grow (or remain same size) */
+                               memcpy(newp, vmemh, memh->len);
+                       }
+               }
+
+               MEM_freeN(vmemh);
+       }
+
+       return newp;
+}
+
+void *MEM_recallocN(void *vmemh, size_t len)
+{
+       void *newp = NULL;
+
+       if (vmemh) {
+               MemHead *memh = vmemh;
+               memh--;
+
+               newp = MEM_mallocN(len, memh->name);
+               if (newp) {
+                       if (len < memh->len) {
+                               /* shrink */
+                               memcpy(newp, vmemh, len);
+                       }
+                       else {
                                memcpy(newp, vmemh, memh->len);
+
+                               if (len > memh->len) {
+                                       /* grow */
+                                       /* zero new bytes */
+                                       memset(((char *)newp) + memh->len, 0, len - memh->len);
+                               }
+                       }
                }
 
                MEM_freeN(vmemh);
index 9d4704cf0f263db776e5e6b173cef55291f92e25..39424057faf6920e8bf9eb36402a9596ce6db73b 100644 (file)
@@ -968,9 +968,7 @@ AviError AVI_write_frame(AviMovie *movie, int frame_num, ...)
                const size_t entry_size = (movie->header->Streams + 1) * sizeof(AviIndexEntry);
 
                if (movie->entries != NULL) {
-                       temp = (AviIndexEntry *)MEM_reallocN(movie->entries, (frame_num + 1) * entry_size);
-                       /* clear new bytes */
-                       memset(&temp[movie->index_entries + 1], 0, (frame_num - movie->index_entries) * entry_size);
+                       temp = (AviIndexEntry *)MEM_recallocN(movie->entries, (frame_num + 1) * entry_size);
                }
                else {
                        temp = (AviIndexEntry *) MEM_callocN((frame_num + 1) * entry_size, "newidxentry");