Skip to content

Commit

Permalink
Use snprintf in place of sprintf.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebrady committed Apr 8, 2018
1 parent 1c437f1 commit 08a7d4f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion common.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ void command_set_volume(double volume) {
inform("Couldn't allocate memory for set_volume argument string");
} else {
memset(command_buffer, 0, command_buffer_size);
sprintf(command_buffer, "%s%f", config.cmd_set_volume, volume);
snprintf(command_buffer, command_buffer_size, "%s%f", config.cmd_set_volume, volume);
// debug(1,"command_buffer is \"%s\".",command_buffer);
int argC;
char **argV;
Expand Down
2 changes: 1 addition & 1 deletion dacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
char *pt = st;
int it;
for (it = 0; it < 16; it++) {
sprintf(pt, "%02X", metadata_store.item_composite_id[it]);
snprintf(pt, 3, "%02X", metadata_store.item_composite_id[it]);
pt += 2;
}
*pt = 0;
Expand Down
7 changes: 3 additions & 4 deletions dbus-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ void dbus_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused))
// Make up the artwork URI if we have one
if (argc->cover_art_pathname) {
char artURIstring[1024];
sprintf(artURIstring, "file://%s", argc->cover_art_pathname);
// sprintf(artURIstring,"");
snprintf(artURIstring, sizeof(artURIstring), "file://%s", argc->cover_art_pathname);
// debug(1,"artURI String: \"%s\".",artURIstring);
GVariant *artUrl = g_variant_new("s", artURIstring);
g_variant_builder_add(dict_builder, "{sv}", "mpris:artUrl", artUrl);
Expand All @@ -152,7 +151,7 @@ void dbus_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused))
if ((argc->track_metadata) && (argc->track_metadata->item_id)) {
char trackidstring[128];
// debug(1, "Set ID using mper ID: \"%u\".",argc->item_id);
sprintf(trackidstring, "/org/gnome/ShairportSync/mper_%u", argc->track_metadata->item_id);
snprintf(trackidstring, sizeof(trackidstring), "/org/gnome/ShairportSync/mper_%u", argc->track_metadata->item_id);
GVariant *trackid = g_variant_new("o", trackidstring);
g_variant_builder_add(dict_builder, "{sv}", "mpris:trackid", trackid);
}
Expand Down Expand Up @@ -733,7 +732,7 @@ static void on_dbus_name_lost(__attribute__((unused)) GDBusConnection *connectio
// name, (config.dbus_service_bus_type == DBT_session) ? "session" : "system");
pid_t pid = getpid();
char interface_name[256] = "";
sprintf(interface_name, "org.gnome.ShairportSync.i%d", pid);
snprintf(interface_name, sizeof(interface_name), "org.gnome.ShairportSync.i%d", pid);
GBusType dbus_bus_type = G_BUS_TYPE_SYSTEM;
if (config.dbus_service_bus_type == DBT_session)
dbus_bus_type = G_BUS_TYPE_SESSION;
Expand Down
2 changes: 1 addition & 1 deletion mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void mdns_register(void) {
char *p = mdns_service_name;
int i;
for (i = 0; i < 6; i++) {
sprintf(p, "%02X", config.hw_addr[i]);
snprintf(p, 3, "%02X", config.hw_addr[i]);
p += 2;
}
*p++ = '@';
Expand Down
2 changes: 1 addition & 1 deletion mdns_avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void resolve_callback(AvahiServiceResolver *r, AVAHI_GCC_UNUSED AvahiIfIn
#ifdef CONFIG_METADATA
char portstring[20];
memset(portstring, 0, sizeof(portstring));
sprintf(portstring, "%u", port);
snprintf(portstring, sizeof(portstring), "%u", port);
send_ssnc_metadata('dapo', strdup(portstring), strlen(portstring), 0);
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions mdns_external.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int fork_execvp(const char *file, char *const argv[]) {

static int mdns_external_avahi_register(char *apname, __attribute__((unused)) int port) {
char mdns_port[6];
sprintf(mdns_port, "%d", config.port);
snprintf(mdns_port, sizeof(mdns_port), "%d", config.port);

char *argvwithoutmetadata[] = {
NULL, apname, config.regtype, mdns_port, MDNS_RECORD_WITHOUT_METADATA, NULL};
Expand Down Expand Up @@ -124,7 +124,7 @@ static int mdns_external_avahi_register(char *apname, __attribute__((unused)) in

static int mdns_external_dns_sd_register(char *apname, __attribute__((unused)) int port) {
char mdns_port[6];
sprintf(mdns_port, "%d", config.port);
snprintf(mdns_port, sizeof(mdns_port), "%d", config.port);

char *argvwithoutmetadata[] = {
NULL, apname, config.regtype, mdns_port, MDNS_RECORD_WITHOUT_METADATA, NULL};
Expand Down
59 changes: 50 additions & 9 deletions metadata_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ void metadata_hub_release_track_metadata(struct track_metadata_bundle *track_met
if (track_metadata) {
release_char_string(&track_metadata->track_name);
release_char_string(&track_metadata->artist_name);
release_char_string(&track_metadata->album_artist_name);
release_char_string(&track_metadata->album_name);
release_char_string(&track_metadata->genre);
release_char_string(&track_metadata->comment);
release_char_string(&track_metadata->composer);
release_char_string(&track_metadata->file_kind);
release_char_string(&track_metadata->song_description);
release_char_string(&track_metadata->song_album_artist);
release_char_string(&track_metadata->sort_as);
release_char_string(&track_metadata->sort_name);
release_char_string(&track_metadata->sort_artist);
release_char_string(&track_metadata->sort_album);
release_char_string(&track_metadata->sort_composer);
free((char *)track_metadata);
} else {
debug(3, "Asked to release non-existent track metadata");
Expand Down Expand Up @@ -213,7 +217,7 @@ char *metadata_write_image_file(const char *buf, int len) {
char jpg[] = "jpg";
int i;
for (i = 0; i < 16; i++)
sprintf(&img_md5_str[i * 2], "%02x", (uint8_t)img_md5[i]);
snprintf(&img_md5_str[i * 2], 3, "%02x", (uint8_t)img_md5[i]);
// see if the file is a jpeg or a png
if (strncmp(buf, "\xFF\xD8\xFF", 3) == 0)
ext = jpg;
Expand Down Expand Up @@ -327,6 +331,14 @@ void metadata_hub_process_metadata(uint32_t type, uint32_t code, char *data, uin
debug(1, "No track metadata memory allocated when artist name received!");
}
break;
case 'assl':
if (track_metadata) {
track_metadata->album_artist_name = strndup(data, length);
debug(2, "MH Album Artist name set to: \"%s\"", track_metadata->album_artist_name);
} else {
debug(1, "No track metadata memory allocated when album artist name received!");
}
break;
case 'ascm':
if (track_metadata) {
track_metadata->comment = strndup(data, length);
Expand Down Expand Up @@ -372,21 +384,48 @@ void metadata_hub_process_metadata(uint32_t type, uint32_t code, char *data, uin
track_metadata->song_album_artist = strndup(data, length);
debug(2, "MH Song Album Artist set to: \"%s\"", track_metadata->song_album_artist);
} else {
debug(1, "No track metadata memory allocated when song description received!");
debug(1, "No track metadata memory allocated when song artist received!");
}
break;
case 'assn':
if (track_metadata) {
track_metadata->sort_as = strndup(data, length);
debug(2, "MH Sort As set to: \"%s\"", track_metadata->sort_as);
track_metadata->sort_name = strndup(data, length);
debug(2, "MH Sort Name set to: \"%s\"", track_metadata->sort_name);
} else {
debug(1,
"No track metadata memory allocated when sort name description received!");
}
break;
case 'assa':
if (track_metadata) {
track_metadata->sort_artist = strndup(data, length);
debug(2, "MH Sort Artist set to: \"%s\"", track_metadata->sort_artist);
} else {
debug(1,
"No track metadata memory allocated when sort artist description received!");
}
break;
case 'assu':
if (track_metadata) {
track_metadata->sort_album = strndup(data, length);
debug(2, "MH Sort Album set to: \"%s\"", track_metadata->sort_album);
} else {
debug(1,
"No track metadata memory allocated when sort as (sort name) description received!");
"No track metadata memory allocated when sort album description received!");
}
break;
case 'assc':
if (track_metadata) {
track_metadata->sort_composer = strndup(data, length);
debug(2, "MH Sort Composer set to: \"%s\"", track_metadata->sort_composer);
} else {
debug(1,
"No track metadata memory allocated when sort composer description received!");
}
break;

// default:
/*
default:
/*
{
char typestring[5];
*(uint32_t *)typestring = htonl(type);
Expand All @@ -403,7 +442,9 @@ void metadata_hub_process_metadata(uint32_t type, uint32_t code, char *data, uin
if (payload)
free(payload);
}
*/
*/
break;

}
} else if (type == 'ssnc') {
switch (code) {
Expand Down
6 changes: 5 additions & 1 deletion metadata_hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ typedef struct track_metadata_bundle {
item_composite_id[16]; // seems to be nowplaying 4 ids: dbid, plid, playlistItem, itemid
char *track_name; // a malloced string -- if non-zero, free it before replacing it
char *artist_name; // a malloced string -- if non-zero, free it before replacing it
char *album_artist_name; // a malloced string -- if non-zero, free it before replacing it
char *album_name; // a malloced string -- if non-zero, free it before replacing it
char *genre; // a malloced string -- if non-zero, free it before replacing it
char *comment; // a malloced string -- if non-zero, free it before replacing it
char *composer; // a malloced string -- if non-zero, free it before replacing it
char *file_kind; // a malloced string -- if non-zero, free it before replacing it
char *song_description; // a malloced string -- if non-zero, free it before replacing it
char *song_album_artist; // a malloced string -- if non-zero, free it before replacing it
char *sort_as; // a malloced string -- if non-zero, free it before replacing it
char *sort_name; // a malloced string -- if non-zero, free it before replacing it
char *sort_artist; // a malloced string -- if non-zero, free it before replacing it
char *sort_album; // a malloced string -- if non-zero, free it before replacing it
char *sort_composer; // a malloced string -- if non-zero, free it before replacing it
uint32_t songtime_in_milliseconds;
} track_metadata_bundle;

Expand Down
11 changes: 5 additions & 6 deletions mpris-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ void mpris_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused)
// Make up the artwork URI if we have one
if (argc->cover_art_pathname) {
char artURIstring[1024];
sprintf(artURIstring, "file://%s", argc->cover_art_pathname);
// sprintf(artURIstring,"");
snprintf(artURIstring, sizeof(artURIstring), "file://%s", argc->cover_art_pathname);
// debug(1,"artURI String: \"%s\".",artURIstring);
GVariant *artUrl = g_variant_new("s", artURIstring);
g_variant_builder_add(dict_builder, "{sv}", "mpris:artUrl", artUrl);
Expand All @@ -109,7 +108,7 @@ void mpris_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused)
for (it = 0; it < 16; it++) {
if (argc->track_metadata->item_composite_id[it])
non_zero = 1;
sprintf(pt, "%02X", argc->track_metadata->item_composite_id[it]);
snprintf(pt, 3, "%02X", argc->track_metadata->item_composite_id[it]);
pt += 2;
}
}
Expand All @@ -118,13 +117,13 @@ void mpris_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused)
if (non_zero) {
// debug(1, "Set ID using composite ID: \"0x%s\".", st);
char trackidstring[1024];
sprintf(trackidstring, "/org/gnome/ShairportSync/%s", st);
snprintf(trackidstring, sizeof(trackidstring), "/org/gnome/ShairportSync/%s", st);
GVariant *trackid = g_variant_new("o", trackidstring);
g_variant_builder_add(dict_builder, "{sv}", "mpris:trackid", trackid);
} else if ((argc->track_metadata) && (argc->track_metadata->item_id)) {
char trackidstring[128];
// debug(1, "Set ID using mper ID: \"%u\".",argc->item_id);
sprintf(trackidstring, "/org/gnome/ShairportSync/mper_%u", argc->track_metadata->item_id);
snprintf(trackidstring, sizeof(trackidstring), "/org/gnome/ShairportSync/mper_%u", argc->track_metadata->item_id);
GVariant *trackid = g_variant_new("o", trackidstring);
g_variant_builder_add(dict_builder, "{sv}", "mpris:trackid", trackid);
}
Expand Down Expand Up @@ -292,7 +291,7 @@ static void on_mpris_name_lost(__attribute__((unused)) GDBusConnection *connecti
// name,(mpris_bus_type==G_BUS_TYPE_SESSION) ? "session" : "system");
pid_t pid = getpid();
char interface_name[256] = "";
sprintf(interface_name, "org.mpris.MediaPlayer2.ShairportSync.i%d", pid);
snprintf(interface_name, sizeof(interface_name), "org.mpris.MediaPlayer2.ShairportSync.i%d", pid);
GBusType mpris_bus_type = G_BUS_TYPE_SYSTEM;
if (config.mpris_service_bus_type == DBT_session)
mpris_bus_type = G_BUS_TYPE_SESSION;
Expand Down
4 changes: 2 additions & 2 deletions rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void *rtp_control_receiver(void *arg) {
char *obfp = obf;
int obfc;
for (obfc = 0; obfc < plen; obfc++) {
sprintf(obfp, "%02X", packet[obfc]);
snprintf(obfp, 3, "%02X", packet[obfc]);
obfp += 2;
};
*obfp = 0;
Expand Down Expand Up @@ -500,7 +500,7 @@ void *rtp_timing_receiver(void *arg) {
char *obfp = obf;
int obfc;
for (obfc=0;obfc<plen;obfc++) {
sprintf(obfp,"%02X",packet[obfc]);
snprintf(obfp, 3, "%02X", packet[obfc]);
obfp+=2;
};
*obfp=0;
Expand Down
19 changes: 9 additions & 10 deletions rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static void debug_print_msg_content(int level, rtsp_message *msg) {
char *obfp = obf;
int obfc;
for (obfc = 0; obfc < msg->contentlength; obfc++) {
sprintf(obfp, "%02X", msg->content[obfc]);
snprintf(obfp, 3, "%02X", msg->content[obfc]);
obfp += 2;
};
*obfp = 0;
Expand Down Expand Up @@ -823,9 +823,8 @@ static void handle_setup(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *
strcat(hdr, q); // should unsplice the timing port entry
}

char *resphdr = alloca(200);
*resphdr = 0;
sprintf(resphdr, "RTP/AVP/"
char resphdr[256] = "";
snprintf(resphdr, sizeof(resphdr), "RTP/AVP/"
"UDP;unicast;interleaved=0-1;mode=record;control_port=%d;"
"timing_port=%d;server_"
"port=%d",
Expand Down Expand Up @@ -1328,7 +1327,7 @@ static void handle_get_parameter(__attribute__((unused)) rtsp_conn_info *conn, r
char *p = malloc(128); // will be automatically deallocated with the response is deleted
if (p) {
resp->content = p;
resp->contentlength = sprintf(p, "\r\nvolume: %.6f\r\n", config.airplay_volume);
resp->contentlength = snprintf(p, 128, "\r\nvolume: %.6f\r\n", config.airplay_volume);
} else {
debug(1, "Couldn't allocate space for a response.");
}
Expand Down Expand Up @@ -1779,7 +1778,7 @@ static int rtsp_auth(char **nonce, rtsp_message *req, rtsp_message *resp) {
int i;
unsigned char buf[33];
for (i = 0; i < 16; i++)
sprintf((char *)buf + 2 * i, "%02x", digest_urp[i]);
snprintf((char *)buf + 2 * i, 3, "%02x", digest_urp[i]);

#ifdef HAVE_LIBSSL
MD5_Init(&ctx);
Expand All @@ -1788,7 +1787,7 @@ static int rtsp_auth(char **nonce, rtsp_message *req, rtsp_message *resp) {
MD5_Update(&ctx, *nonce, strlen(*nonce));
MD5_Update(&ctx, ":", 1);
for (i = 0; i < 16; i++)
sprintf((char *)buf + 2 * i, "%02x", digest_mu[i]);
snprintf((char *)buf + 2 * i, 3, "%02x", digest_mu[i]);
MD5_Update(&ctx, buf, 32);
MD5_Final(digest_total, &ctx);
#endif
Expand All @@ -1800,7 +1799,7 @@ static int rtsp_auth(char **nonce, rtsp_message *req, rtsp_message *resp) {
mbedtls_md5_update(&tctx, (const unsigned char *)*nonce, strlen(*nonce));
mbedtls_md5_update(&tctx, (unsigned char *)":", 1);
for (i = 0; i < 16; i++)
sprintf((char *)buf + 2 * i, "%02x", digest_mu[i]);
snprintf((char *)buf + 2 * i, 3, "%02x", digest_mu[i]);
mbedtls_md5_update(&tctx, buf, 32);
mbedtls_md5_finish(&tctx, digest_total);
#endif
Expand All @@ -1812,13 +1811,13 @@ static int rtsp_auth(char **nonce, rtsp_message *req, rtsp_message *resp) {
md5_update(&tctx, (const unsigned char *)*nonce, strlen(*nonce));
md5_update(&tctx, (unsigned char *)":", 1);
for (i = 0; i < 16; i++)
sprintf((char *)buf + 2 * i, "%02x", digest_mu[i]);
snprintf((char *)buf + 2 * i, 3, "%02x", digest_mu[i]);
md5_update(&tctx, buf, 32);
md5_finish(&tctx, digest_total);
#endif

for (i = 0; i < 16; i++)
sprintf((char *)buf + 2 * i, "%02x", digest_total[i]);
snprintf((char *)buf + 2 * i, 3, "%02x", digest_total[i]);

if (!strcmp(response, (const char *)buf))
return 0;
Expand Down

0 comments on commit 08a7d4f

Please sign in to comment.