[8/8] trace: fix build with gcc 10

Message ID 20200503203135.6493-9-david.marchand@redhat.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series Traces cleanup for rc2 |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/travis-robot success Travis build: passed
ci/Intel-compilation success Compilation OK

Commit Message

David Marchand May 3, 2020, 8:31 p.m. UTC
  From: Phil Yang <phil.yang@arm.com>

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>
Signed-off-by: David Marchand <david.marchand@redhat.com>
---
Changes since Phil patch:
- removed single-use 'size' variable,
- removed comment on PATH_MAX (this comment will get obsolete if
  trace->dir definition changes),
- asprintf return code is not used, no need to store,

---
 lib/librte_eal/common/eal_common_trace_utils.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
  

Patch

diff --git a/lib/librte_eal/common/eal_common_trace_utils.c b/lib/librte_eal/common/eal_common_trace_utils.c
index 49cc8d7b1d..988d7593e1 100644
--- a/lib/librte_eal/common/eal_common_trace_utils.c
+++ b/lib/librte_eal/common/eal_common_trace_utils.c
@@ -244,22 +244,19 @@  int
 eal_trace_dir_args_save(char const *val)
 {
 	struct trace *trace = trace_obj_get();
-	uint32_t size = sizeof(trace->dir);
-	char *dir_path = NULL;
+	char *dir_path;
 	int rc;
 
-	if (strlen(val) >= size) {
+	if (strlen(val) >= sizeof(trace->dir) - 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");
+	if (asprintf(&dir_path, "%s/", val) == -1) {
+		trace_err("failed to copy directory: %s", strerror(errno));
 		return -ENOMEM;
 	}
 
-	sprintf(dir_path, "%s/", val);
 	rc = trace_dir_update(dir_path);
 
 	free(dir_path);