avoid sprintf

This commit is contained in:
Christian Grothoff
2018-11-18 12:21:02 +01:00
parent 49936d88d9
commit 488615cea6
9 changed files with 141 additions and 59 deletions
+20 -6
View File
@@ -207,7 +207,10 @@ testInternalGet (int port, int poll_flag)
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
start_timer ();
for (i=0;i<ROUNDS;i++)
{
@@ -278,7 +281,10 @@ testMultithreadedGet (int port, int poll_flag)
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
start_timer ();
for (i=0;i<ROUNDS;i++)
{
@@ -350,7 +356,10 @@ testMultithreadedPoolGet (int port, int poll_flag)
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
start_timer ();
for (i=0;i<ROUNDS;i++)
{
@@ -424,8 +433,10 @@ testExternalGet (int port)
cbc.buf = buf;
cbc.size = 2048;
d = MHD_start_daemon (MHD_USE_ERROR_LOG,
port, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
if (d == NULL)
port, NULL, NULL,
&ahc_echo, "GET",
MHD_OPTION_END);
if (NULL == d)
return 256;
if (0 == port)
{
@@ -435,7 +446,10 @@ testExternalGet (int port)
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
start_timer ();
multi = curl_multi_init ();
if (multi == NULL)
+4 -2
View File
@@ -218,8 +218,10 @@ do_gets (void * param)
int port = (int)(intptr_t)param;
char *err = NULL;
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
for (j=0;j<PAR;j++)
{
if (0 != pthread_create(&par[j], NULL, &thread_gets, (void*)url))
+4 -1
View File
@@ -169,7 +169,10 @@ do_gets (void * param)
char url[64];
int port = (int)(intptr_t)param;
sprintf(url, "http://127.0.0.1:%d/hello_world", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hello_world",
port);
for (j=0;j<PAR;j++)
{
+53 -28
View File
@@ -57,8 +57,12 @@ struct CBC
size_t size;
};
static size_t
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
copyBuffer (void *ptr,
size_t size,
size_t nmemb,
void *ctx)
{
struct CBC *cbc = ctx;
@@ -69,6 +73,7 @@ copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
return size * nmemb;
}
static int
ahc_echo (void *cls,
struct MHD_Connection *connection,
@@ -95,30 +100,33 @@ ahc_echo (void *cls,
response = MHD_create_response_from_buffer (strlen (DENIED),
DENIED,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_auth_fail_response(connection, realm,
MY_OPAQUE,
response,
MHD_NO);
ret = MHD_queue_auth_fail_response (connection,
realm,
MY_OPAQUE,
response,
MHD_NO);
MHD_destroy_response(response);
return ret;
}
ret = MHD_digest_auth_check(connection, realm,
username,
password,
300);
free(username);
ret = MHD_digest_auth_check (connection,
realm,
username,
password,
300);
free (username);
if ( (ret == MHD_INVALID_NONCE) ||
(ret == MHD_NO) )
{
response = MHD_create_response_from_buffer(strlen (DENIED),
DENIED,
MHD_RESPMEM_PERSISTENT);
response = MHD_create_response_from_buffer (strlen (DENIED),
DENIED,
MHD_RESPMEM_PERSISTENT);
if (NULL == response)
return MHD_NO;
ret = MHD_queue_auth_fail_response(connection, realm,
MY_OPAQUE,
response,
(ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
ret = MHD_queue_auth_fail_response (connection,
realm,
MY_OPAQUE,
response,
(MHD_INVALID_NONCE == ret) ? MHD_YES : MHD_NO);
MHD_destroy_response(response);
return ret;
}
@@ -159,7 +167,8 @@ testDigestAuth ()
cbc.size = 2048;
cbc.pos = 0;
#ifndef WINDOWS
fd = open("/dev/urandom", O_RDONLY);
fd = open ("/dev/urandom",
O_RDONLY);
if (-1 == fd)
{
fprintf (stderr,
@@ -170,7 +179,9 @@ testDigestAuth ()
}
while (off < 8)
{
len = read(fd, rnd, 8);
len = read (fd,
rnd,
8);
if (len == (size_t)-1)
{
fprintf (stderr,
@@ -187,18 +198,25 @@ testDigestAuth ()
{
HCRYPTPROV cc;
BOOL b;
b = CryptAcquireContext (&cc, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
b = CryptAcquireContext (&cc,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT);
if (b == 0)
{
fprintf (stderr, "Failed to acquire crypto provider context: %lu\n",
GetLastError ());
fprintf (stderr,
"Failed to acquire crypto provider context: %lu\n",
GetLastError ());
return 1;
}
b = CryptGenRandom (cc, 8, (BYTE*)rnd);
if (b == 0)
{
fprintf (stderr, "Failed to generate 8 random bytes: %lu\n",
GetLastError ());
fprintf (stderr,
"Failed to generate 8 random bytes: %lu\n",
GetLastError ());
}
CryptReleaseContext (cc, 0);
if (b == 0)
@@ -206,7 +224,8 @@ testDigestAuth ()
}
#endif
d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
port, NULL, NULL, &ahc_echo, PAGE,
port, NULL, NULL,
&ahc_echo, PAGE,
MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd,
MHD_OPTION_NONCE_NC_SIZE, 300,
MHD_OPTION_END);
@@ -215,9 +234,15 @@ testDigestAuth ()
if (0 == port)
{
const union MHD_DaemonInfo *dinfo;
dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
if (NULL == dinfo || 0 == dinfo->port)
{ MHD_stop_daemon (d); return 32; }
dinfo = MHD_get_daemon_info (d,
MHD_DAEMON_INFO_BIND_PORT);
if ( (NULL == dinfo) ||
(0 == dinfo->port) )
{
MHD_stop_daemon (d);
return 32;
}
port = (int)dinfo->port;
}
snprintf (url,
+16 -4
View File
@@ -182,7 +182,10 @@ testInternalGet ()
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/",
port);
curl = fork_curl (url);
(void)sleep (1);
kill_curl (curl);
@@ -227,7 +230,10 @@ testMultithreadedGet ()
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/",
port);
//fprintf (stderr, "Forking cURL!\n");
curl = fork_curl (url);
(void)sleep (1);
@@ -283,7 +289,10 @@ testMultithreadedPoolGet ()
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/",
port);
curl = fork_curl (url);
(void)sleep (1);
kill_curl (curl);
@@ -332,7 +341,10 @@ testExternalGet ()
{ MHD_stop_daemon (d); return 32; }
port = (int)dinfo->port;
}
sprintf(url, "http://127.0.0.1:%d/", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/",
port);
curl = fork_curl (url);
start = time (NULL);
+6 -5
View File
@@ -584,11 +584,12 @@ main (int argc, char *const *argv)
(NULL == (tmp = getenv ("TEMP"))) )
tmp = "/tmp";
sourcefile = malloc (strlen (tmp) + 32);
sprintf (sourcefile,
"%s/%s%s",
tmp,
"test-mhd-sendfile",
oneone ? "11" : "");
snprintf (sourcefile,
strlen (tmp) + 32,
"%s/%s%s",
tmp,
"test-mhd-sendfile",
oneone ? "11" : "");
f = fopen (sourcefile, "w");
if (NULL == f)
{
+27 -9
View File
@@ -93,7 +93,7 @@ ahc_echo (void *cls,
{
if (*mptr != &marker)
abort ();
response = MHD_create_response_from_buffer (2, "OK",
response = MHD_create_response_from_buffer (2, "OK",
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
@@ -150,7 +150,11 @@ testInternalPost ()
c = curl_easy_init ();
cbc.pos = 0;
buf[0] = '\0';
sprintf (url, "http://127.0.0.1:%d/hw%d", port, i);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hw%d",
port,
i);
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
@@ -214,7 +218,9 @@ testMultithreadedPost ()
cbc.buf = buf;
cbc.size = 2048;
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
port, NULL, NULL,
&ahc_echo, NULL,
MHD_OPTION_END);
if (d == NULL)
return 16;
if (0 == port)
@@ -232,7 +238,11 @@ testMultithreadedPost ()
c = curl_easy_init ();
cbc.pos = 0;
buf[0] = '\0';
sprintf (url, "http://127.0.0.1:%d/hw%d", port, i);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hw%d",
port,
i);
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
@@ -315,7 +325,11 @@ testMultithreadedPoolPost ()
c = curl_easy_init ();
cbc.pos = 0;
buf[0] = '\0';
sprintf (url, "http://127.0.0.1:%d/hw%d", port, i);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hw%d",
port,
i);
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
@@ -421,7 +435,11 @@ testExternalPost ()
c = curl_easy_init ();
cbc.pos = 0;
buf[0] = '\0';
sprintf (url, "http://127.0.0.1:%d/hw%d", port, i);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d/hw%d",
port,
i);
curl_easy_setopt (c, CURLOPT_URL, url);
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
@@ -490,7 +508,7 @@ testExternalPost ()
fprintf (stderr,
"select failed: %s\n",
strerror (errno));
break;
break;
}
while (CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform (multi, &running));
@@ -540,11 +558,11 @@ static unsigned long long start_time;
/**
* Get the current timestamp
* Get the current timestamp
*
* @return current time in ms
*/
static unsigned long long
static unsigned long long
now ()
{
struct timeval tv;
+4 -1
View File
@@ -220,7 +220,10 @@ main(void)
{ MHD_stop_daemon (daemon); return 32; }
port = (int)dinfo->port;
}
sprintf(command_line, "curl -s http://127.0.0.1:%d", port);
snprintf (command_line,
sizeof (command_line),
"curl -s http://127.0.0.1:%d",
port);
if (0 != system (command_line))
{
+7 -3
View File
@@ -94,6 +94,8 @@ main (void)
{
struct MHD_Daemon *daemon;
int port;
char url[255];
CURL *curl;
if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
port = 0;
@@ -120,10 +122,12 @@ main (void)
port = (int)dinfo->port;
}
CURL *curl = curl_easy_init ();
curl = curl_easy_init ();
/* curl_easy_setopt(curl, CURLOPT_POST, 1L); */
char url[255];
sprintf (url, "http://127.0.0.1:%d", port);
snprintf (url,
sizeof (url),
"http://127.0.0.1:%d",
port);
curl_easy_setopt (curl, CURLOPT_URL, url);
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);