This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git
commit ac3c936f41987a59b8b6ad30d7cc8598ff1aafb7 Author: Nick Vatamaniuc <[email protected]> AuthorDate: Mon Apr 13 16:43:58 2026 -0400 Fix potential segfault in termstack expansion When we go from a stack array to a heap in `termstack_push()`, we should copy `stack->size * sizeof(ERL_NIF_TERM)` number of bytes not `num_bytes`. `num_bytes` is the new value which is twice as large. This means we've been doing dirty reads and copying extra junk data that we didn't need before. If we had used up most of the stack and were right against the limit, and there was some stack protection involed we could have hit a segfault here. --- c_src/encoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c_src/encoder.c b/c_src/encoder.c index 8ad4067..cd41d35 100644 --- a/c_src/encoder.c +++ b/c_src/encoder.c @@ -123,7 +123,7 @@ termstack_push(TermStack* stack, ERL_NIF_TERM term) if (stack->elements == &stack->__default_elements[0]) { ERL_NIF_TERM* elems = enif_alloc(num_bytes); - memcpy(elems, stack->elements, num_bytes); + memcpy(elems, stack->elements, SMALL_TERMSTACK_SIZE * sizeof(ERL_NIF_TERM)); stack->elements = elems; } else { stack->elements = enif_realloc(stack->elements, num_bytes);
