fix check_available_write_cb buffer-growing logic default tip
authorterom@terom-desktop
Thu, 24 Jul 2008 16:18:29 +0300
changeset 11185 5036deb1fcaf
parent 11184 88c967f1422b
fix check_available_write_cb buffer-growing logic
src/network/newgrf_download.cpp
--- a/src/network/newgrf_download.cpp	Tue Jul 22 23:20:33 2008 +0300
+++ b/src/network/newgrf_download.cpp	Thu Jul 24 16:18:29 2008 +0300
@@ -64,13 +64,16 @@
     struct check_write_ctx *ctx = (struct check_write_ctx *) arg;
     
     // total number of bytes
-    size_t len = size * nmemb;
+    size_t len = size * nmemb, oldlen = ctx->len;
+    
+    // increase the size until the new data fits
+    while (ctx->offset + len + 1 > ctx->len) {
+        ctx->len *= 2;
+    }
     
     // realloc if needed
-    if (ctx->offset + len + 1 > ctx->len) {
-        ctx->len *= 2;
+    if (ctx->len != oldlen)
         ctx->buf = ReallocT<char>(ctx->buf, ctx->len);
-    }
     
     // thankfully we only have a limited amount of data..
     memcpy(ctx->buf + ctx->offset, ptr, len);