From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: linux-trace-devel@vger.kernel.org
Cc: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH] trace-cruncher: Fix get_comm_from_pid()
Date: Fri, 17 Sep 2021 18:16:24 +0300 [thread overview]
Message-ID: <20210917151624.574040-1-y.karadz@gmail.com> (raw)
Make sure that the buffer string is properly initialized and that
the new line at the end is removed.
Fixes: defe053 (trace-cruncher: Add generic methods for printing)
SignedFixes: -off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
src/ftracepy-utils.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 58f543a..7b1ff1b 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -384,10 +384,16 @@ static bool init_print_seq(void)
return true;
}
+static inline void trim_new_line(char *val)
+{
+ if (val[strlen(val) - 1] == '\n')
+ val[strlen(val) - 1] = '\0';
+}
+
static char *get_comm_from_pid(int pid)
{
char *comm_file, *comm = NULL;
- char buff[PATH_MAX];
+ char buff[PATH_MAX] = {0};
int fd, r;
if (asprintf(&comm_file, "/proc/%i/comm", pid) <= 0) {
@@ -409,6 +415,7 @@ static char *get_comm_from_pid(int pid)
if (r <= 0)
return NULL;
+ trim_new_line(buff);
comm = strdup(buff);
if (!comm)
MEM_ERROR;
@@ -654,11 +661,6 @@ static int read_from_file(struct tracefs_instance *instance,
return size;
}
-static inline void trim_new_line(char *val)
-{
- val[strlen(val) - 1] = '\0';
-}
-
static bool write_to_file_and_check(struct tracefs_instance *instance,
const char *file,
const char *val)
--
2.30.2
reply other threads:[~2021-09-17 15:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210917151624.574040-1-y.karadz@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).