Commit c1f0eaff authored by Philipp Schafft's avatar Philipp Schafft 🦁

Fix: Fixed some smaller bugs such as leaks and performance improvements.

This is a first commit to correct minor problmes reported by
Denis Denisov. There is likely another commit to correct some more
such problems.

Thank you Denis Denisov.
parent 74023834
...@@ -644,7 +644,6 @@ static void command_move_clients(client_t *client, ...@@ -644,7 +644,6 @@ static void command_move_clients(client_t *client,
source_move_clients(source, dest); source_move_clients(source, dest);
memset(buf, '\000', sizeof(buf));
snprintf(buf, sizeof(buf), "Clients moved from %s to %s", snprintf(buf, sizeof(buf), "Clients moved from %s to %s",
source->mount, dest_source); source->mount, dest_source);
xmlNewChild(node, NULL, XMLSTR("message"), XMLSTR(buf)); xmlNewChild(node, NULL, XMLSTR("message"), XMLSTR(buf));
...@@ -687,7 +686,6 @@ static inline xmlNodePtr __add_listener(client_t *client, ...@@ -687,7 +686,6 @@ static inline xmlNodePtr __add_listener(client_t *client,
if (tmp) if (tmp)
xmlNewChild(node, NULL, XMLSTR("referer"), XMLSTR(tmp)); xmlNewChild(node, NULL, XMLSTR("referer"), XMLSTR(tmp));
memset(buf, '\000', sizeof(buf));
snprintf(buf, sizeof(buf), "%lu", (unsigned long)(now - client->con->con_time)); snprintf(buf, sizeof(buf), "%lu", (unsigned long)(now - client->con->con_time));
xmlNewChild(node, NULL, XMLSTR(mode == OMODE_LEGACY ? "Connected" : "connected"), XMLSTR(buf)); xmlNewChild(node, NULL, XMLSTR(mode == OMODE_LEGACY ? "Connected" : "connected"), XMLSTR(buf));
......
...@@ -1955,7 +1955,6 @@ static void _parse_paths(xmlDocPtr doc, ...@@ -1955,7 +1955,6 @@ static void _parse_paths(xmlDocPtr doc,
free(alias); free(alias);
continue; continue;
} }
temp = NULL;
temp = (char *)xmlGetProp(node, XMLSTR("port")); temp = (char *)xmlGetProp(node, XMLSTR("port"));
if(temp != NULL) { if(temp != NULL) {
alias->port = atoi(temp); alias->port = atoi(temp);
......
...@@ -212,23 +212,21 @@ static refbuf_t *ebml_get_buffer(source_t *source) ...@@ -212,23 +212,21 @@ static refbuf_t *ebml_get_buffer(source_t *source)
static int ebml_create_client_data(source_t *source, client_t *client) static int ebml_create_client_data(source_t *source, client_t *client)
{ {
ebml_client_data_t *ebml_client_data;
ebml_client_data_t *ebml_client_data = calloc(1, sizeof(ebml_client_data_t));
ebml_source_state_t *ebml_source_state = source->format->_state; ebml_source_state_t *ebml_source_state = source->format->_state;
int ret = -1; if (!ebml_source_state->header)
return -1;
if ((ebml_client_data) && (ebml_source_state->header))
{
ebml_client_data->header = ebml_source_state->header;
refbuf_addref(ebml_client_data->header);
client->format_data = ebml_client_data;
client->free_client_data = ebml_free_client_data;
ret = 0;
}
return ret; ebml_client_data = calloc(1, sizeof(ebml_client_data_t));
if (!ebml_client_data)
return -1;
ebml_client_data->header = ebml_source_state->header;
refbuf_addref(ebml_client_data->header);
client->format_data = ebml_client_data;
client->free_client_data = ebml_free_client_data;
return 0;
} }
......
...@@ -277,12 +277,16 @@ static void mp3_set_title(source_t *source) ...@@ -277,12 +277,16 @@ static void mp3_set_title(source_t *source)
int r; int r;
memset (p->data, '\0', size); memset (p->data, '\0', size);
if (url_artist && url_title) if (url_artist && url_title) {
r = snprintf (p->data, size, "%c%s%s - %s';", len_byte, streamtitle, r = snprintf (p->data, size, "%c%s%s - %s';", len_byte, streamtitle,
url_artist, url_title); url_artist, url_title);
else } else if (url_title) {
r = snprintf (p->data, size, "%c%s%s';", len_byte, streamtitle, r = snprintf (p->data, size, "%c%s%s';", len_byte, streamtitle,
url_title); url_title);
} else {
r = snprintf (p->data, size, "%c%s';", len_byte, streamtitle);
}
if (r > 0) if (r > 0)
{ {
if (source_mp3->inline_url) if (source_mp3->inline_url)
......
...@@ -150,16 +150,21 @@ int fserve_client_waiting (void) ...@@ -150,16 +150,21 @@ int fserve_client_waiting (void)
/* only rebuild ufds if there are clients added/removed */ /* only rebuild ufds if there are clients added/removed */
if (client_tree_changed) if (client_tree_changed)
{ {
client_tree_changed = 0; struct pollfd *ufds_new = realloc(ufds, fserve_clients * sizeof(struct pollfd));
ufds = realloc(ufds, fserve_clients * sizeof(struct pollfd)); /* REVIEW: If we can not allocate new ufds, keep old ones for now. */
fclient = active_list; if (ufds_new) {
while (fclient) ufds = ufds_new;
{ client_tree_changed = 0;
ufds[i].fd = fclient->client->con->sock; ufds = ufds_new;
ufds[i].events = POLLOUT; fclient = active_list;
ufds[i].revents = 0; while (fclient)
fclient = fclient->next; {
i++; ufds[i].fd = fclient->client->con->sock;
ufds[i].events = POLLOUT;
ufds[i].revents = 0;
fclient = fclient->next;
i++;
}
} }
} }
if (!ufds) if (!ufds)
...@@ -466,6 +471,7 @@ int fserve_client_create (client_t *httpclient, const char *path) ...@@ -466,6 +471,7 @@ int fserve_client_create (client_t *httpclient, const char *path)
if (ret == -1 || ret >= (BUFSIZE - 512)) { /* we want at least 512 bytes left for the content of the playlist */ if (ret == -1 || ret >= (BUFSIZE - 512)) { /* we want at least 512 bytes left for the content of the playlist */
ICECAST_LOG_ERROR("Dropping client as we can not build response headers."); ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
client_send_error(httpclient, 500, 0, "Header generation failed."); client_send_error(httpclient, 500, 0, "Header generation failed.");
free(sourceuri);
return -1; return -1;
} }
if (host == NULL) if (host == NULL)
...@@ -612,6 +618,7 @@ int fserve_client_create (client_t *httpclient, const char *path) ...@@ -612,6 +618,7 @@ int fserve_client_create (client_t *httpclient, const char *path)
if (bytes == -1 || bytes >= (BUFSIZE - 512)) { /* we want at least 512 bytes left */ if (bytes == -1 || bytes >= (BUFSIZE - 512)) { /* we want at least 512 bytes left */
ICECAST_LOG_ERROR("Dropping client as we can not build response headers."); ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
client_send_error(httpclient, 500, 0, "Header generation failed."); client_send_error(httpclient, 500, 0, "Header generation failed.");
fclose(file);
return -1; return -1;
} }
bytes += snprintf (httpclient->refbuf->data + bytes, BUFSIZE - bytes, bytes += snprintf (httpclient->refbuf->data + bytes, BUFSIZE - bytes,
......
...@@ -47,7 +47,7 @@ int playlistlog = 0; ...@@ -47,7 +47,7 @@ int playlistlog = 0;
int get_clf_time (char *buffer, unsigned len, struct tm *t) int get_clf_time (char *buffer, unsigned len, struct tm *t)
{ {
char sign; char sign;
char *timezone_string; char timezone_string[7];
struct tm gmt; struct tm gmt;
time_t time1 = time(NULL); time_t time1 = time(NULL);
int time_days, time_hours, time_tz; int time_days, time_hours, time_tz;
...@@ -65,6 +65,11 @@ int get_clf_time (char *buffer, unsigned len, struct tm *t) ...@@ -65,6 +65,11 @@ int get_clf_time (char *buffer, unsigned len, struct tm *t)
#endif #endif
/* FIXME: bail out if gmtime* returns NULL */ /* FIXME: bail out if gmtime* returns NULL */
if (!thetime) {
snprintf(buffer, len, "<<BAD TIMESTAMP>>");
return 0;
}
time_days = t->tm_yday - gmt.tm_yday; time_days = t->tm_yday - gmt.tm_yday;
if (time_days < -1) { if (time_days < -1) {
...@@ -91,15 +96,13 @@ int get_clf_time (char *buffer, unsigned len, struct tm *t) ...@@ -91,15 +96,13 @@ int get_clf_time (char *buffer, unsigned len, struct tm *t)
sign = '+'; sign = '+';
} }
timezone_string = calloc(1, 7); snprintf(timezone_string, sizeof(timezone_string), " %c%.2d%.2d", sign, time_tz / 60, time_tz % 60);
snprintf(timezone_string, 7, " %c%.2d%.2d", sign, time_tz / 60, time_tz % 60);
now = time(NULL); now = time(NULL);
thetime = localtime(&now); thetime = localtime(&now);
strftime (buffer, len-7, "%d/%b/%Y:%H:%M:%S", thetime); strftime(buffer, len - sizeof(timezone_string), "%d/%b/%Y:%H:%M:%S", thetime);
strcat(buffer, timezone_string); strcat(buffer, timezone_string);
free(timezone_string);
return 1; return 1;
} }
#endif #endif
......
...@@ -627,7 +627,6 @@ static void source_init (source_t *source) ...@@ -627,7 +627,6 @@ static void source_init (source_t *source)
strlen(":") + 6 + strlen(source->mount) + 1; strlen(":") + 6 + strlen(source->mount) + 1;
listenurl = malloc (listen_url_size); listenurl = malloc (listen_url_size);
memset (listenurl, '\000', listen_url_size);
snprintf (listenurl, listen_url_size, "http://%s:%d%s", snprintf (listenurl, listen_url_size, "http://%s:%d%s",
config->hostname, config->port, source->mount); config->hostname, config->port, source->mount);
config_release_config(); config_release_config();
......
...@@ -333,7 +333,7 @@ void stats_event_add(const char *source, const char *name, unsigned long value) ...@@ -333,7 +333,7 @@ void stats_event_add(const char *source, const char *name, unsigned long value)
if (event) if (event)
{ {
event->value = malloc (16); event->value = malloc (16);
snprintf (event->value, 16, "%ld", value); snprintf(event->value, 16, "%lu", value);
event->action = STATS_EVENT_ADD; event->action = STATS_EVENT_ADD;
queue_global_event (event); queue_global_event (event);
} }
...@@ -345,7 +345,7 @@ void stats_event_sub(const char *source, const char *name, unsigned long value) ...@@ -345,7 +345,7 @@ void stats_event_sub(const char *source, const char *name, unsigned long value)
if (event) if (event)
{ {
event->value = malloc (16); event->value = malloc (16);
snprintf (event->value, 16, "%ld", value); snprintf(event->value, 16, "%lu", value);
event->action = STATS_EVENT_SUB; event->action = STATS_EVENT_SUB;
queue_global_event (event); queue_global_event (event);
} }
......
...@@ -563,7 +563,8 @@ static inline void _build_headers_loop(char **ret, size_t *len, ice_config_htt ...@@ -563,7 +563,8 @@ static inline void _build_headers_loop(char **ret, size_t *len, ice_config_htt
size_t headerlen; size_t headerlen;
const char *name; const char *name;
const char *value; const char *value;
char * r = *ret; char *r = *ret;
char *n;
if (!header) if (!header)
return; return;
...@@ -590,11 +591,17 @@ static inline void _build_headers_loop(char **ret, size_t *len, ice_config_htt ...@@ -590,11 +591,17 @@ static inline void _build_headers_loop(char **ret, size_t *len, ice_config_htt
/* append the header to the buffer */ /* append the header to the buffer */
headerlen = strlen(name) + strlen(value) + 4; headerlen = strlen(name) + strlen(value) + 4;
*len += headerlen; *len += headerlen;
r = realloc(r, *len); n = realloc(r, *len);
strcat(r, name); if (n) {
strcat(r, ": "); r = n;
strcat(r, value); strcat(r, name);
strcat(r, "\r\n"); strcat(r, ": ");
strcat(r, value);
strcat(r, "\r\n");
} else {
/* FIXME: we skip this header. We should do better. */
*len -= headerlen;
}
} while ((header = header->next)); } while ((header = header->next));
*ret = r; *ret = r;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment