summaryrefslogtreecommitdiffstats
path: root/XUtils.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2020-10-05 12:49:01 +0200
committerChristian Göttsche <cgzones@googlemail.com>2020-10-19 15:35:43 +0200
commit577416d1a946382ab9f0c523e5fae755f9d71f69 (patch)
tree178b99a4d3d40137c0fcb765d655339f53b37728 /XUtils.c
parent0db398d4c3472071b2814505242450cd8f831501 (diff)
Assert allocating non-zero size memory
Allocating zero size memory results in implementation-defined behavior: man:malloc(3) : If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
Diffstat (limited to 'XUtils.c')
-rw-r--r--XUtils.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/XUtils.c b/XUtils.c
index 1e77cd3b..3bdcba84 100644
--- a/XUtils.c
+++ b/XUtils.c
@@ -25,26 +25,26 @@ void fail() {
}
void* xMalloc(size_t size) {
+ assert(size > 0);
void* data = malloc(size);
- if (!data && size > 0) {
+ if (!data) {
fail();
}
return data;
}
void* xCalloc(size_t nmemb, size_t size) {
+ assert(nmemb > 0);
+ assert(size > 0);
void* data = calloc(nmemb, size);
- if (!data && nmemb > 0 && size > 0) {
+ if (!data) {
fail();
}
return data;
}
void* xRealloc(void* ptr, size_t size) {
- if (!size) {
- free(ptr);
- return NULL;
- }
+ assert(size > 0);
void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail()
if (!data) {
free(ptr);

© 2014-2024 Faster IT GmbH | imprint | privacy policy