]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
printbuf_memset(): set gaps to zero 750/head
authorTobias Stoeckmann <tobias@stoeckmann.org>
Thu, 3 Mar 2022 20:18:53 +0000 (21:18 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Thu, 3 Mar 2022 20:18:53 +0000 (21:18 +0100)
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.

printbuf.c

index 00822fac4f19ebe6f9a5aaafea51ebf38070086b..716698caed5dc24710a60d533da4aa3c3822a86f 100644 (file)
@@ -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;