diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 00:41:54 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 00:41:54 +0200 |
| commit | ecc202ac078d5cb5c61d60268fce5ac46bac1e87 (patch) | |
| tree | c78e32d022c07cac6f499e80b6c3c3f83bad9b44 /src/alloc.c | |
| parent | eb7112ae055dda38da52b07449a7e1cf62005702 (diff) | |
| download | ffwd-ecc202ac078d5cb5c61d60268fce5ac46bac1e87.tar.gz ffwd-ecc202ac078d5cb5c61d60268fce5ac46bac1e87.zip | |
cpu: reject negative current capacity in grow_cap
Diffstat (limited to '')
| -rw-r--r-- | src/alloc.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/alloc.c b/src/alloc.c index e3b82d7..41a59a4 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -4,9 +4,10 @@ #include <stdint.h> #include <limits.h> -/* Sequence-capacity growth policy for the forward-pass workspaces: never - * allocate fewer than the minimum, and round up to the granularity so small - * length changes do not trigger a realloc on every call. */ +/* Sequence-capacity growth policy for the forward-pass workspaces: for new + * growth, never allocate fewer than the minimum, and round up to the + * granularity so small length changes do not trigger a realloc on every call. + * Existing larger capacity is preserved. */ #define EMBED_MIN_WORKSPACE_SEQ_CAP 16 #define EMBED_WORKSPACE_SEQ_GRANULARITY 16 @@ -25,7 +26,7 @@ int add_size(size_t a, size_t b, size_t *out) { } int grow_cap(int current, int needed, int *out) { - if (needed <= 0) + if (current < 0 || needed <= 0) return -1; int cap = needed; @@ -34,10 +35,10 @@ int grow_cap(int current, int needed, int *out) { int rem = cap % EMBED_WORKSPACE_SEQ_GRANULARITY; if (rem != 0) { - int add = EMBED_WORKSPACE_SEQ_GRANULARITY - rem; - if (cap > INT_MAX - add) + int pad = EMBED_WORKSPACE_SEQ_GRANULARITY - rem; + if (cap > INT_MAX - pad) return -1; - cap += add; + cap += pad; } if (cap < current) |
