]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
mime: fix use of fseek()
authorDaniel Stenberg <daniel@haxx.se>
Fri, 17 Oct 2025 12:41:08 +0000 (14:41 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 17 Oct 2025 14:13:35 +0000 (16:13 +0200)
Avoid the possible 64-bit offset truncation when used on systems with
small 'long', like Windows.

bonus: make mime_open_file() return bool

Pointed out by ZeroPath
Closes #19100

lib/formdata.c
lib/mime.c
lib/mime.h

index a98384c18ac91ef621f52ef1c93893e99279cbee..382fd221dce63d6fa03d784ac05d08b6849974dc 100644 (file)
@@ -771,20 +771,6 @@ static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
   return res;
 }
 
-/* wrap call to fseeko so it matches the calling convention of callback */
-static int fseeko_wrapper(void *stream, curl_off_t offset, int whence)
-{
-#if defined(_WIN32) && defined(USE_WIN32_LARGE_FILES)
-  return _fseeki64(stream, (__int64)offset, whence);
-#elif defined(HAVE_FSEEKO) && defined(HAVE_DECL_FSEEKO)
-  return fseeko(stream, (off_t)offset, whence);
-#else
-  if(offset > LONG_MAX)
-    return -1;
-  return fseek(stream, (long)offset, whence);
-#endif
-}
-
 /*
  * Curl_getformdata() converts a linked list of "meta data" into a mime
  * structure. The input list is in 'post', while the output is stored in
@@ -874,7 +860,7 @@ CURLcode Curl_getformdata(CURL *data,
 #endif
             result = curl_mime_data_cb(part, (curl_off_t) -1,
                                        (curl_read_callback) fread,
-                                       fseeko_wrapper,
+                                       Curl_fseeko,
                                        NULL, (void *) stdin);
 #if defined(__clang__) && __clang_major__ >= 16
 #pragma clang diagnostic pop
index b403d29b1fd9d0a1037400da5fcc11c038add98d..baca30049f73bb9f13d6866823c7c52275eadf55 100644 (file)
@@ -271,6 +271,19 @@ static char *Curl_basename(char *path)
 #define basename(x)  Curl_basename((x))
 #endif
 
+int Curl_fseeko(void *stream, curl_off_t offset, int whence)
+{
+#if defined(_WIN32) && defined(USE_WIN32_LARGE_FILES)
+  return _fseeki64(stream, (__int64)offset, whence);
+#elif defined(HAVE_FSEEKO) && defined(HAVE_DECL_FSEEKO)
+  return fseeko(stream, (off_t)offset, whence);
+#else
+  if(offset > LONG_MAX)
+    return -1;
+  return fseek(stream, (long)offset, whence);
+#endif
+}
+
 
 /* Set readback state. */
 static void mimesetstate(struct mime_state *state,
@@ -703,14 +716,14 @@ static void mime_mem_free(void *ptr)
 
 /* Named file callbacks. */
 /* Argument is a pointer to the mime part. */
-static int mime_open_file(curl_mimepart *part)
+static bool mime_open_file(curl_mimepart *part)
 {
   /* Open a MIMEKIND_FILE part. */
 
   if(part->fp)
-    return 0;
+    return FALSE;
   part->fp = fopen_read(part->data, "rb");
-  return part->fp ? 0 : -1;
+  return part->fp ? FALSE : TRUE;
 }
 
 static size_t mime_file_read(char *buffer, size_t size, size_t nitems,
@@ -737,7 +750,7 @@ static int mime_file_seek(void *instream, curl_off_t offset, int whence)
   if(mime_open_file(part))
     return CURL_SEEKFUNC_FAIL;
 
-  return fseek(part->fp, (long) offset, whence) ?
+  return Curl_fseeko(part->fp, offset, whence) ?
     CURL_SEEKFUNC_CANTSEEK : CURL_SEEKFUNC_OK;
 }
 
index 5073a38f709a03b8c910a477303f44a13d973c23..dc6f0d4af176d8dc713380fd74f86c40f8cf758d 100644 (file)
@@ -138,6 +138,7 @@ CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
                                     !defined(CURL_DISABLE_IMAP))
 
 /* Prototypes. */
+int Curl_fseeko(void *stream, curl_off_t offset, int whence);
 void Curl_mime_initpart(struct curl_mimepart *part);
 void Curl_mime_cleanpart(struct curl_mimepart *part);
 CURLcode Curl_mime_duppart(struct Curl_easy *data,