tests/server: use curlx file open/close functions

Replace:
- `open()` with `curlx_open()` (1 call).
- `fopen()` with `curlx_fopen()`.
- `fclose()` with `curlx_fclose()`.

To centralize interacting with the CRT in preparation for using "safe"
alternatives on Windows. This also adds long-filename and Unicode
support for these operations on Windows.

Keep using `open()` in the signal handler to avoid any issues with
calling code not allowed in signal handlers.

Cherry-picked from #19643
Closes #19679
This commit is contained in:
Viktor Szakats 2025-11-24 15:55:17 +01:00
parent 56bfde6554
commit ee97c2a96a
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
8 changed files with 49 additions and 51 deletions

View File

@ -3,8 +3,6 @@
# SPDX-License-Identifier: curl
allowfunc accept
allowfunc fclose
allowfunc fopen
allowfunc fprintf
allowfunc freeaddrinfo
allowfunc getaddrinfo

View File

@ -102,7 +102,7 @@ static int store_incoming(const unsigned char *data, size_t size,
snprintf(dumpfile, sizeof(dumpfile), "%s/dnsd.input", logdir);
/* Open request dump file. */
server = fopen(dumpfile, "ab");
server = curlx_fopen(dumpfile, "ab");
if(!server) {
char errbuf[STRERROR_LEN];
int error = errno;
@ -162,7 +162,7 @@ static int store_incoming(const unsigned char *data, size_t size,
if(*qlen > qbuflen) {
logmsg("dnsd: query too large: %lu > %lu",
(unsigned long)*qlen, (unsigned long)qbuflen);
fclose(server);
curlx_fclose(server);
return -1;
}
memcpy(qbuf, qptr, *qlen);
@ -176,7 +176,7 @@ static int store_incoming(const unsigned char *data, size_t size,
fprintf(server, "\n");
#endif
fclose(server);
curlx_fclose(server);
return 0;
}
@ -325,7 +325,7 @@ static void read_instructions(void)
char file[256];
FILE *f;
snprintf(file, sizeof(file), "%s/" INSTRUCTIONS, logdir);
f = fopen(file, FOPEN_READTEXT);
f = curlx_fopen(file, FOPEN_READTEXT);
if(f) {
char buf[256];
ancount_aaaa = ancount_a = 0;
@ -375,7 +375,7 @@ static void read_instructions(void)
}
}
}
fclose(f);
curlx_fclose(f);
}
else
logmsg("Error opening file '%s'", file);

View File

@ -73,7 +73,7 @@ static void mqttd_resetdefaults(void)
static void mqttd_getconfig(void)
{
FILE *fp = fopen(configfile, FOPEN_READTEXT);
FILE *fp = curlx_fopen(configfile, FOPEN_READTEXT);
mqttd_resetdefaults();
if(fp) {
char buffer[512];
@ -119,7 +119,7 @@ static void mqttd_getconfig(void)
}
}
}
fclose(fp);
curlx_fclose(fp);
}
else {
logmsg("No config file '%s' to read", configfile);
@ -430,7 +430,7 @@ static curl_socket_t mqttit(curl_socket_t fd)
0x04 /* protocol level */
};
snprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
dump = fopen(dumpfile, "ab");
dump = curlx_fopen(dumpfile, "ab");
if(!dump)
goto end;
@ -636,9 +636,9 @@ end:
if(buffer)
free(buffer);
if(dump)
fclose(dump);
curlx_fclose(dump);
if(stream)
fclose(stream);
curlx_fclose(stream);
return CURL_SOCKET_BAD;
}

View File

