Skip to content

Commit

Permalink
cleanup: Stop using strerror directly.
Browse files Browse the repository at this point in the history
We have a more portable wrapper that is now also thread-safe. Also
stopped using sprintf in the one place we used it. This doesn't really
help much, but it allows us to forbid sprintf globally.
  • Loading branch information
iphydf committed Jan 15, 2022
1 parent 8d19757 commit de546b8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ START_TEST(test_addr_resolv_localhost)
int res = addr_resolve(localhost, &ip, nullptr);

int error = net_error();
const char *strerror = net_new_strerror(error);
char *strerror = net_new_strerror(error);
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7f4c96481e30156e3c2e7e448e70faaaa12911694390374ae09bd53d5d7b4f9c /usr/local/bin/tox-bootstrapd
fc6b060abddd1898d97b0cf067d19c7a9d68999469690d91e7cf59327e700dbf /usr/local/bin/tox-bootstrapd
6 changes: 4 additions & 2 deletions toxav/bwcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ static void send_update(BWController *bwc)
assert(offset == sizeof(bwc_packet));

if (bwc_send_custom_lossy_packet(bwc->tox, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) {
const char *netstrerror = net_new_strerror(net_error());
char *netstrerror = net_new_strerror(net_error());
char *stdstrerror = net_new_strerror(errno);
LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s",
(unsigned)sizeof(bwc_packet), strerror(errno), netstrerror);
(unsigned)sizeof(bwc_packet), stdstrerror, netstrerror);
net_kill_strerror(stdstrerror);
net_kill_strerror(netstrerror);
}
}
Expand Down
18 changes: 9 additions & 9 deletions toxav/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length);

if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, SIZEOF_VLA(rdata))) {
const char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %u)! std error: %s, net error: %s",
(unsigned)SIZEOF_VLA(rdata), strerror(errno), netstrerror);
char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %u)! net error: %s",
(unsigned)SIZEOF_VLA(rdata), netstrerror);
net_kill_strerror(netstrerror);
}
} else {
Expand All @@ -832,9 +832,9 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,

if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number,
rdata, piece + RTP_HEADER_SIZE + 1)) {
const char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s, net error: %s",
piece + RTP_HEADER_SIZE + 1, strerror(errno), netstrerror);
char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! net error: %s",
piece + RTP_HEADER_SIZE + 1, netstrerror);
net_kill_strerror(netstrerror);
}

Expand All @@ -852,9 +852,9 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,

if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata,
piece + RTP_HEADER_SIZE + 1)) {
const char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s, net error: %s",
piece + RTP_HEADER_SIZE + 1, strerror(errno), netstrerror);
char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! net error: %s",
piece + RTP_HEADER_SIZE + 1, netstrerror);
net_kill_strerror(netstrerror);
}
}
Expand Down
4 changes: 3 additions & 1 deletion toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,9 @@ static Toxav_Err_Send_Frame send_frames(const Logger *log, ToxAVCall *call)
LOGGER_DEBUG(log, "+ _sending_FRAME_ b0=%d b1=%d", buf[0], buf[1]);

if (res < 0) {
LOGGER_WARNING(log, "Could not send video frame: %s", strerror(errno));
char *netstrerror = net_new_strerror(net_error());
LOGGER_WARNING(log, "Could not send video frame: %s", netstrerror);
net_kill_strerror(netstrerror);
return TOXAV_ERR_SEND_FRAME_RTP_FAILED;
}
}
Expand Down
2 changes: 1 addition & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ static char *id_to_string(const uint8_t *pk, char *id_str, size_t length)
}

for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) {
sprintf(&id_str[i * 2], "%02X", pk[i]);
snprintf(&id_str[i * 2], length - i * 2, "%02X", pk[i]);
}

id_str[CRYPTO_PUBLIC_KEY_SIZE * 2] = 0;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static int proxy_http_read_connection_response(const Logger *logger, TCP_Client_

data[sizeof(data) - 1] = 0;

if (strstr((char *)data, success)) {
if (strstr((const char *)data, success)) {
// drain all data
unsigned int data_left = net_socket_data_recv_buffer(tcp_conn->sock);

Expand Down
23 changes: 15 additions & 8 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu

if (res < 0) { /* Windows doesn't necessarily know `%zu` */
int error = net_error();
const char *strerror = net_new_strerror(error);
char *strerror = net_new_strerror(error);
LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
buffer[0], message, min_u16(buflen, 999), 'E',
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error,
Expand Down Expand Up @@ -639,7 +639,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8
int error = net_error();

if (!should_ignore_recv_error(error)) {
const char *strerror = net_new_strerror(error);
char *strerror = net_new_strerror(error);
LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s", error, strerror);
net_kill_strerror(strerror);
}
Expand Down Expand Up @@ -836,7 +836,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from,
/* Check for socket error. */
if (!sock_valid(temp->sock)) {
int neterror = net_error();
const char *strerror = net_new_strerror(neterror);
char *strerror = net_new_strerror(neterror);
LOGGER_ERROR(log, "Failed to get a socket?! %d, %s", neterror, strerror);
net_kill_strerror(strerror);
free(temp);
Expand Down Expand Up @@ -945,7 +945,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from,
const int res = setsockopt(temp->sock.socket, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));

int neterror = net_error();
const char *strerror = net_new_strerror(neterror);
char *strerror = net_new_strerror(neterror);

if (res < 0) {
LOGGER_DEBUG(log, "Failed to activate local multicast membership. (%d, %s)", neterror, strerror);
Expand Down Expand Up @@ -1017,7 +1017,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from,

char ip_str[IP_NTOA_LEN];
int neterror = net_error();
const char *strerror = net_new_strerror(neterror);
char *strerror = net_new_strerror(neterror);
LOGGER_ERROR(log, "Failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", neterror, strerror,
ip_ntoa(&ip, ip_str, sizeof(ip_str)), port_from, port_to);
net_kill_strerror(strerror);
Expand Down Expand Up @@ -1703,7 +1703,7 @@ int net_error(void)
#endif
}

const char *net_new_strerror(int error)
char *net_new_strerror(int error)
{
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
char *str = nullptr;
Expand All @@ -1718,13 +1718,20 @@ const char *net_new_strerror(int error)
error, 0, (char *)&str, 0, nullptr);
return str;
#else
return strerror(error);
char *str = (char *)malloc(256);
const int fmt_error = strerror_r(error, str, 256);
if (fmt_error != 0) {
snprintf(str, 256, "error %d (strerror failed with error %d)", error, fmt_error);
}
return str;
#endif
}

void net_kill_strerror(const char *strerror)
void net_kill_strerror(char *strerror)
{
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
LocalFree((char *)strerror);
#else
free(strerror);
#endif
}
4 changes: 2 additions & 2 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ int net_error(void);
* return pointer to a NULL-terminated string describing the error code on
* success. The returned string must be freed using net_kill_strerror().
*/
const char *net_new_strerror(int error);
char *net_new_strerror(int error);

/** Frees the string returned by net_new_strerror().
* It's valid to pass NULL as the argument, the function does nothing in this
* case.
*/
void net_kill_strerror(const char *strerror);
void net_kill_strerror(char *strerror);

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
Expand Down

0 comments on commit de546b8

Please sign in to comment.