aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-05-06 22:27:59 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-05-06 22:27:59 -0400
commit46555c558870e8b96f1ce361d74fc404ca13c471 (patch)
treee0b01a6a2b5a99754bbc1ca7131a37a8badc9d54 /src
parent817adbf22592955244aecad435ce0555707dba1a (diff)
Check return values
Diffstat (limited to 'src')
-rw-r--r--src/stdlib/c_strings.c3
-rw-r--r--src/stdlib/lists.c14
-rw-r--r--src/stdlib/stdlib.c2
-rw-r--r--src/stdlib/text.c3
-rw-r--r--src/tomo.c2
5 files changed, 15 insertions, 9 deletions
diff --git a/src/stdlib/c_strings.c b/src/stdlib/c_strings.c
index c153a8a9..860a69ff 100644
--- a/src/stdlib/c_strings.c
+++ b/src/stdlib/c_strings.c
@@ -64,7 +64,8 @@ static void CString$deserialize(FILE *in, void *out, List_t *pointers, const Typ
int64_t len = -1;
Int64$deserialize(in, &len, pointers, &Int64$info);
char *str = GC_MALLOC_ATOMIC((size_t)len+1);
- fread(str, sizeof(char), (size_t)len, in);
+ if (fread(str, sizeof(char), (size_t)len, in) != (size_t)len)
+ fail("Not enough data in stream to deserialize");
str[len+1] = '\0';
*(const char**)out = str;
}
diff --git a/src/stdlib/lists.c b/src/stdlib/lists.c
index c73d20dd..c1c119a0 100644
--- a/src/stdlib/lists.c
+++ b/src/stdlib/lists.c
@@ -297,7 +297,7 @@ static int64_t _default_random_int64(int64_t min, int64_t max, void *userdata)
uint64_t min_r = -range % range;
uint64_t r;
for (;;) {
- getrandom(&r, sizeof(r), 0);
+ assert(getrandom(&r, sizeof(r), 0) == sizeof(r));
if (r >= min_r) break;
}
return (int64_t)((uint64_t)min + (r % range));
@@ -361,7 +361,7 @@ static double _default_random_num(void *userdata)
Num_t num;
uint64_t bits;
} r = {.bits=0}, one = {.num=1.0};
- getrandom((uint8_t*)&r, sizeof(r), 0);
+ assert(getrandom((uint8_t*)&r, sizeof(r), 0) == sizeof(r));
// Set r.num to 1.<random-bits>
r.bits &= ~(0xFFFULL << 52);
@@ -808,10 +808,14 @@ public void List$deserialize(FILE *in, void *obj, List_t *pointers, const TypeIn
for (int64_t i = 0; i < len; i++)
item_deserialize(in, list.data + i*list.stride, pointers, type->ListInfo.item);
} else if (list.stride == type->ListInfo.item->size) {
- fread(list.data, (size_t)type->ListInfo.item->size, (size_t)len, in);
+ if (fread(list.data, (size_t)type->ListInfo.item->size, (size_t)len, in) != (size_t)len)
+ fail("Not enough data in stream to deserialize");
} else {
- for (int64_t i = 0; i < len; i++)
- fread(list.data + i*list.stride, (size_t)type->ListInfo.item->size, 1, in);
+ size_t item_size = (size_t)type->ListInfo.item->size;
+ for (int64_t i = 0; i < len; i++) {
+ if (fread(list.data + i*list.stride, item_size, 1, in) != item_size)
+ fail("Not enough data in stream to deserialize");
+ }
}
*(List_t*)obj = list;
}
diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c
index 54c7827f..6a855c66 100644
--- a/src/stdlib/stdlib.c
+++ b/src/stdlib/stdlib.c
@@ -65,7 +65,7 @@ public void tomo_init(void)
USE_COLOR = false;
setlocale(LC_ALL, "");
- getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0);
+ assert(getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0) == sizeof(TOMO_HASH_KEY));
struct sigaction sigact;
sigact.sa_sigaction = signal_handler;
diff --git a/src/stdlib/text.c b/src/stdlib/text.c
index cc8a4daf..2107c1df 100644
--- a/src/stdlib/text.c
+++ b/src/stdlib/text.c
@@ -1620,7 +1620,8 @@ public void Text$deserialize(FILE *in, void *out, List_t *pointers, const TypeIn
int64_t len = -1;
Int64$deserialize(in, &len, pointers, &Int64$info);
char *buf = GC_MALLOC_ATOMIC((size_t)len+1);
- fread(buf, sizeof(char), (size_t)len, in);
+ if (fread(buf, sizeof(char), (size_t)len, in) != (size_t)len)
+ fail("Not enough data in stream to deserialize");
buf[len+1] = '\0';
*(Text_t*)out = Text$from_strn(buf, (size_t)len);
}
diff --git a/src/tomo.c b/src/tomo.c
index 2299b0a1..bbc33bc1 100644
--- a/src/tomo.c
+++ b/src/tomo.c
@@ -142,7 +142,7 @@ int main(int argc, char *argv[])
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
arc4random_buf(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0);
#elif defined(__linux__)
- getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0);
+ assert(getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0) == sizeof(TOMO_HASH_KEY));
#else
#error "Unsupported platform for secure random number generation"
#endif