From 68a471c0df06352c23d512e5a98719017e439f8c Mon Sep 17 00:00:00 2001 From: jcorporation Date: Mon, 14 Jan 2019 23:56:21 +0000 Subject: [PATCH] Fix: optimize compile options Fix: fix compiler warnings --- CMakeLists.txt | 2 +- src/global.c | 2 +- src/global.h.in | 2 +- src/mpd_client.c | 12 ++++++------ src/mympd_api.c | 1 - src/tiny_queue.c | 2 +- src/tiny_queue.h | 2 +- src/web_server.c | 2 +- 8 files changed, 12 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 17b3e17..aee6355 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ include_directories(${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} ${LIBMPDCLIENT_I include(CheckCSourceCompiles) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wextra -pedantic -D MG_ENABLE_SSL -D MG_ENABLE_THREADS -D MG_ENABLE_IPV6 -D MG_DISABLE_MQTT -D MG_DISABLE_MQTT_BROKER -D MG_DISABLE_DNS_SERVER -D MG_DISABLE_COAP -D MG_DISABLE_HTTP_CGI -D MG_DISABLE_HTTP_SSI -D MG_DISABLE_HTTP_WEBDAV") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -O2 -Wall -Wextra -pedantic -Wformat=2 -Wno-unused-parameter -Wshadow -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls -Wnested-externs -Wmissing-include-dirs -D MG_ENABLE_SSL -D MG_ENABLE_THREADS -D MG_ENABLE_IPV6 -D MG_DISABLE_MQTT -D MG_DISABLE_MQTT_BROKER -D MG_DISABLE_DNS_SERVER -D MG_DISABLE_COAP -D MG_DISABLE_HTTP_CGI -D MG_DISABLE_HTTP_SSI -D MG_DISABLE_HTTP_WEBDAV") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -D_FORTIFY_SOURCE=2 -fstack-protector -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=shift -fsanitize=integer-divide-by-zero -fsanitize=unreachable -fsanitize=vla-bound -fsanitize=null -fsanitize=return -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=bounds-strict -fsanitize=alignment -fsanitize=object-size -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=nonnull-attribute -fsanitize=returns-nonnull-attribute -fsanitize=bool -fsanitize=enum -fsanitize=vptr -static-libasan") find_package(OpenSSL REQUIRED) diff --git a/src/global.c b/src/global.c index 1743762..dc19730 100644 --- a/src/global.c +++ b/src/global.c @@ -81,7 +81,7 @@ bool state_file_write(t_config *config, const char *name, const char *value) { } -bool testdir(char *name, char *dirname) { +bool testdir(const char *name, const char *dirname) { DIR* dir = opendir(dirname); if (dir) { closedir(dir); diff --git a/src/global.h.in b/src/global.h.in index 6911700..68a863c 100644 --- a/src/global.h.in +++ b/src/global.h.in @@ -187,7 +187,7 @@ typedef struct t_config { } t_config; //global functions -bool testdir(char *name, char *dirname); +bool testdir(const char *name, const char *dirname); int randrange(int n); bool validate_string(const char *data); int copy_string(char * const dest, char const * const src, size_t const dst_len, size_t const src_len); diff --git a/src/mpd_client.c b/src/mpd_client.c index 0f7647a..78c7edb 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -55,13 +55,13 @@ } while (0) #define PUT_SONG_TAGS() do { \ - struct node *current = mpd_state->mympd_tags.list; \ + struct node *current_tag = mpd_state->mympd_tags.list; \ int tagnr = 0; \ - while (current != NULL) { \ + while (current_tag != NULL) { \ if (tagnr ++) \ len += json_printf(&out, ","); \ - len += json_printf(&out, "%Q: %Q", current->data, mpd_client_get_tag(song, mpd_tag_name_parse(current->data))); \ - current = current->next; \ + len += json_printf(&out, "%Q: %Q", current_tag->data, mpd_client_get_tag(song, mpd_tag_name_parse(current_tag->data))); \ + current_tag = current_tag->next; \ } \ len += json_printf(&out, ", Duration: %d, uri: %Q", mpd_song_get_duration(song), mpd_song_get_uri(song)); \ } while (0) @@ -1402,7 +1402,7 @@ static char *mpd_client_get_tag(struct mpd_song const *song, const enum mpd_tag_ else if (tag == MPD_TAG_ALBUM_ARTIST) str = (char *)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); else - str = "-"; + str = strdup("-"); } return str; } @@ -1623,7 +1623,7 @@ static int mpd_client_put_volume(t_mpd_state *mpd_state, char *buffer) { } static int mpd_client_put_settings(t_mpd_state *mpd_state, char *buffer) { - char *replaygain = ""; + char *replaygain = strdup(""); size_t len; int nr = 0; struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); diff --git a/src/mympd_api.c b/src/mympd_api.c index 47c7301..1574bbe 100644 --- a/src/mympd_api.c +++ b/src/mympd_api.c @@ -168,7 +168,6 @@ static void mympd_api(t_config *config, t_mympd_state *mympd_state, t_work_reque else { len = snprintf(buffer, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Unknown table %s\"}", p_charbuf1); printf("MYMPD_API_COLS_SAVE: Unknown table %s\n", p_charbuf1); - free(p_charbuf1); } if (len == 0) { if (state_file_write(config, p_charbuf1, cols)) diff --git a/src/tiny_queue.c b/src/tiny_queue.c index 2389bc0..17e3744 100644 --- a/src/tiny_queue.c +++ b/src/tiny_queue.c @@ -23,7 +23,7 @@ #include #include "tiny_queue.h" -tiny_queue_t* tiny_queue_create() { +tiny_queue_t *tiny_queue_create(void) { struct tiny_queue_t* queue = (struct tiny_queue_t*)malloc(sizeof(struct tiny_queue_t)); queue->head = NULL; queue->tail = NULL; diff --git a/src/tiny_queue.h b/src/tiny_queue.h index 7a94124..64efeda 100644 --- a/src/tiny_queue.h +++ b/src/tiny_queue.h @@ -33,7 +33,7 @@ typedef struct tiny_queue_t { pthread_cond_t wakeup; } tiny_queue_t; -tiny_queue_t* tiny_queue_create(); +tiny_queue_t *tiny_queue_create(void); void tiny_queue_free(tiny_queue_t *queue); void tiny_queue_push(struct tiny_queue_t *queue, void *data); void *tiny_queue_shift(struct tiny_queue_t *queue); diff --git a/src/web_server.c b/src/web_server.c index aceddf8..8ac139f 100644 --- a/src/web_server.c +++ b/src/web_server.c @@ -196,7 +196,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { bool rc = handle_api(user_data->conn_id, hm->body.p, hm->body.len); if (rc == false) { printf("ERROR: Invalid API request.\n"); - char *response = "{\"type\": \"error\", \"data\": \"Invalid API request\"}"; + const char *response = "{\"type\": \"error\", \"data\": \"Invalid API request\"}"; mg_send_head(nc, 200, strlen(response), "Content-Type: application/json"); mg_printf(nc, "%s", response); }