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
This commit is contained in:
Daniel Stenberg 2025-10-17 14:41:08 +02:00
parent c0564ceb3a
commit 4fb12f2891
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 19 additions and 19 deletions

View 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

View 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;
}

View 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,