@ -255,7 +255,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
/* get the custom server control "commands" */
int error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
req->open = FALSE; /* closes connection */
@ -557,7 +557,7 @@ static void rtspd_storerequest(char *reqbuf, size_t totalsize)
return;
do {
dump = fopen(dumpfile, "ab");
dump = curlx_fopen(dumpfile, "ab");
/* !checksrc! disable ERRNOVAR 1 */
} while(!dump && ((error = errno) == EINTR));
if(!dump) {
@ -589,7 +589,7 @@ static void rtspd_storerequest(char *reqbuf, size_t totalsize)
storerequest_cleanup:
res = fclose(dump);
res = curlx_fclose(dump);
if(res)
logmsg("Error closing file %s error (%d) %s", dumpfile,
errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
@ -815,7 +815,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
}
else {
error = getpart(&ptr, &count, "reply", partbuf, stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
return 0;
@ -841,7 +841,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
else {
/* get the custom server control "commands" */
error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
free(ptr);
@ -870,7 +870,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
else
rtspd_prevbounce = FALSE;
dump = fopen(responsedump, "ab");
dump = curlx_fopen(responsedump, "ab");
if(!dump) {
error = errno;
logmsg("fopen() failed with error (%d) %s",
@ -928,7 +928,7 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
req->rtp_buffersize = 0;
}
res = fclose(dump);
res = curlx_fclose(dump);
if(res)
logmsg("Error closing file %s error (%d) %s", responsedump,
errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));

View File

@ -108,7 +108,7 @@ static void socksd_resetdefaults(void)
static void socksd_getconfig(void)
{
FILE *fp = fopen(configfile, FOPEN_READTEXT);
FILE *fp = curlx_fopen(configfile, FOPEN_READTEXT);
socksd_resetdefaults();
if(fp) {
char buffer[512];
@ -180,7 +180,7 @@ static void socksd_getconfig(void)
}
}
}
fclose(fp);
curlx_fclose(fp);
}
}
@ -470,7 +470,7 @@ static curl_socket_t sockit(curl_socket_t fd)
{
FILE *dump;
dump = fopen(reqlogfile, "ab");
dump = curlx_fopen(reqlogfile, "ab");
if(dump) {
int i;
fprintf(dump, "atyp %u =>", type);
@ -493,7 +493,7 @@ static curl_socket_t sockit(curl_socket_t fd)
fprintf(dump, "\n");
break;
}
fclose(dump);
curlx_fclose(dump);
}
}

View File

