replace sprintf by snprintf (silence warning by macOS compiler)

This commit is contained in:
F. Duncanh
2023-04-15 04:43:00 -04:00
parent 4a86c90c18
commit 67e9c0eca1
8 changed files with 29 additions and 17 deletions

View File

@@ -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++;
}
}

View File

@@ -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));

View File

@@ -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");

View File

@@ -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,

View File

@@ -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,

View File

@@ -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);

View File

@@ -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));