summaryrefslogtreecommitdiff
path: root/sys/src/cmd/audio/libFLAC/share/alloc.h
diff options
context:
space:
mode:
authorSigrid Solveig Haflínudóttir <sigrid@ftrv.se>2022-09-09 16:15:52 +0000
committerSigrid Solveig Haflínudóttir <sigrid@ftrv.se>2022-09-09 16:15:52 +0000
commita5d6746e4d2dc7961910ce7d1729f5500a4dc0f3 (patch)
tree9150b44ca1ad9dc88d42826899e8bade019e3397 /sys/src/cmd/audio/libFLAC/share/alloc.h
parent341502a0fd20bd3188b8d4e100b2d975f2d94007 (diff)
libFLAC: update 1.3.4 → 1.4.0
Diffstat (limited to 'sys/src/cmd/audio/libFLAC/share/alloc.h')
-rw-r--r--sys/src/cmd/audio/libFLAC/share/alloc.h121
1 files changed, 109 insertions, 12 deletions
diff --git a/sys/src/cmd/audio/libFLAC/share/alloc.h b/sys/src/cmd/audio/libFLAC/share/alloc.h
index 914de9ba6..0b40548e7 100644
--- a/sys/src/cmd/audio/libFLAC/share/alloc.h
+++ b/sys/src/cmd/audio/libFLAC/share/alloc.h
@@ -1,6 +1,6 @@
/* alloc - Convenience routines for safely allocating memory
* Copyright (C) 2007-2009 Josh Coalson
- * Copyright (C) 2011-2016 Xiph.Org Foundation
+ * Copyright (C) 2011-2022 Xiph.Org Foundation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -42,7 +42,7 @@
*/
#include <limits.h> /* for SIZE_MAX */
-#if HAVE_STDINT_H
+#ifdef HAVE_STDINT_H
#include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
#endif
#include <stdlib.h> /* for size_t, malloc(), etc */
@@ -63,19 +63,58 @@
# define SIZE_MAX SIZE_T_MAX
#endif
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+extern int alloc_check_threshold, alloc_check_counter;
+
+static inline int alloc_check() {
+ if(alloc_check_threshold == INT32_MAX)
+ return 0;
+ else if(alloc_check_counter++ == alloc_check_threshold)
+ return 1;
+ else
+ return 0;
+}
+
+#endif
+
/* avoid malloc()ing 0 bytes, see:
* https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
*/
+
static inline void *safe_malloc_(size_t size)
{
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ /* Fail if requested */
+ if(alloc_check())
+ return NULL;
+#endif
/* malloc(0) is undefined; FLAC src convention is to always allocate */
if(!size)
size++;
return malloc(size);
}
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+static inline void *malloc_(size_t size)
+{
+ /* Fail if requested */
+ if(alloc_check())
+ return NULL;
+ return malloc(size);
+}
+#else
+#define malloc_ malloc
+#endif
+
+
+
static inline void *safe_calloc_(size_t nmemb, size_t size)
{
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ /* Fail if requested */
+ if(alloc_check())
+ return NULL;
+#endif
if(!nmemb || !size)
return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
return calloc(nmemb, size);
@@ -127,7 +166,7 @@ static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size
size1 *= size2;
if(size1 > SIZE_MAX / size3)
return 0;
- return malloc(size1*size3);
+ return malloc_(size1*size3);
}
/* size1*size2 + size3 */
@@ -150,28 +189,62 @@ static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size
return 0;
if(size1 > SIZE_MAX / size2)
return 0;
- return malloc(size1*size2);
+ return malloc_(size1*size2);
}
static inline void *safe_realloc_(void *ptr, size_t size)
{
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ /* Fail if requested */
+ if(alloc_check() && size > 0) {
+ free(ptr);
+ return NULL;
+ }
+#endif
void *oldptr = ptr;
void *newptr = realloc(ptr, size);
if(size > 0 && newptr == 0)
free(oldptr);
return newptr;
}
-static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
+
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+static inline void *realloc_(void *ptr, size_t size)
+{
+ /* Fail if requested */
+ if(alloc_check())
+ return NULL;
+ return realloc(ptr, size);
+}
+#else
+#define realloc_ realloc
+#endif
+
+
+static inline void *safe_realloc_nofree_add_2op_(void *ptr, size_t size1, size_t size2)
+{
+ size2 += size1;
+ if(size2 < size1)
+ return 0;
+ return realloc_(ptr, size2);
+}
+
+static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
{
size2 += size1;
if(size2 < size1) {
free(ptr);
return 0;
}
- return realloc(ptr, size2);
+ size3 += size2;
+ if(size3 < size2) {
+ free(ptr);
+ return 0;
+ }
+ return safe_realloc_(ptr, size3);
}
-static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
+static inline void *safe_realloc_nofree_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
{
size2 += size1;
if(size2 < size1)
@@ -179,10 +252,10 @@ static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2,
size3 += size2;
if(size3 < size2)
return 0;
- return realloc(ptr, size3);
+ return realloc_(ptr, size3);
}
-static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
+static inline void *safe_realloc_nofree_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
{
size2 += size1;
if(size2 < size1)
@@ -193,27 +266,51 @@ static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2,
size4 += size3;
if(size4 < size3)
return 0;
- return realloc(ptr, size4);
+ return realloc_(ptr, size4);
}
static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
{
if(!size1 || !size2)
return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
- if(size1 > SIZE_MAX / size2)
+ if(size1 > SIZE_MAX / size2) {
+ free(ptr);
return 0;
+ }
return safe_realloc_(ptr, size1*size2);
}
+static inline void *safe_realloc_nofree_mul_2op_(void *ptr, size_t size1, size_t size2)
+{
+ if(!size1 || !size2)
+ return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
+ if(size1 > SIZE_MAX / size2)
+ return 0;
+ return realloc_(ptr, size1*size2);
+}
+
/* size1 * (size2 + size3) */
static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
{
if(!size1 || (!size2 && !size3))
return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
size2 += size3;
- if(size2 < size3)
+ if(size2 < size3) {
+ free(ptr);
return 0;
+ }
return safe_realloc_mul_2op_(ptr, size1, size2);
}
+/* size1 * (size2 + size3) */
+static inline void *safe_realloc_nofree_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
+{
+ if(!size1 || (!size2 && !size3))
+ return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
+ size2 += size3;
+ if(size2 < size3)
+ return 0;
+ return safe_realloc_nofree_mul_2op_(ptr, size1, size2);
+}
+
#endif