diff --git a/lib/fcup_request.h b/lib/fcup_request.h index 8bd42a2..b84ef97 100644 --- a/lib/fcup_request.h +++ b/lib/fcup_request.h @@ -58,7 +58,7 @@ char *create_fcup_request(const char *url, int request_id, const char *client_se plist_dict_set_item(fcup_request_node, "FCUP_Response_Headers", fcup_response_header_node); plist_dict_set_item(req_root_node, "request", fcup_request_node); - uint32_t uint_val; + uint32_t uint_val = 0; plist_to_xml(req_root_node, &plist_xml, &uint_val); *datalen = (int) uint_val; @@ -71,7 +71,7 @@ int fcup_request(void *conn_opaque, const char *media_url, const char *client_se raop_conn_t *conn = (raop_conn_t *) conn_opaque; int datalen = 0; - int requestlen; + int requestlen = 0; int socket_fd = httpd_get_connection_socket_by_type(conn->raop->httpd, CONNECTION_TYPE_PTTH, 1); diff --git a/lib/http_handlers.h b/lib/http_handlers.h index 3ad5e85..23c46df 100644 --- a/lib/http_handlers.h +++ b/lib/http_handlers.h @@ -28,7 +28,6 @@ http_handler_server_info(raop_conn_t *conn, http_request_t *request, http_respon const char *hw_addr_raw = dnssd_get_hw_addr(conn->raop->dnssd, &hw_addr_raw_len); char *hw_addr = calloc(1, 3 * hw_addr_raw_len); - //int hw_addr_len = utils_hwaddr_airplay(hw_addr, 3 * hw_addr_raw_len, hw_addr_raw, hw_addr_raw_len); plist_t r_node = plist_new_dict(); @@ -91,7 +90,7 @@ http_handler_scrub(raop_conn_t *conn, http_request_t *request, http_response_t * if (data) { data++; const char *position = strstr(data, "=") + 1; - char *end; + char *end = NULL; double value = strtod(position, &end); if (end && end != position) { scrub_position = (float) value; @@ -113,7 +112,7 @@ http_handler_rate(raop_conn_t *conn, http_request_t *request, http_response_t *r if (data) { data++; const char *rate = strstr(data, "=") + 1; - char *end; + char *end = NULL; float value = strtof(rate, &end); if (end && end != rate) { rate_value = value; @@ -261,7 +260,7 @@ http_handler_fpsetup2(raop_conn_t *conn, http_request_t *request, http_response_ char **response_data, int *response_datalen) { logger_log(conn->raop->logger, LOGGER_WARNING, "client HTTP request POST fp-setup2 is unhandled"); http_response_add_header(response, "Content-Type", "application/x-apple-binary-plist"); - int req_datalen; + int req_datalen = 0; const unsigned char *req_data = (unsigned char *) http_request_get_data(request, &req_datalen); logger_log(conn->raop->logger, LOGGER_ERR, "only FairPlay version 0x03 is implemented, version is 0x%2.2x", req_data[4]); @@ -326,7 +325,7 @@ int create_playback_info_plist_xml(playback_info_t *playback_info, char **plist_ seekable_time_ranges_node); plist_dict_set_item(res_root_node, "seekableTimeRanges", seekable_time_ranges_node); - int len; + int len = 0; plist_to_xml(res_root_node, plist_xml, (uint32_t *) &len); /* plist_xml is null-terminated, last character is '/n' */ @@ -433,7 +432,7 @@ http_handler_action(raop_conn_t *conn, http_request_t *request, http_response_t airplay_video_t *airplay_video = conn->raop->airplay_video[conn->raop->current_video]; bool data_is_plist = false; plist_t req_root_node = NULL; - uint64_t uint_val; + uint64_t uint_val = 0; int request_id = 0; int fcup_response_statuscode = 0; bool logger_debug = (logger_get_level(conn->raop->logger) >= LOGGER_DEBUG); @@ -923,8 +922,8 @@ http_handler_hls(raop_conn_t *conn, http_request_t *request, http_response_t *r } } else { - int chunks; - float duration; + int chunks = 0; + float duration = 0.0f; char *media_playlist = get_media_playlist(airplay_video, &chunks, &duration, url); if (media_playlist) { char *data = adjust_yt_condensed_playlist(media_playlist); @@ -941,7 +940,7 @@ http_handler_hls(raop_conn_t *conn, http_request_t *request, http_response_t *r http_response_add_header(response, "Access-Control-Allow-Headers", "Content-type"); http_response_add_header(response, "Access-Control-Allow-Origin", "*"); - const char *date; + const char *date = NULL; date = gmt_time_string(); http_response_add_header(response, "Date", date); if (*response_datalen > 0) { diff --git a/lib/raop.c b/lib/raop.c index 7ca3f62..0892398 100644 --- a/lib/raop.c +++ b/lib/raop.c @@ -129,8 +129,8 @@ typedef struct raop_conn_s raop_conn_t; static void * conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remote, int remotelen, unsigned int zone_id) { raop_t *raop = opaque; - raop_conn_t *conn; - char ip_address[40]; + raop_conn_t *conn = NULL; + char ip_address[40] = { '\0' }; assert(raop); conn = calloc(1, sizeof(raop_conn_t)); @@ -154,18 +154,17 @@ conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remot return NULL; } - utils_ipaddress_to_string(locallen, local, zone_id, ip_address, (int) sizeof(ip_address)); logger_log(conn->raop->logger, LOGGER_INFO, "Local : %s", ip_address); utils_ipaddress_to_string(remotelen, remote, zone_id, ip_address, (int) sizeof(ip_address)); logger_log(conn->raop->logger, LOGGER_INFO, "Remote: %s", ip_address); - conn->local = malloc(locallen); + conn->local = (unsigned char *) malloc(locallen); assert(conn->local); memcpy(conn->local, local, locallen); - conn->remote = malloc(remotelen); + conn->remote = (unsigned char *) malloc(remotelen); assert(conn->remote); memcpy(conn->remote, remote, remotelen); @@ -242,7 +241,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) { if (conn->connection_type == CONNECTION_TYPE_UNKNOWN) { if (cseq || ble) { if (httpd_count_connection_type(conn->raop->httpd, CONNECTION_TYPE_RAOP)) { - char ipaddr[40]; + char ipaddr[40] = { '\0' }; utils_ipaddress_to_string(conn->remotelen, conn->remote, conn->zone_id, ipaddr, (int) (sizeof(ipaddr))); if (httpd_nohold(conn->raop->httpd)) { logger_log(conn->raop->logger, LOGGER_INFO, "\"nohold\" feature: switch to new connection request from %s", ipaddr); @@ -264,9 +263,9 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) { logger_log(conn->raop->logger, LOGGER_DEBUG, "New connection %p identified as Connection type AirPlay", ptr); httpd_set_connection_type(conn->raop->httpd, ptr, CONNECTION_TYPE_AIRPLAY); conn->connection_type = CONNECTION_TYPE_AIRPLAY; - size_t len = strlen(client_session_id) + 1; - conn->client_session_id = (char *) malloc(len); - strncpy(conn->client_session_id, client_session_id, len); + conn->client_session_id = (char *) calloc(strlen(client_session_id) + 1, sizeof(char)); + assert(conn->client_session_id); + memcpy(conn->client_session_id, client_session_id, strlen(client_session_id)); /* airplay video has been requested: shut down any running RAOP udp services */ raop_conn_t *raop_conn = (raop_conn_t *) httpd_get_connection_by_type(conn->raop->httpd, CONNECTION_TYPE_RAOP, 1); if (raop_conn) { @@ -461,7 +460,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) { } http_response_finish(*response, response_data, response_datalen); - int len; + int len = 0; const char *data = http_response_get_data(*response, &len); if (response_data && response_datalen > 0) { len -= response_datalen; @@ -620,9 +619,8 @@ raop_init(raop_callbacks_t *callbacks) { int raop_init2(raop_t *raop, int nohold, const char *device_id, const char *keyfile) { - pairing_t *pairing; - httpd_t *httpd; - httpd_callbacks_t httpd_cbs; + pairing_t *pairing = NULL; + httpd_t *httpd = NULL; /* create a new public key for pairing */ int new_key; @@ -636,7 +634,7 @@ raop_init2(raop_t *raop, int nohold, const char *device_id, const char *keyfile) #ifdef PK strncpy(raop->pk_str, PK, 2*ED25519_KEY_SIZE); #else - unsigned char public_key[ED25519_KEY_SIZE]; + unsigned char public_key[ED25519_KEY_SIZE] = { '\0' }; pairing_get_public_key(pairing, public_key); char *pk_str = utils_hex_to_string(public_key, ED25519_KEY_SIZE); strncpy(raop->pk_str, (const char *) pk_str, 2*ED25519_KEY_SIZE); @@ -647,13 +645,14 @@ raop_init2(raop_t *raop, int nohold, const char *device_id, const char *keyfile) } /* Set HTTP callbacks to our handlers */ + httpd_callbacks_t httpd_cbs; memset(&httpd_cbs, 0, sizeof(httpd_cbs)); httpd_cbs.opaque = raop; httpd_cbs.conn_init = &conn_init; httpd_cbs.conn_request = &conn_request; httpd_cbs.conn_destroy = &conn_destroy; - /* Initialize the http daemon */ + /* Initialize the http daemon, (this will take a copy of httpd_cbs) */ httpd = httpd_init(raop->logger, &httpd_cbs, nohold); if (!httpd) { logger_log(raop->logger, LOGGER_ERR, "failed to initialize http daemon"); @@ -800,7 +799,7 @@ raop_set_lang(raop_t *raop, const char *lang) { } if (lang && strlen(lang)) { raop->lang = (char *) calloc(strlen(lang) + 1, sizeof(char)); - memcpy(raop->lang, lang, strlen(lang) * sizeof(char)); + memcpy(raop->lang, lang, strlen(lang)); } } @@ -889,4 +888,3 @@ int get_playlist_by_uuid(raop_t *raop, const char *uuid) { uint64_t get_local_time() { return raop_ntp_get_local_time(); } - diff --git a/lib/raop_handlers.h b/lib/raop_handlers.h index 4fa61a8..054f638 100644 --- a/lib/raop_handlers.h +++ b/lib/raop_handlers.h @@ -51,7 +51,7 @@ raop_handler_info(raop_conn_t *conn, url = http_request_get_url(request); content_type = http_request_get_header(request, "Content-Type"); cseq = http_request_get_header(request, "CSeq"); - int len; + int len = 0; bool add_txt_airplay = false; bool add_txt_raop = false; const char txtRAOP[] = "txtRAOP"; @@ -252,7 +252,7 @@ raop_handler_pairpinstart(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { logger_log(conn->raop->logger, LOGGER_INFO, "client sent PAIR-PIN-START request"); - int pin_4; + int pin_4 = 0; if (conn->raop->pin > 9999) { pin_4 = conn->raop->pin % 10000; } else { @@ -263,7 +263,7 @@ raop_handler_pairpinstart(raop_conn_t *conn, conn->raop->pin = (unsigned short) pin_4 % 10000; } } - char pin[6]; + char pin[6] = { '\0' }; snprintf(pin, 5, "%04u", pin_4); if (conn->raop->callbacks.display_pin) { conn->raop->callbacks.display_pin(conn->raop->callbacks.cls, pin); @@ -309,10 +309,10 @@ raop_handler_pairsetup_pin(raop_conn_t *conn, if (PLIST_IS_STRING(req_method_node) && PLIST_IS_STRING(req_user_node)) { /* this is the initial pair-setup-pin request */ - const char *salt; - char pin[6]; - const char *pk; - int len_pk, len_salt; + const char *salt = NULL; + char pin[6] = { '\0' }; + const char *pk = NULL; + int len_pk = 0, len_salt = 0; char *method = NULL; char *user = NULL; plist_get_string_val(req_method_node, &method); @@ -354,10 +354,10 @@ raop_handler_pairsetup_pin(raop_conn_t *conn, /* this is the second part of pair-setup-pin request */ char *client_pk = NULL; char *client_proof = NULL; - unsigned char proof[64]; + unsigned char proof[64] = { '\0' }; memset(proof, 0, sizeof(proof)); - uint64_t client_pk_len; - uint64_t client_proof_len; + uint64_t client_pk_len = 0; + uint64_t client_proof_len = 0; plist_get_data_val(req_pk_node, &client_pk, &client_pk_len); plist_get_data_val(req_proof_node, &client_proof, &client_proof_len); if (logger_debug) { @@ -391,11 +391,11 @@ raop_handler_pairsetup_pin(raop_conn_t *conn, /* this is the third part of pair-setup-pin request */ char *client_epk = NULL; char *client_authtag = NULL; - uint64_t client_epk_len; - uint64_t client_authtag_len; + uint64_t client_epk_len = 0; + uint64_t client_authtag_len = 0; unsigned char epk[ED25519_KEY_SIZE]; unsigned char authtag[GCM_AUTHTAG_SIZE]; - int ret; + int ret = 0; plist_get_data_val(req_epk_node, &client_epk, &client_epk_len); plist_get_data_val(req_authtag_node, &client_authtag, &client_authtag_len); @@ -441,7 +441,7 @@ raop_handler_pairsetup(raop_conn_t *conn, { unsigned char public_key[ED25519_KEY_SIZE]; //const char *data; - int datalen; + int datalen = 0; //data = http_request_get_data(request, &datalen); @@ -453,7 +453,7 @@ raop_handler_pairsetup(raop_conn_t *conn, pairing_get_public_key(conn->raop->pairing, public_key); pairing_session_set_setup_status(conn->session); - *response_data = malloc(sizeof(public_key)); + *response_data = calloc(1, sizeof(public_key)); if (*response_data) { http_response_add_header(response, "Content-Type", "application/octet-stream"); memcpy(*response_data, public_key, sizeof(public_key)); @@ -477,8 +477,8 @@ raop_handler_pairverify(raop_conn_t *conn, } unsigned char public_key[X25519_KEY_SIZE]; unsigned char signature[PAIRING_SIG_SIZE]; - const unsigned char *data; - int datalen; + const unsigned char *data = NULL; + int datalen = 0; data = (unsigned char *) http_request_get_data(request, &datalen); if (datalen < 4) { @@ -515,7 +515,7 @@ raop_handler_pairverify(raop_conn_t *conn, return; } } - *response_data = malloc(sizeof(public_key) + sizeof(signature)); + *response_data = calloc(1, sizeof(public_key) + sizeof(signature)); if (*response_data) { http_response_add_header(response, "Content-Type", "application/octet-stream"); memcpy(*response_data, public_key, sizeof(public_key)); @@ -546,12 +546,12 @@ raop_handler_fpsetup(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { - const unsigned char *data; - int datalen; + const unsigned char *data = NULL; + int datalen = 0; data = (unsigned char *) http_request_get_data(request, &datalen); if (datalen == 16) { - *response_data = malloc(142); + *response_data = calloc(142, sizeof(char)); if (*response_data) { http_response_add_header(response, "Content-Type", "application/octet-stream"); if (!fairplay_setup(conn->fairplay, data, (unsigned char *) *response_data)) { @@ -563,7 +563,7 @@ raop_handler_fpsetup(raop_conn_t *conn, } } } else if (datalen == 164) { - *response_data = malloc(32); + *response_data = calloc(32, sizeof(char)); if (*response_data) { http_response_add_header(response, "Content-Type", "application/octet-stream"); if (!fairplay_handshake(conn->fairplay, data, (unsigned char *) *response_data)) { @@ -649,7 +649,7 @@ raop_handler_setup(raop_conn_t *conn, conn->raop->random_pw = NULL; } } - int len; + int len = 0; const char *password = conn->raop->callbacks.passwd(conn->raop->callbacks.cls, &len); // len = -1 means use a random password for this connection; len = 0 means no password if (len == -1 && conn->raop->random_pw && conn->raop->auth_fail_count >= MAX_PW_ATTEMPTS) { @@ -665,7 +665,7 @@ raop_handler_setup(raop_conn_t *conn, logger_log(conn->raop->logger, LOGGER_ERR, "Failed to generate random pin"); pin_4 = 1234; } - conn->raop->random_pw = (char *) malloc(pin_len + 1 + 18); + conn->raop->random_pw = (char *) calloc(pin_len + 1 + 18, sizeof(char)); char *pin = conn->raop->random_pw; snprintf(pin, pin_len + 1, "%04u", pin_4 % 10000); pin[pin_len] = '\0'; @@ -1057,9 +1057,9 @@ raop_handler_get_parameter(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { - const char *content_type; - const char *data; - int datalen; + const char *content_type = NULL; + const char *data = NULL; + int datalen = 0; content_type = http_request_get_header(request, "Content-Type"); if (!content_type) { @@ -1112,9 +1112,9 @@ raop_handler_set_parameter(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { - const char *content_type; - const char *data; - int datalen; + const char *content_type = NULL; + const char *data = NULL; + int datalen = 0; content_type = http_request_get_header(request, "Content-Type"); if (!content_type) { @@ -1124,15 +1124,15 @@ raop_handler_set_parameter(raop_conn_t *conn, data = http_request_get_data(request, &datalen); if (!strcmp(content_type, "text/parameters")) { char *datastr; - datastr = calloc(1, datalen+1); + datastr = calloc(1, datalen + 1); if (data && datastr && conn->raop_rtp) { memcpy(datastr, data, datalen); if ((datalen >= 8) && !strncmp(datastr, "volume: ", 8)) { - float vol = 0.0; + float vol = 0.0f; sscanf(datastr+8, "%f", &vol); raop_rtp_set_volume(conn->raop_rtp, vol); } else if ((datalen >= 10) && !strncmp(datastr, "progress: ", 10)) { - uint32_t start, curr, end; + uint32_t start = 0, curr = 0, end = 0; sscanf(datastr+10, "%"PRIu32"/%"PRIu32"/%"PRIu32, &start, &curr, &end); raop_rtp_set_progress(conn->raop_rtp, start, curr, end); } @@ -1164,7 +1164,7 @@ raop_handler_audiomode(raop_conn_t *conn, { const char *data = NULL; char *audiomode = NULL; - int data_len; + int data_len = 0; data = http_request_get_data(request, &data_len); plist_t req_root_node = NULL; plist_from_bin(data, data_len, &req_root_node); @@ -1192,7 +1192,7 @@ raop_handler_record(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { - char audio_latency[12]; + char audio_latency[12] = { '\0' }; unsigned int ad = (unsigned int) (((uint64_t) conn->raop->audio_delay_micros) * AUDIO_SAMPLE_RATE / SECOND_IN_USECS); snprintf(audio_latency, sizeof(audio_latency), "%u", ad); logger_log(conn->raop->logger, LOGGER_DEBUG, "raop_handler_record"); @@ -1205,7 +1205,7 @@ raop_handler_flush(raop_conn_t *conn, http_request_t *request, http_response_t *response, char **response_data, int *response_datalen) { - const char *rtpinfo; + const char *rtpinfo = NULL; int next_seq = -1; rtpinfo = http_request_get_header(request, "RTP-Info"); @@ -1228,8 +1228,8 @@ raop_handler_teardown(raop_conn_t *conn, char **response_data, int *response_datalen) { /* get the teardown request type(s): (type 96, 110, or none) */ - const char *data; - int data_len; + const char *data = NULL; + int data_len = 0; bool teardown_96 = false, teardown_110 = false; data = http_request_get_data(request, &data_len); plist_t req_root_node = NULL; @@ -1237,7 +1237,7 @@ raop_handler_teardown(raop_conn_t *conn, plist_t req_streams_node = plist_dict_get_item(req_root_node, "streams"); /* Process stream teardown requests */ if (PLIST_IS_ARRAY(req_streams_node)) { - uint64_t val; + uint64_t val = 0; int count = plist_array_get_size(req_streams_node); for (int i = 0; i < count; i++) { plist_t req_stream_node = plist_array_get_item(req_streams_node,i);