@ -178,7 +178,7 @@ static bool socket_domain_is_ip(void)
/* parse the file on disk that might have a test number for us */
static int parse_cmdfile(struct sws_httprequest *req)
{
FILE *f = fopen(cmdfile, FOPEN_READTEXT);
FILE *f = curlx_fopen(cmdfile, FOPEN_READTEXT);
if(f) {
int testnum = DOCNUMBER_NOTHING;
char buf[256];
@ -188,7 +188,7 @@ static int parse_cmdfile(struct sws_httprequest *req)
req->testno = testnum;
}
}
fclose(f);
curlx_fclose(f);
}
return 0;
}
@ -220,7 +220,7 @@ static int sws_parse_servercmd(struct sws_httprequest *req)
/* get the custom server control "commands" */
error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
req->open = FALSE; /* closes connection */
@ -734,7 +734,7 @@ static void sws_storerequest(const char *reqbuf, size_t totalsize)
return;
do {
dump = fopen(dumpfile, "ab");
dump = curlx_fopen(dumpfile, "ab");
/* !checksrc! disable ERRNOVAR 1 */
} while(!dump && ((error = errno) == EINTR));
if(!dump) {
@ -766,7 +766,7 @@ static void sws_storerequest(const char *reqbuf, size_t totalsize)
storerequest_cleanup:
res = fclose(dump);
res = curlx_fclose(dump);
if(res)
logmsg("Error closing file %s error (%d) %s", dumpfile,
errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
@ -1050,7 +1050,7 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
}
else {
error = getpart(&ptr, &count, "reply", partbuf, stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
return 0;
@ -1075,7 +1075,7 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
else {
/* get the custom server control "commands" */
error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
free(ptr);
@ -1104,7 +1104,7 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
else
sws_prevbounce = FALSE;
dump = fopen(responsedump, "ab");
dump = curlx_fopen(responsedump, "ab");
if(!dump) {
error = errno;
logmsg("fopen() failed with error (%d) %s",
@ -1158,7 +1158,7 @@ retry:
}
} while((count > 0) && !got_exit_signal);
res = fclose(dump);
res = curlx_fclose(dump);
if(res)
logmsg("Error closing file %s error (%d) %s", responsedump,
errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));

View File

@ -445,13 +445,13 @@ static ssize_t write_behind(struct testcase *test, int convert)
if(!test->ofile) {
char outfile[256];
snprintf(outfile, sizeof(outfile), "%s/upload.%ld", logdir, test->testno);
test->ofile = open(outfile, O_CREAT | O_RDWR | CURL_O_BINARY,
test->ofile = curlx_open(outfile, O_CREAT | O_RDWR | CURL_O_BINARY,
#ifdef _WIN32
S_IREAD | S_IWRITE
S_IREAD | S_IWRITE
#else
S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IWGRP | S_IXGRP |
S_IROTH | S_IWOTH | S_IXOTH
S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IWGRP | S_IXGRP |
S_IROTH | S_IWOTH | S_IXOTH
#endif
);
if(test->ofile == -1) {
@ -910,7 +910,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
snprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
/* Open request dump file. */
server = fopen(dumpfile, "ab");
server = curlx_fopen(dumpfile, "ab");
if(!server) {
char errbuf[STRERROR_LEN];
int error = errno;
@ -963,7 +963,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
if(*cp || !mode) {
nak(TFTP_EBADOP);
fclose(server);
curlx_fclose(server);
return 3;
}
@ -975,7 +975,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
*cp = (char)tolower((int)*cp);
/* store input protocol */
fclose(server);
curlx_fclose(server);
for(pf = formata; pf->f_mode; pf++)
if(strcmp(pf->f_mode, mode) == 0)
@ -1036,7 +1036,7 @@ static int tftpd_parse_servercmd(struct testcase *req)
/* get the custom server control "commands" */
error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
return 1; /* done */
@ -1155,7 +1155,7 @@ static int validate_access(struct testcase *test,
else {
size_t count;
int error = getpart(&test->buffer, &count, "reply", partbuf, stream);
fclose(stream);
curlx_fclose(stream);
if(error) {
logmsg("getpart() failed with error (%d)", error);
return TFTP_EACCESS;

View File

@ -115,12 +115,12 @@ void logmsg(const char *msg, ...)
va_end(ap);
do {
logfp = fopen(serverlogfile, "ab");
logfp = curlx_fopen(serverlogfile, "ab");
/* !checksrc! disable ERRNOVAR 1 */
} while(!logfp && (errno == EINTR));
if(logfp) {
fprintf(logfp, "%s %s\n", timebuf, buffer);
fclose(logfp);
curlx_fclose(logfp);
}
else {
char errbuf[STRERROR_LEN];
@ -193,7 +193,7 @@ FILE *test2fopen(long testno, const char *logdir2)
char filename[256];
/* first try the alternative, preprocessed, file */
snprintf(filename, sizeof(filename), "%s/test%ld", logdir2, testno);
stream = fopen(filename, "rb");
stream = curlx_fopen(filename, "rb");
return stream;
}
@ -224,7 +224,7 @@ int write_pidfile(const char *filename)
curl_off_t pid;
pid = our_getpid();
pidfile = fopen(filename, "wb");
pidfile = curlx_fopen(filename, "wb");
if(!pidfile) {
char errbuf[STRERROR_LEN];
logmsg("Could not write pid file: %s (%d) %s", filename,
@ -232,7 +232,7 @@ int write_pidfile(const char *filename)
return 0; /* fail */
}
fprintf(pidfile, "%ld\n", (long)pid);
fclose(pidfile);
curlx_fclose(pidfile);
logmsg("Wrote pid %ld to %s", (long)pid, filename);
return 1; /* success */
}
@ -240,7 +240,7 @@ int write_pidfile(const char *filename)
/* store the used port number in a file */
int write_portfile(const char *filename, int port)
{
FILE *portfile = fopen(filename, "wb");
FILE *portfile = curlx_fopen(filename, "wb");
if(!portfile) {
char errbuf[STRERROR_LEN];
logmsg("Could not write port file: %s (%d) %s", filename,
@ -248,7 +248,7 @@ int write_portfile(const char *filename, int port)
return 0; /* fail */
}
fprintf(portfile, "%d\n", port);
fclose(portfile);
curlx_fclose(portfile);
logmsg("Wrote port %d to %s", port, filename);
return 1; /* success */
}
@ -261,7 +261,7 @@ void set_advisor_read_lock(const char *filename)
int res;
do {
lockfile = fopen(filename, "wb");
lockfile = curlx_fopen(filename, "wb");
/* !checksrc! disable ERRNOVAR 1 */
} while(!lockfile && ((error = errno) == EINTR));
if(!lockfile) {
@ -270,7 +270,7 @@ void set_advisor_read_lock(const char *filename)
return;
}
res = fclose(lockfile);
res = curlx_fclose(lockfile);
if(res)
logmsg("Error closing lock file %s error (%d) %s", filename,
errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));