]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
Ensure progress.size_dl/progress.size_ul are always >= 0
authorBrandon Casey <drafnel@gmail.com>
Fri, 29 Aug 2014 21:48:03 +0000 (23:48 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 7 Sep 2014 21:23:12 +0000 (23:23 +0200)
Historically the default "unknown" value for progress.size_dl and
progress.size_ul has been zero, since these values are initialized
implicitly by the calloc that allocates the curl handle that these
variables are a part of.  Users of curl that install progress
callbacks may expect these values to always be >= 0.

Currently it is possible for progress.size_dl and progress.size_ul
to by set to a value of -1, if Curl_pgrsSetDownloadSize() or
Curl_pgrsSetUploadSize() are passed a "size" of -1 (which a few
places currently do, and a following patch will add more).  So
lets update Curl_pgrsSetDownloadSize() and Curl_pgrsSetUploadSize()
so they make sure that these variables always contain a value that
is >= 0.

Updates test579 and test599.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
lib/ftp.c
lib/http.c
lib/imap.c
lib/pop3.c
lib/progress.c
lib/smtp.c
lib/ssh.c
tests/data/test579
tests/data/test599
tests/libtest/lib599.c

index 4c4396abbe0b362c67dff9806d2a84972ed5e2dd..715afc2f2610414cff3333e856e03a228c3e2dce 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -4470,8 +4470,8 @@ CURLcode ftp_regular_transfer(struct connectdata *conn,
 
   Curl_pgrsSetUploadCounter(data, 0);
   Curl_pgrsSetDownloadCounter(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
-  Curl_pgrsSetDownloadSize(data, 0);
+  Curl_pgrsSetUploadSize(data, -1);
+  Curl_pgrsSetDownloadSize(data, -1);
 
   ftpc->ctl_valid = TRUE; /* starts good */
 
index 3fdab27eee1d7bac7c6486b3962d58e069188a47..35baa3407d57d9f483bf8b56340d8ef1c4a76741 100644 (file)
@@ -2351,7 +2351,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
     return result;
 
   http->postdata = NULL;  /* nothing to post at this point */
-  Curl_pgrsSetUploadSize(data, 0); /* upload size is 0 atm */
+  Curl_pgrsSetUploadSize(data, -1); /* upload size is unknown atm */
 
   /* If 'authdone' is FALSE, we must not set the write socket index to the
      Curl_transfer() call below, as we're not ready to actually upload any
index e80bc03c13b79401675c9d935b2064c56fa851da..9fc472866d14a1166fc9713639dbc8ca0e4aaede 100644 (file)
@@ -1662,7 +1662,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn, int imapcode,
   (void)instate; /* no use for this yet */
 
   if(imapcode != '*') {
-    Curl_pgrsSetDownloadSize(data, 0);
+    Curl_pgrsSetDownloadSize(data, -1);
     state(conn, IMAP_STOP);
     return CURLE_REMOTE_FILE_NOT_FOUND; /* TODO: Fix error code */
   }
@@ -2336,8 +2336,8 @@ static CURLcode imap_regular_transfer(struct connectdata *conn,
   /* Set the progress data */
   Curl_pgrsSetUploadCounter(data, 0);
   Curl_pgrsSetDownloadCounter(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
-  Curl_pgrsSetDownloadSize(data, 0);
+  Curl_pgrsSetUploadSize(data, -1);
+  Curl_pgrsSetDownloadSize(data, -1);
 
   /* Carry out the perform */
   result = imap_perform(conn, &connected, dophase_done);
index e69f5c5cf233d711d6d436d8a3dd42a330e00274..dc64f81065200836ad67776f59a455abff469e89 100644 (file)
@@ -1935,8 +1935,8 @@ static CURLcode pop3_regular_transfer(struct connectdata *conn,
   /* Set the progress data */
   Curl_pgrsSetUploadCounter(data, 0);
   Curl_pgrsSetDownloadCounter(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
-  Curl_pgrsSetDownloadSize(data, 0);
+  Curl_pgrsSetUploadSize(data, -1);
+  Curl_pgrsSetDownloadSize(data, -1);
 
   /* Carry out the perform */
   result = pop3_perform(conn, &connected, dophase_done);
index e6a8d825a663398e00e397bc405361905e75a0ba..f147ce71ee531d783665679865c6d48f85acea00 100644 (file)
@@ -159,8 +159,8 @@ void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
   data->progress.t_pretransfer = 0.0;
   data->progress.t_starttransfer = 0.0;
 
-  Curl_pgrsSetDownloadSize(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
+  Curl_pgrsSetDownloadSize(data, -1);
+  Curl_pgrsSetUploadSize(data, -1);
 }
 
 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
@@ -234,20 +234,26 @@ void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
 
 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
 {
-  data->progress.size_dl = size;
-  if(size >= 0)
+  if(size >= 0) {
+    data->progress.size_dl = size;
     data->progress.flags |= PGRS_DL_SIZE_KNOWN;
-  else
+  }
+  else {
+    data->progress.size_dl = 0;
     data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
+  }
 }
 
 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
 {
-  data->progress.size_ul = size;
-  if(size >= 0)
+  if(size >= 0) {
+    data->progress.size_ul = size;
     data->progress.flags |= PGRS_UL_SIZE_KNOWN;
-  else
+  }
+  else {
+    data->progress.size_ul = 0;
     data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
+  }
 }
 
 /*
index 4dc78497c8943dffddf44579330ee25de08bf6e1..9aa8b15bd73991eca5541068260f922b94a7551d 100644 (file)
@@ -2031,8 +2031,8 @@ static CURLcode smtp_regular_transfer(struct connectdata *conn,
   /* Set the progress data */
   Curl_pgrsSetUploadCounter(data, 0);
   Curl_pgrsSetDownloadCounter(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
-  Curl_pgrsSetDownloadSize(data, 0);
+  Curl_pgrsSetUploadSize(data, -1);
+  Curl_pgrsSetDownloadSize(data, -1);
 
   /* Carry out the perform */
   result = smtp_perform(conn, &connected, dophase_done);
index b248b43a08abaa53b00493e80a70c7af8481043c..887e10f21ca9c9d9e46bbd656c5847bc76328c5d 100644 (file)
--- a/lib/ssh.c
+++ b/lib/ssh.c
@@ -2878,8 +2878,8 @@ static CURLcode ssh_do(struct connectdata *conn, bool *done)
 
   Curl_pgrsSetUploadCounter(data, 0);
   Curl_pgrsSetDownloadCounter(data, 0);
-  Curl_pgrsSetUploadSize(data, 0);
-  Curl_pgrsSetDownloadSize(data, 0);
+  Curl_pgrsSetUploadSize(data, -1);
+  Curl_pgrsSetDownloadSize(data, -1);
 
   if(conn->handler->protocol & CURLPROTO_SCP)
     res = scp_perform(conn, &connected,  done);
index adbb3dd22854f3b8791a61d5646488a1b14f8c9f..e352e3d602ec244eb4e8cb71190edda449abee58 100644 (file)
@@ -77,12 +77,11 @@ http://%HOSTIP:%HTTPPORT/579 log/ip579
 <verify>
 <file name="log/ip579">
 Progress callback called with UL 0 out of 0
-Progress callback called with UL 0 out of -1
-Progress callback called with UL 8 out of -1
-Progress callback called with UL 16 out of -1
-Progress callback called with UL 26 out of -1
-Progress callback called with UL 61 out of -1
-Progress callback called with UL 66 out of -1
+Progress callback called with UL 8 out of 0
+Progress callback called with UL 16 out of 0
+Progress callback called with UL 26 out of 0
+Progress callback called with UL 61 out of 0
+Progress callback called with UL 66 out of 0
 </file>
 </verify>
 </testcase>
index c57fe5c919fc0f39f2cf5d0fef44641bab39bd2f..9ce8b234c0f67330cfd21009bc728012e3e7e56b 100644 (file)
@@ -71,13 +71,15 @@ lib599
 HTTP GET with progress callback and redirects changing content sizes
  </name>
  <command>
-http://%HOSTIP:%HTTPPORT/599
+http://%HOSTIP:%HTTPPORT/599 log/ip599
 </command>
 </client>
 
 #
 # Verify data after the test has been "shot"
 <verify>
-
+<file name="log/ip599">
+CL: -1
+</file>
 </verify>
 </testcase>
index 6b092677a0f03814fba8966ec8a118760722a2cf..08c536c7215bed186e491498fba374ede3722759 100644 (file)
@@ -43,6 +43,7 @@ int test(char *URL)
 {
   CURL *curl;
   CURLcode res=CURLE_OK;
+  double content_length = 0.0;
 
   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     fprintf(stderr, "curl_global_init() failed\n");
@@ -74,6 +75,17 @@ int test(char *URL)
   /* Perform the request, res will get the return code */
   res = curl_easy_perform(curl);
 
+  if (!res) {
+    FILE *moo;
+    res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
+                            &content_length);
+    moo = fopen(libtest_arg2, "wb");
+    if (moo) {
+      fprintf(moo, "CL: %.0f\n", content_length);
+      fclose(moo);
+    }
+  }
+
 test_cleanup:
 
   /* always cleanup */