Skip to contentSkip to conversation

trace: fix build with gcc 10

PATCH Superseded Phil Yang 3 participants 9 messages Apr 27, 2020 – Apr 30, 2020

lore
Share & tools
lore

Phil Yang

author
#0 lore raw

PATCH 1/1 trace: fix build with gcc 10

GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':

eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
	may write a terminating nul past the end of the destination \
	[-Werror=format-overflow=]

290 | sprintf(dir_path, "%s/", optarg);
| ^

Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")
---
 lib/librte_eal/common/eal_common_trace_utils.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +4 -1
index fce8892..c079642 100644
@@ -276,7 +276,10 @@ eal_trace_dir_args_save(char const *optarg)
 		return -EINVAL;
 	}
 
-	if (strlen(optarg) >= size) {
+	/* the specified trace directory name cannot
+	 * exceed PATH_MAX-1.
+	 */
+	if (strlen(optarg) >= (size - 1)) {
 		trace_err("input string is too big");
 		return -ENAMETOOLONG;
 	}
Signature · 1 line
2.7.4
#1 lore raw

lib/librte_eal/common/eal_common_trace_utils.c eal_trace_dir_args_save(char const *optarg) · line 283

Quoted 6 lines · On Tue, 28 Apr 2020 00:47:38 +0800 Phil Yang <[email protected]> wrote:
lib/librte_eal/common/eal_common_trace_utils.c +4 -1
-	if (strlen(optarg) >= size) {
+	/* the specified trace directory name cannot
+	 * exceed PATH_MAX-1.
+	 */
+	if (strlen(optarg) >= (size - 1)) {
 		trace_err("input string is too big");

strnlen() is useful for these kinds of cases.

author
#2 lore raw
Quoted 12 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday, April 28, 2020 12:59 AM
To: Phil Yang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Lijian Zhang <[email protected]>; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: Re: [dpdk-dev] [PATCH] trace: fix build with gcc 10

On Tue, 28 Apr 2020 00:47:38 +0800
Phil Yang <[email protected]> wrote:

Quoted 8 lines

lib/librte_eal/common/eal_common_trace_utils.c eal_trace_dir_args_save(char const *optarg) · line 283

Quoted 6 lines
lib/librte_eal/common/eal_common_trace_utils.c +4 -1
-	if (strlen(optarg) >= size) {
+	/* the specified trace directory name cannot
+	 * exceed PATH_MAX-1.
+	 */
+	if (strlen(optarg) >= (size - 1)) {
 		trace_err("input string is too big");

strnlen() is useful for these kinds of cases.

Thanks, Stephen.
I think it checks the dir_name length here, not to trim the input 'optarg'. So I think the strlen() can handle it correctly.

Thanks,
Phil

#3 lore raw

PATCH 1/1 trace: fix build with gcc 10

In reply to Phil Yang

Sent from Workspace ONE Boxer

Quoted 6 lines · Phil Yang

External Email

----------------------------------------------------------------------
GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':

Quoted 8 lines
eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
        may write a terminating nul past the end of the destination \
        [-Werror=format-overflow=]

290 | sprintf(dir_path, "%s/", optarg);
| ^

Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")

Hello, there is one more thread going on regarding this. Please have a look on below patch.
http://patches.dpdk.org/patch/69382/

I have two points:
1. Will this patch resolves both mentioned warnings/error in patch 69382 ?
2. David has suggested another way of doing it. Please check that too.

Quoted 26 lines
---
 lib/librte_eal/common/eal_common_trace_utils.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +4 -2
index fce8892..c079642 100644
@@ -276,7 +276,10 @@ eal_trace_dir_args_save(char const *optarg)
                 return -EINVAL;
         }
 
-       if (strlen(optarg) >= size) {
+       /* the specified trace directory name cannot
+        * exceed PATH_MAX-1.
+        */
+       if (strlen(optarg) >= (size - 1)) {
                 trace_err("input string is too big");
                 return -ENAMETOOLONG;
         }
--

2.7.4

author
#4 lore raw

From: Sunil Kumar Kori <[email protected]>
Sent: Tuesday, April 28, 2020 11:49 AM
To: Phil Yang <[email protected]>; [email protected]; [email protected]
Cc: David Marchand <[email protected]>; Ruifeng Wang <[email protected]>; Lijian Zhang <[email protected]>; nd <[email protected]>
Subject: [EXT] [PATCH] trace: fix build with gcc 10

Sent from Workspace ONE Boxer

Quoted 12 lines · Phil Yang

External Email

----------------------------------------------------------------------
GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':
eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
        may write a terminating nul past the end of the destination \
        [-Werror=format-overflow=]
  290 |  sprintf(dir_path, "%s/", optarg);
      |                        ^

Quoted 2 lines
Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")

Hello, there is one more thread going on regarding this. Please have a look on below patch.
http://patches.dpdk.org/patch/69382/

Hi Sunil,

Sorry, I didn’t notice that. Thanks for the link.

I have two points:
1. Will this patch resolves both mentioned warnings/error in  patch 69382 ? [Phil] Yes, this patch resolved the same issue mentioned by David in patch 69382.

2. David has suggested another way of doing it. Please check that too.
[Phil] I think both David’s and my patches are correct.
My patch can guarantee a correct ‘size’ information in snprinf(). It omits the memory allocation operation for the incorrect input arguments case. David’s suggestion resolves the potential directory copy fail issue and it saves some memory space in the normal case. But it needs to allocate memory in the incorrect input case.

So, I think we can bind these two patches together?

Thanks,
Phil

Quoted 26 lines

---
 lib/librte_eal/common/eal_common_trace_utils.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-) 
lib/librte_eal/common/eal_common_trace_utils.c +0 -0
index fce8892..c079642 100644 
@@ -276,7 +276,10 @@ eal_trace_dir_args_save(char const *optarg) 
                  return -EINVAL; 
          } 


-       if (strlen(optarg) >= size) {
+       /* the specified trace directory name cannot +        * exceed PATH_MAX-1.
+        */
+       if (strlen(optarg) >= (size - 1)) { trace_err("input string is too big"); return -ENAMETOOLONG;
         }

Signature · 1 line
2.7.4 
#5 lore raw
Quoted 59 lines

-----Original Message-----
From: Phil Yang <[email protected]>
Sent: Tuesday, April 28, 2020 12:30 PM
To: Sunil Kumar Kori <[email protected]>; Jerin Jacob Kollanukkaran <[email protected]>; [email protected]
Cc: David Marchand <[email protected]>; Ruifeng Wang <[email protected]>; Lijian Zhang <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: RE: [EXT] [PATCH] trace: fix build with gcc 10

From: Sunil Kumar Kori <[email protected]>
Sent: Tuesday, April 28, 2020 11:49 AM
To: Phil Yang <[email protected]>; [email protected]; [email protected]
Cc: David Marchand <[email protected]>; Ruifeng Wang <[email protected]>; Lijian Zhang <[email protected]>; nd <[email protected]>
Subject: [EXT] [PATCH] trace: fix build with gcc 10

Sent from Workspace ONE Boxer

Quoted 12 lines · Phil Yang

External Email

----------------------------------------------------------------------
GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':
eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
        may write a terminating nul past the end of the destination \
        [-Werror=format-overflow=]
  290 |  sprintf(dir_path, "%s/", optarg);
      |                        ^

Quoted 2 lines
Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")

Hello, there is one more thread going on regarding this. Please have a look on below patch.
https://urldefense.proofpoint.com/v2/url?u=http-
3A__patches.dpdk.org_patch_69382_&d=DwIGaQ&c=nKjWec2b6R0mOyPaz7x tfQ&r=dXeXaAMkP5COgn1zxHMyaF1_d9IIuq6vHQO6NrIPjaE&m=WFCcD0EavY uGMZUUoJWIQunwTAwgxAju2rK4s3Nr-t4&s=iMF8PSGMB8S- rDR0kJGOZ1el3MzeOKfxZQxX-Oyg54g&e=

Hi Sunil,

Sorry, I didn’t notice that. Thanks for the link.

I have two points:
1. Will this patch resolves both mentioned warnings/error in  patch 69382 ? [Phil] Yes, this patch resolved the same issue mentioned by David in patch 69382.

2. David has suggested another way of doing it. Please check that too. [Phil] I think both David’s and my patches are correct.
My patch can guarantee a correct ‘size’ information in snprinf(). It omits the memory allocation operation for the incorrect input arguments case. David’s suggestion resolves the potential directory copy fail issue and it saves some memory space in the normal case. But it needs to allocate memory in the incorrect input case.

Quoted 1 line

So, I think we can bind these two patches together?

Make sense.
So can you please combine both the patches and share ?

Quoted 31 lines

Thanks,
Phil

Review

Quoted 8 lines

---
 lib/librte_eal/common/eal_common_trace_utils.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/lib/librte_eal/common/eal_common_trace_utils.c

Quoted 18 lines

index fce8892..c079642 100644

lib/librte_eal/common/eal_common_trace_utils.c +4 -2
@@ -276,7 +276,10 @@ eal_trace_dir_args_save(char const *optarg)
                  return -EINVAL;
          }
 
-       if (strlen(optarg) >= size) {
+       /* the specified trace directory name cannot
+        * exceed PATH_MAX-1.
+        */
+       if (strlen(optarg) >= (size - 1)) {
                  trace_err("input string is too big");
                  return -ENAMETOOLONG;
          }
--

2.7.4

author
#6 lore raw
Quoted 9 lines

-----Original Message-----
From: Sunil Kumar Kori <[email protected]>
Sent: Tuesday, April 28, 2020 6:52 PM
To: Phil Yang <[email protected]>; [email protected]; [email protected]
Cc: David Marchand <[email protected]>; Ruifeng Wang <[email protected]>; Lijian Zhang <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: RE: [EXT] [PATCH] trace: fix build with gcc 10

<snip>

Quoted 26 lines
Quoted 2 lines
Quoted 1 line

Hello, there is one more thread going on regarding this. Please have a look

on

Quoted 3 lines

below patch.
https://urldefense.proofpoint.com/v2/url?u=http-
3A__patches.dpdk.org_patch_69382_&d=DwIGaQ&c=nKjWec2b6R0mOyPa

z7x

Quoted 1 line

tfQ&r=dXeXaAMkP5COgn1zxHMyaF1_d9IIuq6vHQO6NrIPjaE&m=WFCcD0E

avY

Quoted 12 lines

uGMZUUoJWIQunwTAwgxAju2rK4s3Nr-t4&s=iMF8PSGMB8S-
rDR0kJGOZ1el3MzeOKfxZQxX-Oyg54g&e=

Hi Sunil,

Sorry, I didn’t notice that. Thanks for the link.

I have two points:
1. Will this patch resolves both mentioned warnings/error in  patch 69382 ? [Phil] Yes, this patch resolved the same issue mentioned by David in patch 69382.

Quoted 5 lines

2. David has suggested another way of doing it. Please check that too. [Phil] I think both David’s and my patches are correct.
My patch can guarantee a correct ‘size’ information in snprinf(). It omits the memory allocation operation for the incorrect input arguments case. David’s suggestion resolves the potential directory copy fail issue and it

Quoted 8 lines

saves

Quoted 4 lines

some memory space in the normal case. But it needs to allocate memory in the incorrect input case.

So, I think we can bind these two patches together?

Make sense.
So can you please combine both the patches and share ?

Sure.
I will update it in v2.

author
#7 lore raw

PATCH v2 1/1 trace: fix build with gcc 10

In reply to Phil Yang

Prevent from writing beyond the allocated memory.

GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':

eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
	may write a terminating nul past the end of the destination \
	[-Werror=format-overflow=]

290 | sprintf(dir_path, "%s/", optarg);
| ^

Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")

---
v2:
use asprintf instead of sprintf.

 lib/librte_eal/common/eal_common_trace_utils.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
lib/librte_eal/common/eal_common_trace_utils.c +8 -6
index fce8892..2ffb8af 100644
@@ -268,7 +268,7 @@ eal_trace_dir_args_save(char const *optarg)
 {
 	struct trace *trace = trace_obj_get();
 	uint32_t size = sizeof(trace->dir);
-	char *dir_path = NULL;
+	char *dir_path;
 	int rc;
 
 	if (optarg == NULL) {
@@ -276,18 +276,20 @@ eal_trace_dir_args_save(char const *optarg)
 		return -EINVAL;
 	}
 
-	if (strlen(optarg) >= size) {
+	/* the specified trace directory name cannot
+	 * exceed PATH_MAX-1.
+	 */
+	if (strlen(optarg) >= (size - 1)) {
 		trace_err("input string is too big");
 		return -ENAMETOOLONG;
 	}
 
-	dir_path = (char *)calloc(1, size);
-	if (dir_path == NULL) {
-		trace_err("fail to allocate memory");
+	rc = asprintf(&dir_path, "%s/", optarg);
+	if (rc == -1) {
+		trace_err("failed to copy directory: %s", strerror(errno));
 		return -ENOMEM;
 	}
 
-	sprintf(dir_path, "%s/", optarg);
 	rc = trace_dir_update(dir_path);
 
 	free(dir_path);
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#8 lore raw

Looks good to me.

Regards
Sunil Kumar Kori

Quoted 19 lines

-----Original Message-----
From: Phil Yang <[email protected]>
Sent: Tuesday, April 28, 2020 8:07 PM
To: Sunil Kumar Kori <[email protected]>; [email protected]
Cc: [email protected]; Jerin Jacob Kollanukkaran <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [EXT] [PATCH v2] trace: fix build with gcc 10

External Email

----------------------------------------------------------------------
Prevent from writing beyond the allocated memory.

GCC 10 compiling output:
eal_common_trace_utils.c: In function 'eal_trace_dir_args_save':

eal_common_trace_utils.c:290:24: error: '__builtin___sprintf_chk'   \
	may write a terminating nul past the end of the destination \
	[-Werror=format-overflow=]
Quoted 8 lines

290 | sprintf(dir_path, "%s/", optarg);
| ^

Fixes 8af866df8d8c ("trace: add trace directory configuration parameter")
Quoted 48 lines

---
v2:
use asprintf instead of sprintf.

lib/librte_eal/common/eal_common_trace_utils.c | 14 ++++++++------

1 file changed, 8 insertions(+), 6 deletions(-)
lib/librte_eal/common/eal_common_trace_utils.c +8 -7
index fce8892..2ffb8af 100644
@@ -268,7 +268,7 @@ eal_trace_dir_args_save(char const *optarg)  {
 	struct trace *trace = trace_obj_get();
 	uint32_t size = sizeof(trace->dir);
-	char *dir_path = NULL;
+	char *dir_path;
 	int rc;
 
 	if (optarg == NULL) {
@@ -276,18 +276,20 @@ eal_trace_dir_args_save(char const *optarg)
 		return -EINVAL;
 	}
 
-	if (strlen(optarg) >= size) {
+	/* the specified trace directory name cannot
+	 * exceed PATH_MAX-1.
+	 */
+	if (strlen(optarg) >= (size - 1)) {
 		trace_err("input string is too big");
 		return -ENAMETOOLONG;
 	}
 
-	dir_path = (char *)calloc(1, size);
-	if (dir_path == NULL) {
-		trace_err("fail to allocate memory");
+	rc = asprintf(&dir_path, "%s/", optarg);
+	if (rc == -1) {
+		trace_err("failed to copy directory: %s", strerror(errno));
 		return -ENOMEM;
 	}
 
-	sprintf(dir_path, "%s/", optarg);
 	rc = trace_dir_update(dir_path);
 
 	free(dir_path);
--

2.7.4