aboutsummaryrefslogtreecommitdiff
path: root/tinyalloc.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-11-12 00:42:32 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2025-11-12 00:42:32 +0100
commite32da5559962c6131e1f933a245526d699d45cf6 (patch)
treed16f75c235302b78cbaca7b86c569a5990ff0ce6 /tinyalloc.c
parent07bff5530cf22d95b98139063ab50cabb8cabe76 (diff)
Fix buffer overflow on ta_reallocHEADmaster
When reallocating a buffer to one with a smaller size, the number of elements to be copied to the new buffer must not be that of the old buffer, or the behaviour is undefined.
Diffstat (limited to 'tinyalloc.c')
-rw-r--r--tinyalloc.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/tinyalloc.c b/tinyalloc.c
index 1c53567..ba3f916 100644
--- a/tinyalloc.c
+++ b/tinyalloc.c
@@ -287,7 +287,9 @@ void *ta_realloc(void *ptr, size_t n) {
if (!new)
return NULL;
- for (size_t i = 0; i < prev->size; i++)
+ const size_t size = n > prev->size ? prev->size : n;
+
+ for (size_t i = 0; i < size; i++)
*p++ = *src++;
ta_free(prev->addr);