Address printbuf review comments

This commit is contained in:
Darren Carreras
2026-08-09 20:01:12 -04:00
parent 62bb838c57
commit 9782370c3f
3 changed files with 26 additions and 2 deletions
+1 -2
View File
@@ -97,8 +97,6 @@ static int printbuf_extend(struct printbuf *p, int min_size)
int printbuf_memappend(struct printbuf *p, const char *buf, int size) int printbuf_memappend(struct printbuf *p, const char *buf, int size)
{ {
int buf_offset = -1;
/* Prevent signed integer overflows with large buffers. */ /* Prevent signed integer overflows with large buffers. */
if (size < 0 || size > INT_MAX - p->bpos - 1) if (size < 0 || size > INT_MAX - p->bpos - 1)
{ {
@@ -107,6 +105,7 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size)
} }
if (p->size <= p->bpos + size + 1) if (p->size <= p->bpos + size + 1)
{ {
int buf_offset = -1;
const uintptr_t buf_addr = (uintptr_t)(const void *)buf; const uintptr_t buf_addr = (uintptr_t)(const void *)buf;
const uintptr_t p_buf_addr = (uintptr_t)(const void *)p->buf; const uintptr_t p_buf_addr = (uintptr_t)(const void *)p->buf;
+4
View File
@@ -52,6 +52,8 @@ JSON_EXPORT struct printbuf *printbuf_new(void);
* *
* Your code should not use printbuf_memappend() directly unless it * Your code should not use printbuf_memappend() directly unless it
* checks the return code. Use printbuf_memappend_fast() instead. * checks the return code. Use printbuf_memappend_fast() instead.
* The source may point into p->buf, including a region that overlaps the
* appended destination.
*/ */
JSON_EXPORT int printbuf_memappend(struct printbuf *p, const char *buf, int size); JSON_EXPORT int printbuf_memappend(struct printbuf *p, const char *buf, int size);
@@ -112,6 +114,8 @@ JSON_EXPORT int printbuf_memset(struct printbuf *pb, int offset, int charvalue,
* important than speed. Avoid using this function in high performance code or * important than speed. Avoid using this function in high performance code or
* tight loops; in these scenarios, consider using snprintf() with a static * tight loops; in these scenarios, consider using snprintf() with a static
* buffer in conjunction with one of the printbuf_*append() functions. * buffer in conjunction with one of the printbuf_*append() functions.
* Format arguments may point into p->buf, including when appending the
* formatted result grows the buffer.
* *
* See also: * See also:
* printbuf_memappend_fast() * printbuf_memappend_fast()
+21
View File
@@ -154,6 +154,27 @@ static void test_printbuf_self_append(void)
assert(pb->buf[i] == 'X'); assert(pb->buf[i] == 'X');
assert(pb->buf[pb->bpos] == '\0'); assert(pb->buf[pb->bpos] == '\0');
printbuf_free(pb);
/* Formatted arguments may also point into a buffer that must grow. */
pb = printbuf_new();
assert(pb != NULL);
initial_size = pb->size;
data_size = initial_size - 2;
data = malloc(data_size);
assert(data != NULL);
memset(data, 'X', data_size);
assert(printbuf_memappend(pb, data, data_size) == data_size);
free(data);
assert(pb->size == initial_size);
assert(sprintbuf(pb, "%s", pb->buf + 1) == data_size - 1);
assert(pb->size > initial_size);
assert(pb->bpos == data_size * 2 - 1);
for (i = 0; i < pb->bpos; i++)
assert(pb->buf[i] == 'X');
assert(pb->buf[pb->bpos] == '\0');
printbuf_free(pb); printbuf_free(pb);
printf("%s: end test\n", __func__); printf("%s: end test\n", __func__);
} }