From: Tobias Stoeckmann Date: Thu, 3 Mar 2022 20:18:53 +0000 (+0100) Subject: printbuf_memset(): set gaps to zero X-Git-Tag: json-c-0.16-20220414~15^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F750%2Fhead;p=thirdparty%2Fjson-c.git printbuf_memset(): set gaps to zero It is possible to have a printbuf with "gaps", i.e. areas within the print buffer which have not been initialized by using printbuf_memset. Always clear memory in such cases. Example: ``` struct printbuf *pb = printbuf_new(); printbuf_memset(pb, 10, 'a', 2); ``` In this case pb->buf[0] is '\0' but pb->buf[1] up to pb->buf[9] are not set. The length would be 12 due to successful printbuf_memset. --- diff --git a/printbuf.c b/printbuf.c index 00822fac..716698ca 100644 --- a/printbuf.c +++ b/printbuf.c @@ -120,6 +120,8 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len) return -1; } + if (pb->bpos < offset) + memset(pb->buf + pb->bpos, '\0', offset - pb->bpos); memset(pb->buf + offset, charvalue, len); if (pb->bpos < size_needed) pb->bpos = size_needed;