1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 08:08:50 -05:00

Ensure progress.size_dl/progress.size_ul are always >= 0

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>
This commit is contained in:
Brandon Casey 2014-08-29 23:48:03 +02:00 committed by Daniel Stenberg
parent 8acbb074f8
commit 6beb0eeea1
10 changed files with 47 additions and 28 deletions

View File

@ -4470,8 +4470,8 @@ CURLcode ftp_regular_transfer(struct connectdata *conn,
Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
ftpc->ctl_valid = TRUE; /* starts good */ ftpc->ctl_valid = TRUE; /* starts good */

View File

@ -2351,7 +2351,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
return result; return result;
http->postdata = NULL; /* nothing to post at this point */ 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 /* 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 Curl_transfer() call below, as we're not ready to actually upload any

View File

@ -1662,7 +1662,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn, int imapcode,
(void)instate; /* no use for this yet */ (void)instate; /* no use for this yet */
if(imapcode != '*') { if(imapcode != '*') {
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
state(conn, IMAP_STOP); state(conn, IMAP_STOP);
return CURLE_REMOTE_FILE_NOT_FOUND; /* TODO: Fix error code */ 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 */ /* Set the progress data */
Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
/* Carry out the perform */ /* Carry out the perform */
result = imap_perform(conn, &connected, dophase_done); result = imap_perform(conn, &connected, dophase_done);

View File

@ -1935,8 +1935,8 @@ static CURLcode pop3_regular_transfer(struct connectdata *conn,
/* Set the progress data */ /* Set the progress data */
Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
/* Carry out the perform */ /* Carry out the perform */
result = pop3_perform(conn, &connected, dophase_done); result = pop3_perform(conn, &connected, dophase_done);

View File

@ -159,8 +159,8 @@ void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
data->progress.t_pretransfer = 0.0; data->progress.t_pretransfer = 0.0;
data->progress.t_starttransfer = 0.0; data->progress.t_starttransfer = 0.0;
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
} }
void Curl_pgrsTime(struct SessionHandle *data, timerid timer) 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) 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; data->progress.flags |= PGRS_DL_SIZE_KNOWN;
else }
else {
data->progress.size_dl = 0;
data->progress.flags &= ~PGRS_DL_SIZE_KNOWN; data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
}
} }
void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size) 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; data->progress.flags |= PGRS_UL_SIZE_KNOWN;
else }
else {
data->progress.size_ul = 0;
data->progress.flags &= ~PGRS_UL_SIZE_KNOWN; data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
}
} }
/* /*

View File

@ -2031,8 +2031,8 @@ static CURLcode smtp_regular_transfer(struct connectdata *conn,
/* Set the progress data */ /* Set the progress data */
Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
/* Carry out the perform */ /* Carry out the perform */
result = smtp_perform(conn, &connected, dophase_done); result = smtp_perform(conn, &connected, dophase_done);

View File

@ -2878,8 +2878,8 @@ static CURLcode ssh_do(struct connectdata *conn, bool *done)
Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0); Curl_pgrsSetUploadSize(data, -1);
Curl_pgrsSetDownloadSize(data, 0); Curl_pgrsSetDownloadSize(data, -1);
if(conn->handler->protocol & CURLPROTO_SCP) if(conn->handler->protocol & CURLPROTO_SCP)
res = scp_perform(conn, &connected, done); res = scp_perform(conn, &connected, done);

View File

@ -77,12 +77,11 @@ http://%HOSTIP:%HTTPPORT/579 log/ip579
<verify> <verify>
<file name="log/ip579"> <file name="log/ip579">
Progress callback called with UL 0 out of 0 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 0
Progress callback called with UL 8 out of -1 Progress callback called with UL 16 out of 0
Progress callback called with UL 16 out of -1 Progress callback called with UL 26 out of 0
Progress callback called with UL 26 out of -1 Progress callback called with UL 61 out of 0
Progress callback called with UL 61 out of -1 Progress callback called with UL 66 out of 0
Progress callback called with UL 66 out of -1
</file> </file>
</verify> </verify>
</testcase> </testcase>

View File

@ -71,13 +71,15 @@ lib599
HTTP GET with progress callback and redirects changing content sizes HTTP GET with progress callback and redirects changing content sizes
</name> </name>
<command> <command>
http://%HOSTIP:%HTTPPORT/599 http://%HOSTIP:%HTTPPORT/599 log/ip599
</command> </command>
</client> </client>
# #
# Verify data after the test has been "shot" # Verify data after the test has been "shot"
<verify> <verify>
<file name="log/ip599">
CL: -1
</file>
</verify> </verify>
</testcase> </testcase>

View File

@ -43,6 +43,7 @@ int test(char *URL)
{ {
CURL *curl; CURL *curl;
CURLcode res=CURLE_OK; CURLcode res=CURLE_OK;
double content_length = 0.0;
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n"); fprintf(stderr, "curl_global_init() failed\n");
@ -74,6 +75,17 @@ int test(char *URL)
/* Perform the request, res will get the return code */ /* Perform the request, res will get the return code */
res = curl_easy_perform(curl); 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: test_cleanup:
/* always cleanup */ /* always cleanup */