From: Daniel Stenberg Date: Fri, 17 Oct 2025 12:41:08 +0000 (+0200) Subject: mime: fix use of fseek() X-Git-Tag: rc-8_17_0-3~143 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fb12f289189e8113967e9c9da09958fd8bfa4cb;p=thirdparty%2Fcurl.git mime: fix use of fseek() 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 --- diff --git a/lib/formdata.c b/lib/formdata.c index a98384c18a..382fd221dc 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -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 diff --git a/lib/mime.c b/lib/mime.c index b403d29b1f..baca30049f 100644 --- a/lib/mime.c +++ b/lib/mime.c @@ -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; } diff --git a/lib/mime.h b/lib/mime.h index 5073a38f70..dc6f0d4af1 100644 --- a/lib/mime.h +++ b/lib/mime.h @@ -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,