diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-12 00:42:32 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-12 00:42:32 +0100 |
| commit | e32da5559962c6131e1f933a245526d699d45cf6 (patch) | |
| tree | d16f75c235302b78cbaca7b86c569a5990ff0ce6 | |
| parent | 07bff5530cf22d95b98139063ab50cabb8cabe76 (diff) | |
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.
| -rw-r--r-- | tinyalloc.c | 4 |
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); |
