examples: fix two issues found by CodeQL

- http2-upload: use `fstat()` to query file length to fix TOCTOU.

- ftpuploadresume: fix checking `sscanf()` return value.

Follow-up to b4922b1295 #18564
Closes #18605
This commit is contained in:
Viktor Szakats 2025-09-18 18:50:09 +02:00
parent ce354d0f4d
commit f13250edf1
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
2 changed files with 17 additions and 11 deletions

View file

@ -38,7 +38,7 @@ static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
long len = 0;
r = sscanf(ptr, "Content-Length: %ld\n", &len);
if(r)
if(r == 1)
*((long *) stream) = len;
return size * nmemb;

View file

@ -45,6 +45,9 @@
#ifdef _WIN32
#undef stat
#define stat _stat
#undef fstat
#define fstat _fstat
#define fileno _fileno
#endif
/* curl stuff */
@ -223,16 +226,6 @@ static int setup(struct input *i, int num, const char *upload)
curl_msnprintf(url, 256, "https://localhost:8443/upload-%d", num);
/* get the file size of the local file */
if(stat(upload, &file_info)) {
fprintf(stderr, "error: could not stat file %s: %s\n", upload,
strerror(errno));
fclose(out);
return 1;
}
uploadsize = file_info.st_size;
i->in = fopen(upload, "rb");
if(!i->in) {
fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
@ -241,6 +234,19 @@ static int setup(struct input *i, int num, const char *upload)
return 1;
}
#ifdef UNDER_CE
if(stat(upload, &file_info) != 0) {
#else
if(fstat(fileno(i->in), &file_info) != 0) {
#endif
fprintf(stderr, "error: could not stat file %s: %s\n", upload,
strerror(errno));
fclose(out);
return 1;
}
uploadsize = file_info.st_size;
hnd = i->hnd = curl_easy_init();
/* write to this file */