deprecate sprint and replace with snprintf

This commit is contained in:
Arnaud Loonstra 2023-01-24 12:54:48 +01:00
parent b674ce68d0
commit f7df6c8599
11 changed files with 41 additions and 31 deletions

View File

@ -129,7 +129,8 @@ static std::string make_address_string (const char *hbuf_,
pos += hbuf_len; pos += hbuf_len;
memcpy (pos, ipv6_suffix_, sizeof ipv6_suffix_ - 1); memcpy (pos, ipv6_suffix_, sizeof ipv6_suffix_ - 1);
pos += sizeof ipv6_suffix_ - 1; pos += sizeof ipv6_suffix_ - 1;
pos += sprintf (pos, "%d", ntohs (port_)); pos += snprintf (pos, max_port_str_length + 1 * sizeof (char), "%d",
ntohs (port_));
return std::string (buf, pos - buf); return std::string (buf, pos - buf);
} }

View File

@ -367,8 +367,8 @@ void zmq::udp_engine_t::sockaddr_to_msg (zmq::msg_t *msg_,
const char *const name = inet_ntoa (addr_->sin_addr); const char *const name = inet_ntoa (addr_->sin_addr);
char port[6]; char port[6];
const int port_len = const int port_len = snprintf (port, 6 * sizeof (char), "%d",
sprintf (port, "%d", static_cast<int> (ntohs (addr_->sin_port))); static_cast<int> (ntohs (addr_->sin_port)));
zmq_assert (port_len > 0); zmq_assert (port_len > 0);
const size_t name_len = strlen (name); const size_t name_len = strlen (name);

View File

@ -155,7 +155,7 @@ void test_connect_before_bind_ctx_term ()
void *connect_socket = test_context_socket (ZMQ_ROUTER); void *connect_socket = test_context_socket (ZMQ_ROUTER);
char ep[32]; char ep[32];
sprintf (ep, "inproc://cbbrr%d", i); snprintf (ep, 32 * sizeof (char), "inproc://cbbrr%d", i);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, ep)); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, ep));
// Cleanup // Cleanup
@ -233,7 +233,7 @@ void test_simultaneous_connect_bind_threads ()
// Set up thread arguments: context followed by endpoint string // Set up thread arguments: context followed by endpoint string
for (unsigned int i = 0; i < no_of_times; ++i) { for (unsigned int i = 0; i < no_of_times; ++i) {
thr_args[i] = (void *) endpts[i]; thr_args[i] = (void *) endpts[i];
sprintf (endpts[i], "inproc://foo_%d", i); snprintf (endpts[i], 20 * sizeof (char), "inproc://foo_%d", i);
} }
// Spawn all threads as simultaneously as possible // Spawn all threads as simultaneously as possible

View File

@ -58,7 +58,7 @@ void test_issue_566 ()
void *dealer = zmq_socket (ctx2, ZMQ_DEALER); void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
// Leave space for NULL char from sprintf, gcc warning // Leave space for NULL char from sprintf, gcc warning
char routing_id[11]; char routing_id[11];
sprintf (routing_id, "%09d", cycle); snprintf (routing_id, 11 * sizeof (char), "%09d", cycle);
TEST_ASSERT_SUCCESS_ERRNO ( TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10)); zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
int rcvtimeo = 1000; int rcvtimeo = 1000;

View File

