mirror of
https://github.com/curl/curl.git
synced 2026-07-30 07:48:02 +03:00
examples: fix two more cases of stat() TOCTOU
Also:
- ftpupload: bump an intermediate variable size.
Follow-up to f13250edf1 #18605
Closes #18778
This commit is contained in:
parent
5b086ba188
commit
c478c7efdf
2 changed files with 35 additions and 17 deletions
|
|
@ -37,6 +37,9 @@
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#undef stat
|
#undef stat
|
||||||
#define stat _stat
|
#define stat _stat
|
||||||
|
#undef fstat
|
||||||
|
#define fstat _fstat
|
||||||
|
#define fileno _fileno
|
||||||
#else
|
#else
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -78,25 +81,31 @@ int main(void)
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
FILE *hd_src;
|
FILE *hd_src;
|
||||||
struct stat file_info;
|
struct stat file_info;
|
||||||
unsigned long fsize;
|
curl_off_t fsize;
|
||||||
|
|
||||||
struct curl_slist *headerlist = NULL;
|
struct curl_slist *headerlist = NULL;
|
||||||
static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
|
static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
|
||||||
static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
|
static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
|
||||||
|
|
||||||
/* get the file size of the local file */
|
/* get a FILE * of the file */
|
||||||
if(stat(LOCAL_FILE, &file_info)) {
|
|
||||||
printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
fsize = (unsigned long)file_info.st_size;
|
|
||||||
|
|
||||||
printf("Local file size: %lu bytes.\n", fsize);
|
|
||||||
|
|
||||||
/* get a FILE * of the same file */
|
|
||||||
hd_src = fopen(LOCAL_FILE, "rb");
|
hd_src = fopen(LOCAL_FILE, "rb");
|
||||||
if(!hd_src)
|
if(!hd_src) {
|
||||||
|
printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno));
|
||||||
return 2;
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* to get the file size */
|
||||||
|
#ifdef UNDER_CE
|
||||||
|
if(stat(LOCAL_FILE, &file_info) != 0) {
|
||||||
|
#else
|
||||||
|
if(fstat(fileno(hd_src), &file_info) != 0) {
|
||||||
|
#endif
|
||||||
|
fclose(hd_src);
|
||||||
|
return 1; /* cannot continue */
|
||||||
|
}
|
||||||
|
fsize = file_info.st_size;
|
||||||
|
|
||||||
|
printf("Local file size: %lu bytes.\n", (unsigned long)fsize);
|
||||||
|
|
||||||
/* In Windows, this inits the Winsock stuff */
|
/* In Windows, this inits the Winsock stuff */
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
@ -127,8 +136,7 @@ int main(void)
|
||||||
option you MUST make sure that the type of the passed-in argument is a
|
option you MUST make sure that the type of the passed-in argument is a
|
||||||
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
|
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
|
||||||
make sure that to pass in a type 'long' argument. */
|
make sure that to pass in a type 'long' argument. */
|
||||||
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, fsize);
|
||||||
(curl_off_t)fsize);
|
|
||||||
|
|
||||||
/* Now run off and do what you have been told! */
|
/* Now run off and do what you have been told! */
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,9 @@
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#undef stat
|
#undef stat
|
||||||
#define stat _stat
|
#define stat _stat
|
||||||
|
#undef fstat
|
||||||
|
#define fstat _fstat
|
||||||
|
#define fileno _fileno
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -79,9 +82,6 @@ int main(int argc, char **argv)
|
||||||
file = argv[1];
|
file = argv[1];
|
||||||
url = argv[2];
|
url = argv[2];
|
||||||
|
|
||||||
/* get the file size of the local file */
|
|
||||||
stat(file, &file_info);
|
|
||||||
|
|
||||||
/* get a FILE * of the same file, could also be made with
|
/* get a FILE * of the same file, could also be made with
|
||||||
fdopen() from the previous descriptor, but hey this is just
|
fdopen() from the previous descriptor, but hey this is just
|
||||||
an example! */
|
an example! */
|
||||||
|
|
@ -89,6 +89,16 @@ int main(int argc, char **argv)
|
||||||
if(!hd_src)
|
if(!hd_src)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
|
/* get the file size of the local file */
|
||||||
|
#ifdef UNDER_CE
|
||||||
|
if(stat(file, &file_info) != 0) {
|
||||||
|
#else
|
||||||
|
if(fstat(fileno(hd_src), &file_info) != 0) {
|
||||||
|
#endif
|
||||||
|
fclose(hd_src);
|
||||||
|
return 1; /* cannot continue */
|
||||||
|
}
|
||||||
|
|
||||||
/* In Windows, this inits the Winsock stuff */
|
/* In Windows, this inits the Winsock stuff */
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue