snprintf: renamed and we now only use msnprintf()

The function does not return the same value as snprintf() normally does,
so readers may be mislead into thinking the code works differently than
it actually does. A different function name makes this easier to detect.

Reported-by: Tomas Hoger
Assisted-by: Daniel Gustafsson
Fixes #3296
Closes #3297
This commit is contained in:
Daniel Stenberg 2018-11-22 09:01:24 +01:00
parent 9944d6ba33
commit dcd6f81025
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
92 changed files with 632 additions and 631 deletions

View file

@ -6,7 +6,7 @@
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
* Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 2011 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -75,7 +75,7 @@ static char *printable(char *inbuf, size_t inlength)
return NULL;
if(!inlength) {
snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
msnprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
return outbuf;
}
@ -97,7 +97,7 @@ static char *printable(char *inbuf, size_t inlength)
o++;
}
else {
snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
msnprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
o += HEX_STR_LEN;
}
@ -174,7 +174,7 @@ int main(int argc, char *argv[])
}
/* logmsg cannot be used until this file name is set */
snprintf(logfilename, sizeof(logfilename), LOGFILE, testnum);
msnprintf(logfilename, sizeof(logfilename), LOGFILE, testnum);
serverlogfile = logfilename;
logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -382,11 +382,11 @@ static int ProcessRequest(struct httprequest *req)
char *filename;
if((strlen(doc) + strlen(request)) < 200)
snprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
request, doc, prot_str, prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
request, doc, prot_str, prot_major, prot_minor);
else
snprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request %s/%d.%d",
prot_str, prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request %s/%d.%d",
prot_str, prot_major, prot_minor);
logmsg("%s", logbuf);
if(!strncmp("/verifiedserver", ptr, 15)) {
@ -416,8 +416,8 @@ static int ProcessRequest(struct httprequest *req)
else
req->partno = 0;
snprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno);
msnprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno);
logmsg("%s", logbuf);
filename = test2file(req->testno);
@ -540,9 +540,9 @@ static int ProcessRequest(struct httprequest *req)
else {
if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
doc, &prot_major, &prot_minor) == 3) {
snprintf(logbuf, sizeof(logbuf),
"Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf),
"Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
logmsg("%s", logbuf);
if(req->prot_version == 10)
@ -947,12 +947,12 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case DOCNUMBER_WERULEZ:
/* we got a "friends?" question, reply back that we sure are */
logmsg("Identifying ourselves as friends");
snprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
(long)getpid());
msnprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
(long)getpid());
msglen = strlen(msgbuf);
snprintf(weare, sizeof(weare),
"HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
msnprintf(weare, sizeof(weare),
"HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
buffer = weare;
break;
case DOCNUMBER_INTERNAL:
@ -985,7 +985,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
char partbuf[80]="data";
FILE *stream;
if(0 != req->partno)
snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
msnprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
stream = fopen(filename, "rb");
if(!stream) {

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -486,20 +486,20 @@ static void lograw(unsigned char *buffer, ssize_t len)
for(i = 0; i<len; i++) {
switch(ptr[i]) {
case '\n':
snprintf(optr, left, "\\n");
msnprintf(optr, left, "\\n");
width += 2;
optr += 2;
left -= 2;
break;
case '\r':
snprintf(optr, left, "\\r");
msnprintf(optr, left, "\\r");
width += 2;
optr += 2;
left -= 2;
break;
default:
snprintf(optr, left, "%c", (ISGRAPH(ptr[i]) ||
ptr[i] == 0x20) ?ptr[i]:'.');
msnprintf(optr, left, "%c", (ISGRAPH(ptr[i]) ||
ptr[i] == 0x20) ?ptr[i]:'.');
width++;
optr++;
left--;
@ -1062,9 +1062,9 @@ static bool juggle(curl_socket_t *sockfdp,
else if(!memcmp("PORT", buffer, 4)) {
/* Question asking us what PORT number we are listening to.
Replies to PORT with "IPv[num]/[port]" */
snprintf((char *)buffer, sizeof(buffer), "%s/%hu\n", ipv_inuse, port);
msnprintf((char *)buffer, sizeof(buffer), "%s/%hu\n", ipv_inuse, port);
buffer_len = (ssize_t)strlen((char *)buffer);
snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
msnprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
if(!write_stdout(data, 10))
return FALSE;
if(!write_stdout(buffer, buffer_len))
@ -1155,7 +1155,7 @@ static bool juggle(curl_socket_t *sockfdp,
nread_socket = sread(sockfd, buffer, sizeof(buffer));
if(nread_socket > 0) {
snprintf(data, sizeof(data), "DATA\n%04zx\n", nread_socket);
msnprintf(data, sizeof(data), "DATA\n%04zx\n", nread_socket);
if(!write_stdout(data, 10))
return FALSE;
if(!write_stdout(buffer, nread_socket))

View file

@ -509,11 +509,11 @@ static int ProcessRequest(struct httprequest *req)
/* get the number after it */
if(ptr) {
if((strlen(doc) + strlen(request)) < 400)
snprintf(logbuf, sizeof(logbuf), "Got request: %s %s HTTP/%d.%d",
request, doc, prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf), "Got request: %s %s HTTP/%d.%d",
request, doc, prot_major, prot_minor);
else
snprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request HTTP/%d.%d",
prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request HTTP/%d.%d",
prot_major, prot_minor);
logmsg("%s", logbuf);
if(!strncmp("/verifiedserver", ptr, 15)) {
@ -545,8 +545,8 @@ static int ProcessRequest(struct httprequest *req)
if(req->testno) {
snprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno);
msnprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno);
logmsg("%s", logbuf);
/* find and parse <servercmd> for this test */
@ -565,9 +565,9 @@ static int ProcessRequest(struct httprequest *req)
doc, &prot_major, &prot_minor) == 3) {
char *portp = NULL;
snprintf(logbuf, sizeof(logbuf),
"Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
msnprintf(logbuf, sizeof(logbuf),
"Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
logmsg("%s", logbuf);
req->connect_request = TRUE;
@ -645,9 +645,9 @@ static int ProcessRequest(struct httprequest *req)
else
req->partno = 0;
snprintf(logbuf, sizeof(logbuf),
"Requested test number %ld part %ld (from host name)",
req->testno, req->partno);
msnprintf(logbuf, sizeof(logbuf),
"Requested test number %ld part %ld (from host name)",
req->testno, req->partno);
logmsg("%s", logbuf);
}
@ -696,9 +696,9 @@ static int ProcessRequest(struct httprequest *req)
else
req->partno = 0;
snprintf(logbuf, sizeof(logbuf),
"Requested GOPHER test number %ld part %ld",
req->testno, req->partno);
msnprintf(logbuf, sizeof(logbuf),
"Requested GOPHER test number %ld part %ld",
req->testno, req->partno);
logmsg("%s", logbuf);
}
}
@ -1117,14 +1117,14 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case DOCNUMBER_WERULEZ:
/* we got a "friends?" question, reply back that we sure are */
logmsg("Identifying ourselves as friends");
snprintf(msgbuf, sizeof(msgbuf), "WE ROOLZ: %ld\r\n", (long)getpid());
msnprintf(msgbuf, sizeof(msgbuf), "WE ROOLZ: %ld\r\n", (long)getpid());
msglen = strlen(msgbuf);
if(use_gopher)
snprintf(weare, sizeof(weare), "%s", msgbuf);
msnprintf(weare, sizeof(weare), "%s", msgbuf);
else
snprintf(weare, sizeof(weare),
"HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
msnprintf(weare, sizeof(weare),
"HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
buffer = weare;
break;
case DOCNUMBER_404:
@ -1145,9 +1145,9 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
const char *section = req->connect_request?"connect":"data";
if(req->partno)
snprintf(partbuf, sizeof(partbuf), "%s%ld", section, req->partno);
msnprintf(partbuf, sizeof(partbuf), "%s%ld", section, req->partno);
else
snprintf(partbuf, sizeof(partbuf), "%s", section);
msnprintf(partbuf, sizeof(partbuf), "%s", section);
logmsg("Send response test%ld section <%s>", req->testno, partbuf);
@ -2118,7 +2118,7 @@ int main(int argc, char *argv[])
}
}
snprintf(port_str, sizeof(port_str), "port %hu", port);
msnprintf(port_str, sizeof(port_str), "port %hu", port);
#ifdef WIN32
win32_init();

View file

@ -15,7 +15,7 @@
*/
/*
* Copyright (c) 1983, 2016 Regents of the University of California.
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -569,7 +569,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
if(!test->ofile) {
char outfile[256];
snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
msnprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
#ifdef WIN32
test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777);
#else
@ -1141,8 +1141,8 @@ static int validate_access(struct testcase *test,
if(!strncmp("verifiedserver", filename, 14)) {
char weare[128];
size_t count = snprintf(weare, sizeof(weare),
"WE ROOLZ: %ld\r\n", (long)getpid());
size_t count = msnprintf(weare, sizeof(weare),
"WE ROOLZ: %ld\r\n", (long)getpid());
logmsg("Are-we-friendly question received");
test->buffer = strdup(weare);
@ -1187,7 +1187,7 @@ static int validate_access(struct testcase *test,
file = test2file(testno);
if(0 != partno)
snprintf(partbuf, sizeof(partbuf), "data%ld", partno);
msnprintf(partbuf, sizeof(partbuf), "data%ld", partno);
if(file) {
FILE *stream = fopen(file, "rb");

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -87,7 +87,7 @@ char *data_to_hex(char *data, size_t len)
if((data[i] >= 0x20) && (data[i] < 0x7f))
*optr++ = *iptr++;
else {
snprintf(optr, 4, "%%%02x", *iptr++);
msnprintf(optr, 4, "%%%02x", *iptr++);
optr += 3;
}
}
@ -121,11 +121,12 @@ void logmsg(const char *msg, ...)
sec = epoch_offset + tv.tv_sec;
now = localtime(&sec); /* not thread safe but we don't care */
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
(int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec, (long)tv.tv_usec);
msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
(int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec,
(long)tv.tv_usec);
va_start(ap, msg);
vsnprintf(buffer, sizeof(buffer), msg, ap);
mvsnprintf(buffer, sizeof(buffer), msg, ap);
va_end(ap);
logfp = fopen(serverlogfile, "ab");
@ -151,7 +152,7 @@ void win32_perror(const char *msg)
if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
LANG_NEUTRAL, buf, sizeof(buf), NULL))
snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
msnprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
if(msg)
fprintf(stderr, "%s: ", msg);
fprintf(stderr, "%s\n", buf);
@ -195,7 +196,7 @@ const char *path = ".";
char *test2file(long testno)
{
static char filename[256];
snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
msnprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
return filename;
}