@ -88,7 +88,8 @@ static void client_task (void *db_)
TEST_ASSERT_SUCCESS_ERRNO ( TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (endpoint, ZMQ_LINGER, &linger, sizeof (linger))); zmq_setsockopt (endpoint, ZMQ_LINGER, &linger, sizeof (linger)));
char endpoint_source[256]; char endpoint_source[256];
sprintf (endpoint_source, "inproc://endpoint%d", databag->id); snprintf (endpoint_source, 256 * sizeof (char), "inproc://endpoint%d",
databag->id);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (endpoint, endpoint_source)); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (endpoint, endpoint_source));
char *my_endpoint = s_recv (endpoint); char *my_endpoint = s_recv (endpoint);
TEST_ASSERT_NOT_NULL (my_endpoint); TEST_ASSERT_NOT_NULL (my_endpoint);
@ -107,7 +108,8 @@ static void client_task (void *db_)
char content[CONTENT_SIZE_MAX] = {}; char content[CONTENT_SIZE_MAX] = {};
// Set random routing id to make tracing easier // Set random routing id to make tracing easier
char routing_id[ROUTING_ID_SIZE] = {}; char routing_id[ROUTING_ID_SIZE] = {};
sprintf (routing_id, "%04X-%04X", rand () % 0xFFFF, rand () % 0xFFFF); snprintf (routing_id, ROUTING_ID_SIZE * sizeof (char), "%04X-%04X",
rand () % 0xFFFF, rand () % 0xFFFF);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt ( TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
client, ZMQ_ROUTING_ID, routing_id, client, ZMQ_ROUTING_ID, routing_id,
ROUTING_ID_SIZE)); // includes '\0' as an helper for printf ROUTING_ID_SIZE)); // includes '\0' as an helper for printf
@ -164,7 +166,8 @@ static void client_task (void *db_)
} }
if (keep_sending) { if (keep_sending) {
sprintf (content, "request #%03d", ++request_nbr); // CONTENT_SIZE snprintf (content, CONTENT_SIZE_MAX * sizeof (char),
"request #%03d", ++request_nbr); // CONTENT_SIZE
if (is_verbose) if (is_verbose)
printf ("client send - routing_id = %s request #%03d\n", printf ("client send - routing_id = %s request #%03d\n",
routing_id, request_nbr); routing_id, request_nbr);
@ -228,7 +231,8 @@ void server_task (void * /*unused_*/)
TEST_ASSERT_NOT_NULL (endpoint_receivers[i]); TEST_ASSERT_NOT_NULL (endpoint_receivers[i]);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt ( TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
endpoint_receivers[i], ZMQ_LINGER, &linger, sizeof (linger))); endpoint_receivers[i], ZMQ_LINGER, &linger, sizeof (linger)));
sprintf (endpoint_source, "inproc://endpoint%d", i); snprintf (endpoint_source, 256 * sizeof (char), "inproc://endpoint%d",
i);
TEST_ASSERT_SUCCESS_ERRNO ( TEST_ASSERT_SUCCESS_ERRNO (
zmq_bind (endpoint_receivers[i], endpoint_source)); zmq_bind (endpoint_receivers[i], endpoint_source));
} }

View File

@ -66,7 +66,11 @@ void make_connect_address (char *connect_address_,
const int port_, const int port_,
const char *bind_address_) const char *bind_address_)
{ {
sprintf (connect_address_, "tcp://%s:%i;%s", ipv6_ ? "[::1]" : "127.0.0.1", if (ipv6_)
snprintf (connect_address_, 30 * sizeof (char), "tcp://[::1]:%i;%s",
port_, strrchr (bind_address_, '/') + 1);
else
snprintf (connect_address_, 38 * sizeof (char), "tcp://127.0.0.1:%i;%s",
port_, strrchr (bind_address_, '/') + 1); port_, strrchr (bind_address_, '/') + 1);
} }

View File

@ -120,7 +120,7 @@ void test_setsockopt_bindtodevice ()
TEST_ASSERT_EQUAL_INT8 ('\0', devname[0]); TEST_ASSERT_EQUAL_INT8 ('\0', devname[0]);
TEST_ASSERT_EQUAL_UINT (1, buflen); TEST_ASSERT_EQUAL_UINT (1, buflen);
sprintf (devname, "testdev"); snprintf (devname, BOUNDDEVBUFSZ * sizeof (char), "testdev");
buflen = strlen (devname); buflen = strlen (devname);
TEST_ASSERT_SUCCESS_ERRNO ( TEST_ASSERT_SUCCESS_ERRNO (

View File

@ -78,8 +78,8 @@ void test_stream_disconnect ()
// Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome. // Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
sprintf (connect_endpoint, "tcp://127.0.0.1:%s", snprintf (connect_endpoint, MAX_SOCKET_STRING * sizeof (char),
strrchr (bind_endpoint, ':') + 1); "tcp://127.0.0.1:%s", strrchr (bind_endpoint, ':') + 1);
#else #else
strcpy (connect_endpoint, bind_endpoint); strcpy (connect_endpoint, bind_endpoint);
#endif #endif

View File

@ -40,7 +40,7 @@ void test_address_wildcard_ipv4 ()
// Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome. // Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
sprintf (connect_endpoint, "tcp://127.0.0.1:%s", snprintf (connect_endpoint, 256 * sizeof (char), "tcp://127.0.0.1:%s",
strrchr (bind_endpoint, ':') + 1); strrchr (bind_endpoint, ':') + 1);
#else #else
strcpy (connect_endpoint, bind_endpoint); strcpy (connect_endpoint, bind_endpoint);
@ -81,10 +81,10 @@ void test_address_wildcard_ipv6 ()
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
if (ipv6) if (ipv6)
sprintf (connect_endpoint, "tcp://[::1]:%s", snprintf (connect_endpoint, 256 * sizeof (char), "tcp://[::1]:%s",
strrchr (bind_endpoint, ':') + 1); strrchr (bind_endpoint, ':') + 1);
else else
sprintf (connect_endpoint, "tcp://127.0.0.1:%s", snprintf (connect_endpoint, 256 * sizeof (char), "tcp://127.0.0.1:%s",
strrchr (bind_endpoint, ':') + 1); strrchr (bind_endpoint, ':') + 1);
#else #else
strcpy (connect_endpoint, bind_endpoint); strcpy (connect_endpoint, bind_endpoint);

View File

@ -47,7 +47,8 @@ void test_roundtrip ()
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bind_address, &addr_length)); zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bind_address, &addr_length));
// Windows can't connect to 0.0.0.0 // Windows can't connect to 0.0.0.0
sprintf (connect_address, "ws://127.0.0.1%s", strrchr (bind_address, ':')); snprintf (connect_address, MAX_SOCKET_STRING * sizeof (char),
"ws://127.0.0.1%s", strrchr (bind_address, ':'));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address)); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_address));

View File

@ -518,14 +518,14 @@ fd_t bind_socket_resolve_port (const char *address_,
addr_len = sizeof (struct sockaddr_storage); addr_len = sizeof (struct sockaddr_storage);
TEST_ASSERT_SUCCESS_RAW_ERRNO ( TEST_ASSERT_SUCCESS_RAW_ERRNO (
getsockname (s_pre, (struct sockaddr *) &addr, &addr_len)); getsockname (s_pre, (struct sockaddr *) &addr, &addr_len));
sprintf (my_endpoint_, "%s://%s:%u", snprintf (
my_endpoint_, 6 + strlen (address_) + 7 * sizeof (char), "%s://%s:%u",
protocol_ == IPPROTO_TCP ? "tcp" protocol_ == IPPROTO_TCP ? "tcp"
: protocol_ == IPPROTO_UDP ? "udp" : protocol_ == IPPROTO_UDP ? "udp"
: protocol_ == IPPROTO_WSS ? "wss" : protocol_ == IPPROTO_WSS ? "wss"
: "ws", : "ws",
address_, address_,
af_ == AF_INET af_ == AF_INET ? ntohs ((*(struct sockaddr_in *) &addr).sin_port)
? ntohs ((*(struct sockaddr_in *) &addr).sin_port)
: ntohs ((*(struct sockaddr_in6 *) &addr).sin6_port)); : ntohs ((*(struct sockaddr_in6 *) &addr).sin6_port));
} }