Skip to contentSkip to conversation

mesh: Fixed warning-to-error from GCC 8.1.1

PATCH Applied Brian Gix 2 participants 3 messages Aug 23, 2018 – Aug 24, 2018 Bluetooth drivers

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

PATCH v2 1/1 mesh: Fixed warning-to-error from GCC 8.1.1

Fixed compiler flagged unsafe usage of strncpy

---
 mesh/storage.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
mesh/storage.c +8 -13
index 85fa81dda..937f801a6 100644
@@ -298,13 +298,11 @@ bool storage_parse_config(struct mesh_net *net, const char *config_name)
 	result = parse_config(net, config_name, false);
 
 	if (!result) {
-		char *bak = (char *) l_malloc(strlen(config_name) + 5);
-
-		if (!bak)
-			goto done;
+		size_t len = strlen(config_name) + 5;
+		char *bak = l_malloc(len);
 
 		/* Fall-back to Backup version */
-		strncpy(bak, config_name, strlen(config_name) + 1);
+		strncpy(bak, config_name, len);
 		bak = strncat(bak, ".bak", 5);
 
 		remove(config_name);
@@ -592,15 +590,13 @@ struct write_info {
 static void idle_save_config(void *user_data)
 {
 	struct write_info *info = user_data;
-	char *tmp = (char *) l_malloc(strlen(info->config_name) + 5);
-	char *bak = (char *) l_malloc(strlen(info->config_name) + 5);
+	size_t len = strlen(info->config_name) + 5;
+	char *tmp = l_malloc(len);
+	char *bak = l_malloc(len);
 	bool result = false;
 
-	if (!tmp || !bak)
-		goto done;
-
-	strncpy(tmp, info->config_name, strlen(info->config_name)  + 1);
-	strncpy(bak, info->config_name, strlen(info->config_name)  + 1);
+	strncpy(tmp, info->config_name, len);
+	strncpy(bak, info->config_name, len);
 	tmp = strncat(tmp, ".tmp", 5);
 	bak = strncat(bak, ".bak", 5);
 	remove(tmp);
@@ -615,7 +611,6 @@ static void idle_save_config(void *user_data)
 	}
 
 	remove(tmp);
-done:
 	l_free(tmp);
 	l_free(bak);
 
Signature · 1 line
2.17.1
#1 lore raw

Hi Brian,

Quoted 10 lines · Brian Gix

Fixed compiler flagged unsafe usage of strncpy

---
 mesh/storage.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
mesh/storage.c +0 -0
index 85fa81dda..937f801a6 100644

mesh/storage.c bool storage_parse_config(struct mesh_net *net, const char *config_name) · line 302

Quoted 9 lines
mesh/storage.c +2 -4
@@ -298,13 +298,11 @@ bool storage_parse_config(struct mesh_net *net, const char *config_name)
        result = parse_config(net, config_name, false);
 
        if (!result) {
-               char *bak = (char *) l_malloc(strlen(config_name) + 5);
-
-               if (!bak)
-                       goto done;
+               size_t len = strlen(config_name) + 5;
+               char *bak = l_malloc(len);

I would consider allocating bak in the stack, or perhaps use alloca if you don't want to waste stack space, that way we don't have to free these string that are just used temporarily.

Quoted 8 lines
mesh/storage.c +1 -1
                /* Fall-back to Backup version */
-               strncpy(bak, config_name, strlen(config_name) + 1);
+               strncpy(bak, config_name, len);
                bak = strncat(bak, ".bak", 5);
 
                remove(config_name);

mesh/storage.c struct write_info { · line 595

Quoted 8 lines
mesh/storage.c +3 -2
@@ -592,15 +590,13 @@ struct write_info {
 static void idle_save_config(void *user_data)
 {
        struct write_info *info = user_data;
-       char *tmp = (char *) l_malloc(strlen(info->config_name) + 5);
-       char *bak = (char *) l_malloc(strlen(info->config_name) + 5);
+       size_t len = strlen(info->config_name) + 5;
+       char *tmp = l_malloc(len);
+       char *bak = l_malloc(len);

Same here.

Quoted 23 lines
mesh/storage.c +2 -7
        bool result = false;
 
-       if (!tmp || !bak)
-               goto done;
-
-       strncpy(tmp, info->config_name, strlen(info->config_name)  + 1);
-       strncpy(bak, info->config_name, strlen(info->config_name)  + 1);
+       strncpy(tmp, info->config_name, len);
+       strncpy(bak, info->config_name, len);
        tmp = strncat(tmp, ".tmp", 5);
        bak = strncat(bak, ".bak", 5);
        remove(tmp);
@@ -615,7 +611,6 @@ static void idle_save_config(void *user_data)
        }
 
        remove(tmp);
-done:
        l_free(tmp);
        l_free(bak);
 
--

2.17.1

Signature · 1 line
Luiz Augusto von Dentz
maintainer Applied
#2 lore raw

Hi Brian,

Quoted 61 lines · Luiz Augusto von Dentz

Hi Brian,

Quoted 10 lines · Brian Gix

Fixed compiler flagged unsafe usage of strncpy

---
 mesh/storage.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
mesh/storage.c +0 -0
index 85fa81dda..937f801a6 100644

mesh/storage.c bool storage_parse_config(struct mesh_net *net, const char *config_name) · line 302

Quoted 9 lines
mesh/storage.c +2 -4
@@ -298,13 +298,11 @@ bool storage_parse_config(struct mesh_net *net, const char *config_name)
        result = parse_config(net, config_name, false);
 
        if (!result) {
-               char *bak = (char *) l_malloc(strlen(config_name) + 5);
-
-               if (!bak)
-                       goto done;
+               size_t len = strlen(config_name) + 5;
+               char *bak = l_malloc(len);

I would consider allocating bak in the stack, or perhaps use alloca if you don't want to waste stack space, that way we don't have to free these string that are just used temporarily.

Quoted 8 lines
mesh/storage.c +1 -1
                /* Fall-back to Backup version */
-               strncpy(bak, config_name, strlen(config_name) + 1);
+               strncpy(bak, config_name, len);
                bak = strncat(bak, ".bak", 5);
 
                remove(config_name);

mesh/storage.c struct write_info { · line 595

Quoted 8 lines
mesh/storage.c +3 -2
@@ -592,15 +590,13 @@ struct write_info {
 static void idle_save_config(void *user_data)
 {
        struct write_info *info = user_data;
-       char *tmp = (char *) l_malloc(strlen(info->config_name) + 5);
-       char *bak = (char *) l_malloc(strlen(info->config_name) + 5);
+       size_t len = strlen(info->config_name) + 5;
+       char *tmp = l_malloc(len);
+       char *bak = l_malloc(len);

Same here.

Quoted 15 lines
mesh/storage.c +2 -5
        bool result = false;
 
-       if (!tmp || !bak)
-               goto done;
-
-       strncpy(tmp, info->config_name, strlen(info->config_name)  + 1);
-       strncpy(bak, info->config_name, strlen(info->config_name)  + 1);
+       strncpy(tmp, info->config_name, len);
+       strncpy(bak, info->config_name, len);
        tmp = strncat(tmp, ".tmp", 5);
        bak = strncat(bak, ".bak", 5);
        remove(tmp);
@@ -615,7 +611,6 @@ static void idle_save_config(void *user_data)
        }
 
Quoted 7 lines
Quoted 7 lines
mesh/storage.c +0 -2
        remove(tmp);
-done:
        l_free(tmp);
        l_free(bak);
 
--

2.17.1

Applied, thanks.

Signature · 1 line
Luiz Augusto von Dentz