]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
dynbuf: never allocate larger than "toobig"
authorDaniel Stenberg <daniel@haxx.se>
Mon, 27 Mar 2023 11:02:08 +0000 (13:02 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 28 Mar 2023 08:22:53 +0000 (10:22 +0200)
As dynbufs always have a fixed maximum size which they are not allowed
to grow larger than, making sure that it never allocates a larger buffer
makes sure the buffer does not allocate memory that will never be used.

Closes #10845

lib/dynbuf.c

index bd3b9356c7664d375918fe0ed8c73f92583ff183..0c9c491aebc1b7ffc4f3d62d616d44e07fb32c6a 100644 (file)
@@ -76,6 +76,7 @@ static CURLcode dyn_nappend(struct dynbuf *s,
   DEBUGASSERT(s->toobig);
   DEBUGASSERT(indx < s->toobig);
   DEBUGASSERT(!s->leng || s->bufr);
+  DEBUGASSERT(a <= s->toobig);
 
   if(fit > s->toobig) {
     Curl_dyn_free(s);
@@ -84,7 +85,9 @@ static CURLcode dyn_nappend(struct dynbuf *s,
   else if(!a) {
     DEBUGASSERT(!indx);
     /* first invoke */
-    if(fit < MIN_FIRST_ALLOC)
+    if(MIN_FIRST_ALLOC > s->toobig)
+      a = s->toobig;
+    else if(fit < MIN_FIRST_ALLOC)
       a = MIN_FIRST_ALLOC;
     else
       a = fit;
@@ -92,6 +95,9 @@ static CURLcode dyn_nappend(struct dynbuf *s,
   else {
     while(a < fit)
       a *= 2;
+    if(a > s->toobig)
+      /* no point in allocating a larger buffer than this is allowed to use */
+      a = s->toobig;
   }
 
   if(a != s->allc) {