ext/redcarpet/buffer.c in redcarpet-1.13.1 vs ext/redcarpet/buffer.c in redcarpet-1.13.2

- old
+ new

@@ -186,11 +186,11 @@ /* bufnullterm • NUL-termination of the string array (making a C-string) */ void bufnullterm(struct buf *buf) { if (!buf || !buf->unit) return; if (buf->size < buf->asize && buf->data[buf->size] == 0) return; - if (bufgrow(buf, buf->size + 1)) + if (buf->size + 1 <= buf->asize || bufgrow(buf, buf->size + 1)) buf->data[buf->size] = 0; } /* bufprintf • formatted printing to a buffer */ void @@ -203,11 +203,13 @@ /* bufput • appends raw data to a buffer */ void bufput(struct buf *buf, const void *data, size_t len) { - if (!buf || !bufgrow(buf, buf->size + len)) return; + if (!buf) return; + if (buf->size + len > buf->asize && !bufgrow(buf, buf->size + len)) + return; memcpy(buf->data + buf->size, data, len); buf->size += len; } /* bufputs • appends a NUL-terminated string to a buffer */ @@ -217,10 +219,12 @@ /* bufputc • appends a single char to a buffer */ void bufputc(struct buf *buf, char c) { - if (!buf || !bufgrow(buf, buf->size + 1)) return; + if (!buf) return; + if (buf->size + 1 > buf->asize && !bufgrow(buf, buf->size + 1)) + return; buf->data[buf->size] = c; buf->size += 1; } /* bufrelease • decrease the reference count and free the buffer if needed */