From 586ade4fb19021fb8893912cda13875ae4120236 Mon Sep 17 00:00:00 2001
From: Gijs van Tulder <gvtulder@gmail.com>
Date: Sat, 28 Jan 2012 14:08:52 +0100
Subject: [PATCH] Fix memory leak.

---
 src/ChangeLog |  5 +++++
 src/http.c    | 14 +++++++++++---
 src/retr.c    | 22 ++++++++++++++++------
 3 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 141a47d4..e10d4c02 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2012-01-27  Gijs van Tulder  <gvtulder@gmail.com>
+
+	* retr.c (fd_read_body): Fix a memory leak with chunked responses.
+	* http.c (skip_short_body): Fix the same memory leak.
+
 2012-01-09  Gijs van Tulder  <gvtulder@gmail.com>
 
 	* init.c: Disable WARC compression if zlib is disabled.
diff --git a/src/http.c b/src/http.c
index 69789fcd..78725796 100644
--- a/src/http.c
+++ b/src/http.c
@@ -951,9 +951,12 @@ skip_short_body (int fd, wgint contlen, bool chunked)
                 break;
 
               remaining_chunk_size = strtol (line, &endl, 16);
+              xfree (line);
+
               if (remaining_chunk_size == 0)
                 {
-                  fd_read_line (fd);
+                  line = fd_read_line (fd);
+                  xfree_null (line);
                   break;
                 }
             }
@@ -978,8 +981,13 @@ skip_short_body (int fd, wgint contlen, bool chunked)
         {
           remaining_chunk_size -= ret;
           if (remaining_chunk_size == 0)
-            if (fd_read_line (fd) == NULL)
-              return false;
+            {
+              char *line = fd_read_line (fd);
+              if (line == NULL)
+                return false;
+              else
+                xfree (line);
+            }
         }
 
       /* Safe even if %.*s bogusly expects terminating \0 because
diff --git a/src/retr.c b/src/retr.c
index 3df582b8..f57b2c6d 100644
--- a/src/retr.c
+++ b/src/retr.c
@@ -307,11 +307,16 @@ fd_read_body (int fd, FILE *out, wgint toread, wgint startpos,
                 }
 
               remaining_chunk_size = strtol (line, &endl, 16);
+              xfree (line);
+
               if (remaining_chunk_size == 0)
                 {
                   ret = 0;
-                  if (fd_read_line (fd) == NULL)
+                  line = fd_read_line (fd);
+                  if (line == NULL)
                     ret = -1;
+                  else
+                    xfree (line);
                   break;
                 }
             }
@@ -371,11 +376,16 @@ fd_read_body (int fd, FILE *out, wgint toread, wgint startpos,
             {
               remaining_chunk_size -= ret;
               if (remaining_chunk_size == 0)
-                if (fd_read_line (fd) == NULL)
-                  {
-                    ret = -1;
-                    break;
-                  }
+                {
+                  char *line = fd_read_line (fd);
+                  if (line == NULL)
+                    {
+                      ret = -1;
+                      break;
+                    }
+                  else
+                    xfree (line);
+                }
             }
         }