diff options
Diffstat (limited to 'csv.c')
| -rw-r--r-- | csv.c | 19 |
1 files changed, 17 insertions, 2 deletions
| @@ -12,6 +12,17 @@ | |||
| 12 | #define CSV_DEFAULT_SEPARATOR ',' | 12 | #define CSV_DEFAULT_SEPARATOR ',' |
| 13 | #endif | 13 | #endif |
| 14 | 14 | ||
| 15 | #if defined(__GNUC__) || defined(__clang__) | ||
| 16 | #define mul_overflow __builtin_mul_overflow | ||
| 17 | #else | ||
| 18 | static inline int | ||
| 19 | mul_overflow(size_t a, size_t b, size_t *out) | ||
| 20 | { | ||
| 21 | *out = a * b; | ||
| 22 | return ( a != 0 && (*out / a) != b ); | ||
| 23 | } | ||
| 24 | #endif | ||
| 25 | |||
| 15 | // === CSV-MEMORY Interface === | 26 | // === CSV-MEMORY Interface === |
| 16 | 27 | ||
| 17 | static void * | 28 | static void * |
| @@ -19,7 +30,11 @@ csv_mem_allocate(size_t n, size_t sz, void *cb_arg) | |||
| 19 | { | 30 | { |
| 20 | (void) cb_arg; | 31 | (void) cb_arg; |
| 21 | 32 | ||
| 22 | return calloc(n, sz); | 33 | size_t len; |
| 34 | if ( mul_overflow(n, sz, &len) ) { | ||
| 35 | return NULL; | ||
| 36 | } | ||
| 37 | return malloc(len); | ||
| 23 | } | 38 | } |
| 24 | 39 | ||
| 25 | static void * | 40 | static void * |
| @@ -28,7 +43,7 @@ csv_mem_reallocate(void *ptr, size_t n, size_t sz, void *cb_arg) | |||
| 28 | (void) cb_arg; | 43 | (void) cb_arg; |
| 29 | 44 | ||
| 30 | size_t len; | 45 | size_t len; |
| 31 | if ( __builtin_mul_overflow(n, sz, &len) ) { | 46 | if ( mul_overflow(n, sz, &len) ) { |
| 32 | return NULL; | 47 | return NULL; |
| 33 | } | 48 | } |
| 34 | return realloc(ptr, len); | 49 | return realloc(ptr, len); |
