Просмотр исходного кода

Merge branch 'feature/wwrite_string_for_idf' into 'master'

Enable -Wwrite-strings when compiling IDF's own C files

See merge request idf/esp-idf!2193
Ivan Grokhotkov 7 лет назад
Родитель
Сommit
bdadd95dd7

+ 14 - 0
Kconfig

@@ -148,6 +148,20 @@ config STACK_CHECK
     help
         Stack smashing protection.
 
+config WARN_WRITE_STRINGS
+    bool "Enable -Wwrite-strings warning flag"
+    default "n"
+    help
+        Adds -Wwrite-strings flag for the C/C++ compilers.
+
+        For C, this gives string constants the type "const char[]" so that
+        copying the address of one into a non-"const" "char *" pointer
+        produces a warning. This warning helps to find at compile time code
+        that tries to write into a string constant.
+
+        For C++, this warns about the deprecated conversion from string
+        literals to "char *".
+
 endmenu # Compiler Options
 
 menu "Component config"

+ 2 - 2
components/bootloader/subproject/main/bootloader_start.c

@@ -141,7 +141,7 @@ bool load_partition_table(bootloader_state_t* bs)
 {
     const esp_partition_info_t *partitions;
     const int ESP_PARTITION_TABLE_DATA_LEN = 0xC00; /* length of actual data (signature is appended to this) */
-    char *partition_usage;
+    const char *partition_usage;
     esp_err_t err;
     int num_partitions;
 
@@ -900,7 +900,7 @@ static void wdt_reset_info_dump(int cpu)
 {
     uint32_t inst = 0, pid = 0, stat = 0, data = 0, pc = 0,
              lsstat = 0, lsaddr = 0, lsdata = 0, dstat = 0;
-    char *cpu_name = cpu ? "APP" : "PRO";
+    const char *cpu_name = cpu ? "APP" : "PRO";
 
     if (cpu == 0) {
         stat    = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_STATUS_REG);

+ 1 - 1
components/bt/bluedroid/btc/core/btc_ble_storage.c

@@ -392,7 +392,7 @@ bool _btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr,
 {
     bdstr_t bdstr;
     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
-    char *key_type_str;
+    const char *key_type_str;
     switch (key_type) {
     case BTM_LE_KEY_PENC:
         key_type_str = BTC_BLE_STORAGE_LE_KEY_PENC_STR;

+ 1 - 1
components/bt/bluedroid/btc/core/btc_config.c

@@ -36,7 +36,7 @@ static void btc_key_value_to_string(uint8_t *key_value, char *value_str, int key
 static osi_mutex_t lock;  // protects operations on |config|.
 static config_t *config;
 
-bool btc_compare_address_key_value(const char *section, char *key_type, void *key_value, int key_length)
+bool btc_compare_address_key_value(const char *section, const char *key_type, void *key_value, int key_length)
 {
     assert(key_value != NULL);
     bool status = false;

+ 1 - 1
components/bt/bluedroid/btc/include/btc_config.h

@@ -49,7 +49,7 @@ int btc_config_clear(void);
 
 // TODO(zachoverflow): Eww...we need to move these out. These are peer specific, not config general.
 bool btc_get_address_type(const BD_ADDR bd_addr, int *p_addr_type);
-bool btc_compare_address_key_value(const char *section, char *key_type, void *key_value, int key_length);
+bool btc_compare_address_key_value(const char *section, const char *key_type, void *key_value, int key_length);
 bool btc_get_device_type(const BD_ADDR bd_addr, int *p_device_type);
 
 void btc_config_lock(void);

+ 1 - 1
components/bt/bluedroid/osi/config.c

@@ -134,7 +134,7 @@ bool config_has_key(const config_t *config, const char *section, const char *key
     return (entry_find(config, section, key) != NULL);
 }
 
-bool config_has_key_in_section(config_t *config, char *key, char *key_value)
+bool config_has_key_in_section(config_t *config, const char *key, char *key_value)
 {
     LOG_DEBUG("key = %s, value = %s", key, key_value);
     for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {

+ 1 - 1
components/bt/bluedroid/osi/include/config.h

@@ -68,7 +68,7 @@ bool config_has_key(const config_t *config, const char *section, const char *key
 
 // Returns true if the config file has a key named |key| and the key_value.
 // Returns false otherwise. |config|, |key|, and |key_value| must not be NULL.
-bool config_has_key_in_section(config_t *config, char *key, char *key_value);
+bool config_has_key_in_section(config_t *config, const char *key, char *key_value);
 
 // Returns the integral value for a given |key| in |section|. If |section|
 // or |key| do not exist, or the value cannot be fully converted to an integer,

+ 3 - 3
components/bt/bluedroid/stack/btm/btm_sec.c

@@ -90,7 +90,7 @@ static tBTM_STATUS btm_sec_send_hci_disconnect (tBTM_SEC_DEV_REC *p_dev_rec, UIN
 UINT8           btm_sec_start_role_switch (tBTM_SEC_DEV_REC *p_dev_rec);
 tBTM_SEC_DEV_REC *btm_sec_find_dev_by_sec_state (UINT8 state);
 
-static BOOLEAN  btm_sec_set_security_level ( CONNECTION_TYPE conn_type, char *p_name, UINT8 service_id,
+static BOOLEAN  btm_sec_set_security_level ( CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
         UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
         UINT32 mx_chan_id);
 #if (SMP_INCLUDED == TRUE)
@@ -459,7 +459,7 @@ void BTM_SetSecureConnectionsOnly (BOOLEAN secure_connections_only_mode)
 ** Returns          TRUE if registered OK, else FALSE
 **
 *******************************************************************************/
-BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, char *p_name, UINT8 service_id,
+BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, const char *p_name, UINT8 service_id,
                               UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
                               UINT32 mx_chan_id)
 {
@@ -499,7 +499,7 @@ BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, char *p_name, UINT8 service
 ** Returns          TRUE if registered OK, else FALSE
 **
 *******************************************************************************/
-static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, char *p_name, UINT8 service_id,
+static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
         UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
         UINT32 mx_chan_id)
 {

+ 1 - 1
components/bt/bluedroid/stack/include/btm_api.h

@@ -3354,7 +3354,7 @@ void BTM_SetSecureConnectionsOnly (BOOLEAN secure_connections_only_mode);
 **
 *******************************************************************************/
 //extern
-BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, char *p_name,
+BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, const char *p_name,
                               UINT8 service_id, UINT16 sec_level,
                               UINT16 psm, UINT32 mx_proto_id,
                               UINT32 mx_chan_id);

+ 3 - 0
components/coap/component.mk

@@ -9,3 +9,6 @@ COMPONENT_OBJS = libcoap/src/address.o libcoap/src/async.o libcoap/src/block.o l
 COMPONENT_SRCDIRS := libcoap/src libcoap port
 
 COMPONENT_SUBMODULES += libcoap
+
+libcoap/src/debug.o: CFLAGS += -Wno-write-strings
+libcoap/src/pdu.o: CFLAGS += -Wno-write-strings

+ 3 - 3
components/console/argtable3/argtable3.c

@@ -4535,9 +4535,9 @@ static
 void arg_print_gnuswitch(FILE *fp, struct arg_hdr * *table)
 {
     int tabindex;
-    char *format1 = " -%c";
-    char *format2 = " [-%c";
-    char *suffix = "";
+    const char *format1 = " -%c";
+    const char *format2 = " [-%c";
+    const char *suffix = "";
 
     /* print all mandatory switches that are without argument values */
     for(tabindex = 0;

+ 1 - 1
components/esp32/gdbstub.c

@@ -66,7 +66,7 @@ static void ATTR_GDBFN gdbPacketChar(char c) {
 }
 
 //Send a string as part of a packet
-static void ATTR_GDBFN gdbPacketStr(char *c) {
+static void ATTR_GDBFN gdbPacketStr(const char *c) {
 	while (*c!=0) {
 		gdbPacketChar(*c);
 		c++;

+ 1 - 1
components/fatfs/test/test_fatfs_common.c

@@ -194,7 +194,7 @@ void test_fatfs_link_rename(const char* filename_prefix)
 
     FILE* f = fopen(name_src, "w+");
     TEST_ASSERT_NOT_NULL(f);
-    char* str = "0123456789";
+    const char* str = "0123456789";
     for (int i = 0; i < 4000; ++i) {
         TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
     }

+ 1 - 1
components/newlib/test/test_newlib.c

@@ -115,7 +115,7 @@ TEST_CASE("test asctime", "[newlib]")
     TEST_ASSERT_EQUAL_STRING(buf, time_str);
 }
 
-static bool fn_in_rom(void *fn, char *name)
+static bool fn_in_rom(void *fn, const char *name)
 {
     const int fnaddr = (int)fn;
     return (fnaddr >= 0x40000000) && (fnaddr < 0x40070000);

+ 4 - 4
components/openssl/library/ssl_lib.c

@@ -1106,9 +1106,9 @@ const char *SSL_rstate_string_long(SSL *ssl)
 /**
  * @brief get SSL statement string
  */
-char *SSL_state_string(const SSL *ssl)
+const char *SSL_state_string(const SSL *ssl)
 {
-    char *str = "UNKWN ";
+    const char *str = "UNKWN ";
 
     SSL_ASSERT2(ssl);
 
@@ -1214,9 +1214,9 @@ char *SSL_state_string(const SSL *ssl)
 /**
  * @brief get SSL statement long string
  */
-char *SSL_state_string_long(const SSL *ssl)
+const char *SSL_state_string_long(const SSL *ssl)
 {
-    char *str = "UNKWN ";
+    const char *str = "UNKWN ";
 
     SSL_ASSERT2(ssl);
 

+ 1 - 1
components/spiffs/test/test_spiffs.c

@@ -158,7 +158,7 @@ void test_spiffs_rename(const char* filename_prefix)
 
     FILE* f = fopen(name_src, "w+");
     TEST_ASSERT_NOT_NULL(f);
-    char* str = "0123456789";
+    const char* str = "0123456789";
     for (int i = 0; i < 400; ++i) {
         TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
     }

+ 2 - 2
examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c

@@ -85,9 +85,9 @@ static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
     },
 };
 
-static char *esp_key_type_to_str(esp_ble_key_type_t key_type)
+static const char *esp_key_type_to_str(esp_ble_key_type_t key_type)
 {
-   char *key_str = NULL;
+   const char *key_str = NULL;
    switch(key_type) {
     case ESP_LE_KEY_NONE:
         key_str = "ESP_LE_KEY_NONE";

+ 1 - 1
examples/protocols/http2_request/components/sh2lib/sh2lib.c

@@ -101,7 +101,7 @@ static ssize_t callback_recv(nghttp2_session *session, uint8_t *buf,
     return rv;
 }
 
-char *sh2lib_frame_type_str(int type)
+const char *sh2lib_frame_type_str(int type)
 {
     switch (type) {
     case NGHTTP2_HEADERS:

+ 2 - 2
examples/protocols/pppos_client/main/pppos_client_main.c

@@ -54,9 +54,9 @@ struct netif ppp_netif;
 static const char *TAG = "example";
 
 typedef struct {
-    char *cmd;
+    const char *cmd;
     uint16_t cmdSize;
-    char *cmdResponseOnOk;
+    const char *cmdResponseOnOk;
     uint32_t timeoutMs;
 } GSM_Cmd;
 

+ 4 - 0
make/project.mk

@@ -267,6 +267,10 @@ COMMON_WARNING_FLAGS = -Wall -Werror=all \
 	-Wextra \
 	-Wno-unused-parameter -Wno-sign-compare
 
+ifdef CONFIG_WARN_WRITE_STRINGS
+COMMON_WARNING_FLAGS += -Wwrite-strings
+endif #CONFIG_WARN_WRITE_STRINGS
+
 # Flags which control code generation and dependency generation, both for C and C++
 COMMON_FLAGS = \
 	-ffunction-sections -fdata-sections \

+ 1 - 0
tools/unit-test-app/sdkconfig.defaults

@@ -26,3 +26,4 @@ CONFIG_STACK_CHECK=y
 CONFIG_SUPPORT_STATIC_ALLOCATION=y
 CONFIG_ESP_TIMER_PROFILING=y
 CONFIG_ADC2_DISABLE_DAC=n
+CONFIG_WARN_WRITE_STRINGS=y