From 1e36ae36aeb92220c9f13e0af173850ad9054320 Mon Sep 17 00:00:00 2001 From: Nguyen Thien Date: Wed, 13 Nov 2024 11:34:42 +0700 Subject: [PATCH] fix: only update the buffer if realloc succeeded --- src/buffer.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index aeca03d09..6f111392c 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -52,9 +52,14 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) { bufsize_t new_size = target_size + target_size / 2; new_size += 1; new_size = (new_size + 7) & ~7; - - buf->ptr = (unsigned char *)buf->mem->realloc(buf->asize ? buf->ptr : NULL, - new_size); + + unsigned char *new_ptr = (unsigned char *)buf->mem->realloc(buf->asize ? buf->ptr : NULL, new_size); + + if (new_ptr != NULL) { + // Only update the buffer if realloc succeeded + buf->ptr = new_ptr; + } + buf->asize = new_size; }