]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
strbuf: Update buf->size if realloc succeeds. 3481/head
authorFlorian Forster <octo@google.com>
Thu, 9 Jul 2020 10:33:09 +0000 (12:33 +0200)
committerFlorian Forster <octo@google.com>
Tue, 14 Jul 2020 17:18:50 +0000 (19:18 +0200)
src/utils/strbuf/strbuf.c

index b0b5da186a8a74a7800738d5abf7899c4aad1aa1..d72db6a78bc427d824a07db9319390ca6d501ed7 100644 (file)
@@ -135,10 +135,13 @@ void strbuf_reset(strbuf_t *buf) {
   /* Truncate the buffer to the page size. This is deemed a good compromise
    * between freeing memory (after a large buffer has been constructed) and
    * performance (avoid unnecessary allocations). */
-  if (buf->size > strbuf_pagesize()) {
-    char *new_ptr = realloc(buf->ptr, strbuf_pagesize());
-    if (new_ptr != NULL)
+  size_t new_size = strbuf_pagesize();
+  if (buf->size > new_size) {
+    char *new_ptr = realloc(buf->ptr, new_size);
+    if (new_ptr != NULL) {
       buf->ptr = new_ptr;
+      buf->size = new_size;
+    }
   }
 }