aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-06-26 10:47:16 +0200
committerThomas Schmucker <ts@its1.de>2020-06-26 10:47:16 +0200
commitb4bf0e3f4cae08358570784902761348b92bbec5 (patch)
tree51b033d76ba055833518a876fcfb16fc393f0584
parente0ac05034137f7beaa24f371d4b8bd71584d1a87 (diff)
downloadlibcsv-b4bf0e3f4cae08358570784902761348b92bbec5.tar.gz
libcsv-b4bf0e3f4cae08358570784902761348b92bbec5.tar.bz2
libcsv-b4bf0e3f4cae08358570784902761348b92bbec5.zip
Überarbeite die Speicherverwaltung...
-rw-r--r--csv.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/csv.c b/csv.c
index a4f9b51..2b39be0 100644
--- a/csv.c
+++ b/csv.c
@@ -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
18static inline int
19mul_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
17static void * 28static 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
25static void * 40static 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);