git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/3] http: offer to cast `size_t` to `curl_off_t` safely

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 26, 2025, 10:32 UTC
Message-ID
<114eb9c8ecfbc5be33edf697ffc05fed6444beb7.1758882772.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1974.v2.git.1758882772.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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 <johannes.schindelin@gmx.de>
---
 http.h        | 10 ++++++++++
 remote-curl.c | 14 +++-----------
 2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/http.h b/http.h
index 36202139f4..0a36dbd294 100644
--- a/http.h
+++ b/http.h
@@ -8,6 +8,7 @@ struct packed_git;
 #include <curl/curl.h>
 #include <curl/easy.h>
 
+#include "gettext.h"
 #include "strbuf.h"
 #include "remote.h"
 
@@ -95,6 +96,15 @@ static inline int missing__target(int code, int result)
 
 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
 
+static inline curl_off_t cast_size_t_to_curl_off_t(size_t a)
+{
+	uintmax_t size = a;
+	if (size > maximum_signed_value_of_type(curl_off_t))
+		die(_("number too large to represent as curl_off_t "
+		      "on this platform: %"PRIuMAX), (uintmax_t)a);
+	return (curl_off_t)a;
+}
+
 /*
  * Normalize curl results to handle CURL_FAILONERROR (or lack thereof). Failing
  * http codes have their "result" converted to CURLE_HTTP_RETURNED_ERROR, and
diff --git a/remote-curl.c b/remote-curl.c
index 84f4694780..69f919454a 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -894,14 +894,6 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
 	return err;
 }
 
-static curl_off_t xcurl_off_t(size_t len)
-{
-	uintmax_t size = len;
-	if (size > maximum_signed_value_of_type(curl_off_t))
-		die(_("cannot handle pushes this big"));
-	return (curl_off_t)size;
-}
-
 /*
  * If flush_received is true, do not attempt to read any more; just use what's
  * in rpc->buf.
@@ -999,7 +991,7 @@ retry:
 		 * and we just need to send it.
 		 */
 		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
-		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
+		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size));
 
 	} else if (use_gzip && 1024 < rpc->len) {
 		/* The client backend isn't giving us compressed data so
@@ -1030,7 +1022,7 @@ retry:
 
 		headers = curl_slist_append(headers, "Content-Encoding: gzip");
 		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
-		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
+		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size));
 
 		if (options.verbosity > 1) {
 			fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
@@ -1043,7 +1035,7 @@ retry:
 		 * more normal Content-Length approach.
 		 */
 		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
-		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
+		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(rpc->len));
 		if (options.verbosity > 1) {
 			fprintf(stderr, "POST %s (%lu bytes)\n",
 				rpc->service_name, (unsigned long)rpc->len);
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 9 of 12 in “Fix curl_easy_setopt() parameter type problem, again”
  1. 0/3 Fix curl_easy_setopt() parameter type problem, againJohannes Schindelin via GitGitGadget, Sep 21, 2025
  2. 1/3 http: offer to cast `size_t` to `curl_off_t` safelyJohannes Schindelin via GitGitGadget, Sep 21, 2025
  3. Junio C HamanoSep 21, 2025
  4. 2/3 imap-send: be more careful when casting to `curl_off_t`Johannes Schindelin via GitGitGadget, Sep 21, 2025
  5. Junio C HamanoSep 21, 2025
  6. 3/3 http-push: avoid new compile errorJohannes Schindelin via GitGitGadget, Sep 21, 2025
  7. Junio C HamanoSep 21, 2025
  8. 0/3 Fix curl_easy_setopt() parameter type problem, againJohannes Schindelin via GitGitGadget, Sep 26, 2025
  9. 1/3 http: offer to cast `size_t` to `curl_off_t` safelyJohannes Schindelin via GitGitGadget, Sep 26, 2025
  10. 2/3 imap-send: be more careful when casting to `curl_off_t`Johannes Schindelin via GitGitGadget, Sep 26, 2025
  11. 3/3 http-push: avoid new compile errorJohannes Schindelin via GitGitGadget, Sep 26, 2025
  12. Junio C HamanoSep 26, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.