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
13 13
14API changes, most recent first:14API changes, most recent first:
15 15
162013-06-xx - xxxxxxx - lavu 52.13.0 - mem.h
17 Add av_realloc_array and av_reallocp_array
18
162013-05-xx - xxxxxxx - lavfi 3.10.0 - avfilter.h192013-05-xx - xxxxxxx - lavfi 3.10.0 - avfilter.h
17 Add support for slice multithreading to lavfi. Filters supporting threading20 Add support for slice multithreading to lavfi. Filters supporting threading
18 are marked with AVFILTER_FLAG_SLICE_THREADS.21 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
136#endif136#endif
137}137}
138 138
139void *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
146int 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
139void av_free(void *ptr)165void av_free(void *ptr)
140{166{
141#if CONFIG_MEMALIGN_HACK167#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
111 */111 */
112void *av_realloc(void *ptr, size_t size) av_alloc_size(2);112void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
113 113
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 */
125av_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 */
138av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
139
114/**140/**
115 * Free a memory block which has been allocated with av_malloc(z)() or141 * Free a memory block which has been allocated with av_malloc(z)() or
116 * av_realloc().142 * av_realloc().
libavutil/version.h+1−1
@@ -37,7 +37,7 @@ · 36 unmodified lines
37 */37 */
38 38
39#define LIBAVUTIL_VERSION_MAJOR 5239#define LIBAVUTIL_VERSION_MAJOR 52
40#define LIBAVUTIL_VERSION_MINOR 1240#define LIBAVUTIL_VERSION_MINOR 13
41#define LIBAVUTIL_VERSION_MICRO 041#define LIBAVUTIL_VERSION_MICRO 0
42 42
43#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \43#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