History

mem: Add av_realloc_array and av_reallocp_array

These help avoiding overflows and simplify error handling.

Martin Storsjö committed 13 years ago

Commit fc962d41 parent 9683e374 files · +56 −1Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/mem.c +26 −0
  3. libavutil/mem.h +26 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2012-10-22 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2013-06-xx - xxxxxxx - lavu 52.13.0 - mem.h
17+ Add av_realloc_array and av_reallocp_array
18+
1619 2013-05-xx - xxxxxxx - lavfi 3.10.0 - avfilter.h
1720 Add support for slice multithreading to lavfi. Filters supporting threading
1821 are marked with AVFILTER_FLAG_SLICE_THREADS.
libavutil/mem.c+26−0
@@ -136,6 +136,32 @@ void *av_realloc(void *ptr, size_t size) · 135 unmodified lines
136136 #endif
137137 }
138138
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+
139165 void av_free(void *ptr)
140166 {
141167 #if CONFIG_MEMALIGN_HACK
libavutil/mem.h+26−0
@@ -111,6 +111,32 @@ av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t siz · 110 unmodified lines
111111 */
112112 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
113113
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+
114140 /**
115141 * Free a memory block which has been allocated with av_malloc(z)() or
116142 * av_realloc().
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
3737 */
3838
3939 #define LIBAVUTIL_VERSION_MAJOR 52
40−#define LIBAVUTIL_VERSION_MINOR 12
40+#define LIBAVUTIL_VERSION_MINOR 13
4141 #define LIBAVUTIL_VERSION_MICRO 0
4242
4343 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t