Skip to content

Commit e4efcd7

Browse files
dschogitster
authored andcommitted
http: offer to cast size_t to curl_off_t safely
This commit moves the `xcurl_off_t()` function, which validates that a given value fits within the `curl_off_t` data type and then casts it, to a more central place so that it can be used outside of `remote-curl.c`, too. At the same time, this function is renamed to conform better with the naming convention of the helper functions that safely cast from one data type to another which has been well established in `git-compat-util.h`. With this move, `gettext.h` must be `#include`d in `http.h` to allow the error message to remain translatable. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f368df4 commit e4efcd7

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

http.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct packed_git;
88
#include <curl/curl.h>
99
#include <curl/easy.h>
1010

11+
#include "gettext.h"
1112
#include "strbuf.h"
1213
#include "remote.h"
1314

@@ -95,6 +96,15 @@ static inline int missing__target(int code, int result)
9596

9697
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
9798

99+
static inline curl_off_t cast_size_t_to_curl_off_t(size_t a)
100+
{
101+
uintmax_t size = a;
102+
if (size > maximum_signed_value_of_type(curl_off_t))
103+
die(_("number too large to represent as curl_off_t "
104+
"on this platform: %"PRIuMAX), (uintmax_t)a);
105+
return (curl_off_t)a;
106+
}
107+
98108
/*
99109
* Normalize curl results to handle CURL_FAILONERROR (or lack thereof). Failing
100110
* http codes have their "result" converted to CURLE_HTTP_RETURNED_ERROR, and

remote-curl.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,6 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
894894
return err;
895895
}
896896

897-
static curl_off_t xcurl_off_t(size_t len)
898-
{
899-
uintmax_t size = len;
900-
if (size > maximum_signed_value_of_type(curl_off_t))
901-
die(_("cannot handle pushes this big"));
902-
return (curl_off_t)size;
903-
}
904-
905897
/*
906898
* If flush_received is true, do not attempt to read any more; just use what's
907899
* in rpc->buf.
@@ -999,7 +991,7 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
999991
* and we just need to send it.
1000992
*/
1001993
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1002-
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
994+
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size));
1003995

1004996
} else if (use_gzip && 1024 < rpc->len) {
1005997
/* The client backend isn't giving us compressed data so
@@ -1030,7 +1022,7 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
10301022

10311023
headers = curl_slist_append(headers, "Content-Encoding: gzip");
10321024
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1033-
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1025+
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size));
10341026

10351027
if (options.verbosity > 1) {
10361028
fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
@@ -1043,7 +1035,7 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
10431035
* more normal Content-Length approach.
10441036
*/
10451037
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1046-
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1038+
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(rpc->len));
10471039
if (options.verbosity > 1) {
10481040
fprintf(stderr, "POST %s (%lu bytes)\n",
10491041
rpc->service_name, (unsigned long)rpc->len);

0 commit comments

Comments
 (0)