Browse code

mem: Add av_realloc_array and av_reallocp_array

These help avoiding overflows and simplify error handling.

Signed-off-by: Martin Storsjö <martin@martin.st>

Martin Storsjö authored on 2013/06/03 18:31:46
Showing 4 changed files
... ...
@@ -13,6 +13,9 @@ libavutil:     2012-10-22
13 13
 
14 14
 API changes, most recent first:
15 15
 
16
+2013-06-xx - xxxxxxx - lavu 52.13.0 - mem.h
17
+  Add av_realloc_array and av_reallocp_array
18
+
16 19
 2013-05-xx - xxxxxxx - lavfi 3.10.0 - avfilter.h
17 20
   Add support for slice multithreading to lavfi. Filters supporting threading
18 21
   are marked with AVFILTER_FLAG_SLICE_THREADS.
... ...
@@ -136,6 +136,32 @@ void *av_realloc(void *ptr, size_t size)
136 136
 #endif
137 137
 }
138 138
 
139
+void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
140
+{
141
+    if (size <= 0 || nmemb >= INT_MAX / size)
142
+        return NULL;
143
+    return av_realloc(ptr, nmemb * size);
144
+}
145
+
146
+int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
147
+{
148
+    void **ptrptr = ptr;
149
+    void *ret;
150
+    if (size <= 0 || nmemb >= INT_MAX / size)
151
+        return AVERROR(ENOMEM);
152
+    if (nmemb <= 0) {
153
+        av_freep(ptr);
154
+        return 0;
155
+    }
156
+    ret = av_realloc(*ptrptr, nmemb * size);
157
+    if (!ret) {
158
+        av_freep(ptr);
159
+        return AVERROR(ENOMEM);
160
+    }
161
+    *ptrptr = ret;
162
+    return 0;
163
+}
164
+
139 165
 void av_free(void *ptr)
140 166
 {
141 167
 #if CONFIG_MEMALIGN_HACK
... ...
@@ -112,6 +112,32 @@ av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t siz
112 112
 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
113 113
 
114 114
 /**
115
+ * Allocate or reallocate an array.
116
+ * If ptr is NULL and nmemb > 0, allocate a new block. If
117
+ * nmemb is zero, free the memory block pointed to by ptr.
118
+ * @param ptr Pointer to a memory block already allocated with
119
+ * av_malloc(z)() or av_realloc() or NULL.
120
+ * @param nmemb Number of elements
121
+ * @param size Size of the single element
122
+ * @return Pointer to a newly reallocated block or NULL if the block
123
+ * cannot be reallocated or the function is used to free the memory block.
124
+ */
125
+av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
126
+
127
+/**
128
+ * Allocate or reallocate an array.
129
+ * If *ptr is NULL and nmemb > 0, allocate a new block. If
130
+ * nmemb is zero, free the memory block pointed to by ptr.
131
+ * @param ptr Pointer to a pointer to a memory block already allocated
132
+ * with av_malloc(z)() or av_realloc(), or pointer to a pointer to NULL.
133
+ * The pointer is updated on success, or freed on failure.
134
+ * @param nmemb Number of elements
135
+ * @param size Size of the single element
136
+ * @return Zero on success, an AVERROR error code on failure.
137
+ */
138
+av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
139
+
140
+/**
115 141
  * Free a memory block which has been allocated with av_malloc(z)() or
116 142
  * av_realloc().
117 143
  * @param ptr Pointer to the memory block which should be freed.
... ...
@@ -37,7 +37,7 @@
37 37
  */
38 38
 
39 39
 #define LIBAVUTIL_VERSION_MAJOR 52
40
-#define LIBAVUTIL_VERSION_MINOR 12
40
+#define LIBAVUTIL_VERSION_MINOR 13
41 41
 #define LIBAVUTIL_VERSION_MICRO  0
42 42
 
43 43
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \