From 67e9c0eca1bbc043dbdd86e70b3ec820e12a6fe5 Mon Sep 17 00:00:00 2001 From: "F. Duncanh" Date: Sat, 15 Apr 2023 04:43:00 -0400 Subject: [PATCH] replace sprintf by snprintf (silence warning by macOS compiler) --- lib/http_request.c | 15 ++++++++++----- lib/mirror_buffer.c | 4 ++-- lib/raop_handlers.h | 2 +- lib/raop_ntp.c | 3 ++- lib/raop_rtp.c | 2 +- lib/raop_rtp_mirror.c | 6 ++++-- lib/utils.c | 10 +++++++--- uxplay.cpp | 4 ++-- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/http_request.c b/lib/http_request.c index cdce53b..2a1936e 100644 --- a/lib/http_request.c +++ b/lib/http_request.c @@ -276,14 +276,19 @@ http_request_get_header_string(http_request_t *request, char **header_str) assert(str); *header_str = str; char *p = str; + int n = sizeof(str); for (int i = 0; i < request->headers_size; i++) { - sprintf(p,"%s", request->headers[i]); - p += strlen(request->headers[i]); + int hlen = strlen(request->headers[i]); + snprintf(p, n, "%s", request->headers[i]); + n -= hlen; + p += hlen; if (i%2 == 0) { - sprintf(p, ": "); - p +=2; + snprintf(p, n, ": "); + n -= 2; + p += 2; } else { - sprintf(p, "\n"); + snprintf(p, n, "\n"); + n--; p++; } } diff --git a/lib/mirror_buffer.c b/lib/mirror_buffer.c index 94c7f68..2bbccda 100644 --- a/lib/mirror_buffer.c +++ b/lib/mirror_buffer.c @@ -50,8 +50,8 @@ mirror_buffer_init_aes(mirror_buffer_t *mirror_buffer, const uint64_t *streamCon /* AES key and IV */ // Need secondary processing to use - sprintf((char*) aeskey_video, "AirPlayStreamKey%" PRIu64, *streamConnectionID); - sprintf((char*) aesiv_video, "AirPlayStreamIV%" PRIu64, *streamConnectionID); + snprintf((char*) aeskey_video, sizeof(aeskey_video), "AirPlayStreamKey%" PRIu64, *streamConnectionID); + snprintf((char*) aesiv_video, sizeof(aesiv_video), "AirPlayStreamIV%" PRIu64, *streamConnectionID); sha_ctx_t *ctx = sha_init(); sha_update(ctx, aeskey_video, strlen((char*) aeskey_video)); diff --git a/lib/raop_handlers.h b/lib/raop_handlers.h index 6bfc823..b8554ac 100644 --- a/lib/raop_handlers.h +++ b/lib/raop_handlers.h @@ -694,7 +694,7 @@ raop_handler_record(raop_conn_t *conn, { char audio_latency[12]; unsigned int ad = (unsigned int) (((uint64_t) conn->raop->audio_delay_micros) * AUDIO_SAMPLE_RATE / SECOND_IN_USECS); - sprintf(audio_latency, "%u", ad); + snprintf(audio_latency, sizeof(audio_latency), "%u", ad); logger_log(conn->raop->logger, LOGGER_DEBUG, "raop_handler_record"); http_response_add_header(response, "Audio-Latency", audio_latency); http_response_add_header(response, "Audio-Jack-Status", "connected; type=analog"); diff --git a/lib/raop_ntp.c b/lib/raop_ntp.c index 7fd5361..ce0ad34 100644 --- a/lib/raop_ntp.c +++ b/lib/raop_ntp.c @@ -126,7 +126,8 @@ raop_ntp_parse_remote_address(raop_ntp_t *raop_ntp, const unsigned char *remote_ return -1; } memset(current, 0, sizeof(current)); - sprintf(current, "%d.%d.%d.%d", remote_addr[0], remote_addr[1], remote_addr[2], remote_addr[3]); + snprintf(current, sizeof(current), "%d.%d.%d.%d", remote_addr[0], remote_addr[1], + remote_addr[2], remote_addr[3]); logger_log(raop_ntp->logger, LOGGER_DEBUG, "raop_ntp parse remote ip = %s", current); ret = netutils_parse_address(family, current, &raop_ntp->remote_saddr, diff --git a/lib/raop_rtp.c b/lib/raop_rtp.c index 55706d6..dadb905 100644 --- a/lib/raop_rtp.c +++ b/lib/raop_rtp.c @@ -136,7 +136,7 @@ raop_rtp_parse_remote(raop_rtp_t *raop_rtp, const unsigned char *remote, int rem return -1; } memset(current, 0, sizeof(current)); - sprintf(current, "%d.%d.%d.%d", remote[0], remote[1], remote[2], remote[3]); + snprintf(current, sizeof(current), "%d.%d.%d.%d", remote[0], remote[1], remote[2], remote[3]); logger_log(raop_rtp->logger, LOGGER_DEBUG, "raop_rtp parse remote ip = %s", current); ret = netutils_parse_address(family, current, &raop_rtp->remote_saddr, diff --git a/lib/raop_rtp_mirror.c b/lib/raop_rtp_mirror.c index 4ddfda1..06cdaec 100644 --- a/lib/raop_rtp_mirror.c +++ b/lib/raop_rtp_mirror.c @@ -127,7 +127,7 @@ raop_rtp_parse_remote(raop_rtp_mirror_t *raop_rtp_mirror, const unsigned char *r return -1; } memset(current, 0, sizeof(current)); - sprintf(current, "%d.%d.%d.%d", remote[0], remote[1], remote[2], remote[3]); + snprintf(current, sizeof(current), "%d.%d.%d.%d", remote[0], remote[1], remote[2], remote[3]); logger_log(raop_rtp_mirror->logger, LOGGER_DEBUG, "raop_rtp_mirror parse remote ip = %s", current); ret = netutils_parse_address(family, current, &raop_rtp_mirror->remote_saddr, @@ -316,8 +316,10 @@ raop_rtp_mirror_thread(void *arg) int payload_size = byteutils_get_int(packet, 0); char packet_description[13] = {0}; char *p = packet_description; + int n = sizeof(packet_description); for (int i = 4; i < 8; i++) { - sprintf(p, "%2.2x ", (unsigned int) packet[i]); + snprintf(p, n, "%2.2x ", (unsigned int) packet[i]); + n -= 3; p += 3; } ntp_timestamp_raw = byteutils_get_long(packet, 8); diff --git a/lib/utils.c b/lib/utils.c index 230dd15..9ba21f5 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -191,15 +191,19 @@ char *utils_data_to_string(const unsigned char *data, int datalen, int chars_per char *str = (char *) calloc(len + 1, sizeof(char)); assert(str); char *p = str; + int n = sizeof(str); for (int i = 0; i < datalen; i++) { if (i > 0 && i % chars_per_line == 0) { - sprintf(p,"\n"); + snprintf(p, n, "\n"); + n--; p++; } - sprintf(p,"%2.2x ", (unsigned int) data[i]); + snprintf(p, n, "%2.2x ", (unsigned int) data[i]); + n -= 3; p += 3; } - sprintf(p,"\n"); + snprintf(p, n, "\n"); + n--; p++; assert(p == &(str[len])); assert(len == strlen(str)); diff --git a/uxplay.cpp b/uxplay.cpp index 45ad20d..0d8dc3b 100644 --- a/uxplay.cpp +++ b/uxplay.cpp @@ -296,7 +296,7 @@ static std::string find_mac () { } mac.erase(); for (int i = 0; i < 6; i++) { - sprintf(str,"%02x", int(address->PhysicalAddress[i])); + snprintf(str, sizeof(str), "%02x", int(address->PhysicalAddress[i])); mac = mac + str; if (i < 5) mac = mac + ":"; } @@ -329,7 +329,7 @@ static std::string find_mac () { if (non_null_octets) { mac.erase(); for (int i = 0; i < 6 ; i++) { - sprintf(str,"%02x", octet[i]); + snprintf(str, sizeof(str), "%02x", octet[i]); mac = mac + str; if (i < 5) mac = mac + ":"; }