aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-23 01:29:52 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-23 02:06:31 +0200
commit0a5383ccdf8e87d2259d02a9ff44420b3bc3f58d (patch)
treeb92fb06a065a201c0cd21aafd3ee265c847c9b84 /include
parent8c8467a824c950fd1094e46a74c1f57048e5f099 (diff)
Add wrappers for calloc style functions with size overflow checking
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r--include/util.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/include/util.h b/include/util.h
index ae8c43c..ba95b03 100644
--- a/include/util.h
+++ b/include/util.h
@@ -115,4 +115,20 @@ int padd_file(int outfd, uint64_t size, size_t blocksize);
*/
uint32_t get_source_date_epoch(void);
+/*
+ Helper for allocating data structures with flexible array members.
+
+ 'base_size' is the size of the struct itself, 'item_size' the size of a
+ single array element and 'nmemb' the number of elements.
+
+ Iternally checks for arithmetic overflows when allocating the combined thing.
+ */
+void *alloc_flex(size_t base_size, size_t item_size, size_t nmemb);
+
+/* Basically the same as calloc, but *ALWAYS* does overflow checking */
+void *alloc_array(size_t item_size, size_t nmemb);
+
+/* allocates len + 1 (for the null-terminator) and does overflow checking */
+void *alloc_string(size_t len);
+
#endif /* UTIL_H */