From a2d263356e272cd04e3201e4134ade8cc25ebc17 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Sat, 19 May 2018 09:27:50 +0200 Subject: [PATCH] configure.ac: enable -Wwrite-strings As discussed in bug #185 [1], const strings should be protected from being overwritten. This change adds -Wwrite-strings to the compiler warning flags and fixes resulting warnings by making static strings const. [1] https://github.com/obgm/libcoap/issues/185 --- configure.ac | 1 + examples/client.c | 4 ++-- include/coap/pdu.h | 2 +- src/debug.c | 4 ++-- src/net.c | 2 +- src/pdu.c | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 3cb7969c..7285c03e 100644 --- a/configure.ac +++ b/configure.ac @@ -137,6 +137,7 @@ WARNING_CFLAGS="\ -Wswitch-default \ -Wswitch-enum \ -Wunused \ +-Wwrite-strings \ " # check whether or not the compiler supports -Wlogical-op (clang does not...) diff --git a/examples/client.c b/examples/client.c index 2e68ec53..a17dfbfc 100644 --- a/examples/client.c +++ b/examples/client.c @@ -559,7 +559,7 @@ usage( const char *program, const char *version) { typedef struct { unsigned char code; - char *media_type; + const char *media_type; } content_type_t; static void @@ -968,7 +968,7 @@ cmdline_input_from_file(char *filename, str *buf) { static method_t cmdline_method(char *arg) { - static char *methods[] = + static const char *methods[] = { 0, "get", "post", "put", "delete", "fetch", "patch", "ipatch", 0}; unsigned char i; diff --git a/include/coap/pdu.h b/include/coap/pdu.h index 827d38eb..afe50eb2 100644 --- a/include/coap/pdu.h +++ b/include/coap/pdu.h @@ -132,7 +132,7 @@ struct coap_session_t; * @return A zero-terminated string describing the error, or @c NULL if not * found. */ -char *coap_response_phrase(unsigned char code); +const char *coap_response_phrase(unsigned char code); #define COAP_ERROR_PHRASE_LENGTH 32 /**< maximum length of error phrase */ diff --git a/src/debug.c b/src/debug.c index d82ab9b5..f8a7bf74 100644 --- a/src/debug.c +++ b/src/debug.c @@ -71,7 +71,7 @@ coap_set_log_level(coap_log_t level) { } /* this array has the same order as the type log_t */ -static char *loglevels[] = { +static const char *loglevels[] = { "EMRG", "ALRT", "CRIT", "ERR", "WARN", "NOTE", "INFO", "DEBG" }; @@ -265,7 +265,7 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le /** Returns a textual description of the message type @p t. */ static const char * msg_type_string(uint16_t t) { - static char *types[] = { "CON", "NON", "ACK", "RST", "???" }; + static const char *types[] = { "CON", "NON", "ACK", "RST", "???" }; return types[min(t, sizeof(types)/sizeof(char *) - 1)]; } diff --git a/src/net.c b/src/net.c index 1fe4265e..08d951e0 100644 --- a/src/net.c +++ b/src/net.c @@ -1426,7 +1426,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, uint16_t opt_type = 0; /* used for calculating delta-storage */ #if COAP_ERROR_PHRASE_LENGTH > 0 - char *phrase = coap_response_phrase(code); + const char *phrase = coap_response_phrase(code); /* Need some more space for the error phrase and payload start marker */ if (phrase) diff --git a/src/pdu.c b/src/pdu.c index 0722bbb2..0ab2cb7a 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -344,7 +344,7 @@ coap_get_data(coap_pdu_t *pdu, size_t *len, uint8_t **data) { #ifndef SHORT_ERROR_RESPONSE typedef struct { unsigned char code; - char *phrase; + const char *phrase; } error_desc_t; /* if you change anything here, make sure, that the longest string does not @@ -376,7 +376,7 @@ error_desc_t coap_error[] = { { 0, NULL } /* end marker */ }; -char * +const char * coap_response_phrase(unsigned char code) { int i; for (i = 0; coap_error[i].code; ++i) {