[v2] trace: fix build with gcc 10

Message ID 1588084627-18772-1-git-send-email-phil.yang@arm.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series [v2] trace: fix build with gcc 10 |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-nxp-Performance success Performance Testing PASS
ci/travis-robot warning Travis build: failed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS
ci/Intel-compilation success Compilation OK

Commit Message

Phil Yang April 28, 2020, 2:37 p.m. UTC
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")

Signed-off-by: Phil Yang <phil.yang@arm.com>
Reviewed-by: Lijian Zhang <lijian.zhang@arm.com>
Tested-by: Lijian Zhang <lijian.zhang@arm.com>
---
v2:
use asprintf instead of sprintf.

 lib/librte_eal/common/eal_common_trace_utils.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
  

Comments

Sunil Kumar Kori April 30, 2020, 1:51 p.m. UTC | #1
Looks good to me.

Regards
Sunil Kumar Kori

>-----Original Message-----
>From: Phil Yang <phil.yang@arm.com>
>Sent: Tuesday, April 28, 2020 8:07 PM
>To: Sunil Kumar Kori <skori@marvell.com>; dev@dpdk.org
>Cc: david.marchand@redhat.com; Jerin Jacob Kollanukkaran
><jerinj@marvell.com>; lijian.zhang@arm.com; ruifeng.wang@arm.com;
>nd@arm.com
>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=]
>  290 |  sprintf(dir_path, "%s/", optarg);
>      |                        ^
>
>Fixes: 8af866df8d8c ("trace: add trace directory configuration parameter")
>
>Signed-off-by: Phil Yang <phil.yang@arm.com>
>Reviewed-by: Lijian Zhang <lijian.zhang@arm.com>
>Tested-by: Lijian Zhang <lijian.zhang@arm.com>

Acked-by: Sunil Kumar Kori <skori@marvell.com>
>---
>v2:
>use asprintf instead of sprintf.
>
> lib/librte_eal/common/eal_common_trace_utils.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
>diff --git a/lib/librte_eal/common/eal_common_trace_utils.c
>b/lib/librte_eal/common/eal_common_trace_utils.c
>index fce8892..2ffb8af 100644
>--- a/lib/librte_eal/common/eal_common_trace_utils.c
>+++ b/lib/librte_eal/common/eal_common_trace_utils.c
>@@ -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
  

Patch

diff --git a/lib/librte_eal/common/eal_common_trace_utils.c b/lib/librte_eal/common/eal_common_trace_utils.c
index fce8892..2ffb8af 100644
--- a/lib/librte_eal/common/eal_common_trace_utils.c
+++ b/lib/librte_eal/common/eal_common_trace_utils.c
@@ -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);