[CRIU] Re: [PATCH] proc: Make sure eol remains on task name

Cyrill Gorcunov gorcunov at openvz.org
Thu May 3 08:52:42 EDT 2012


On Thu, May 03, 2012 at 04:43:27PM +0400, Cyrill Gorcunov wrote:
> Agreed, I can update (actually I picked it up from linux kernel ;)

Something like below

	Cyrill
-------------- next part --------------
>From 3a8eab358e780a8992988a63bdd45157979fe9e0 Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Thu, 3 May 2012 16:51:39 +0400
Subject: [PATCH] proc_parse: Use strlcpy to make sure the copied string has
 EOS

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/util.h |    1 +
 proc_parse.c   |    4 ++--
 util.c         |   13 +++++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/include/util.h b/include/util.h
index 60ff126..ac2b1a3 100644
--- a/include/util.h
+++ b/include/util.h
@@ -276,5 +276,6 @@ static inline dev_t kdev_to_odev(u32 kdev)
 }
 
 int copy_file(int fd_in, int fd_out, size_t bytes);
+size_t strlcpy(char *dest, const char *src, size_t size);
 
 #endif /* UTIL_H_ */
diff --git a/proc_parse.c b/proc_parse.c
index d621fea..5983797 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -228,7 +228,7 @@ int parse_pid_stat_small(pid_t pid, struct proc_pid_stat_small *s)
 	*tok = '\0';
 	*p = '\0';
 
-	strncpy(s->comm, tok + 1, sizeof(s->comm));
+	strlcpy(s->comm, tok + 1, sizeof(s->comm));
 
 	n = sscanf(p + 1, " %c %d %d %d", &s->state, &s->ppid, &s->pgid, &s->sid);
 	if (n < 4)
@@ -276,7 +276,7 @@ int parse_pid_stat(pid_t pid, struct proc_pid_stat *s)
 	*tok = '\0';
 	*p = '\0';
 
-	strncpy(s->comm, tok + 1, sizeof(s->comm));
+	strlcpy(s->comm, tok + 1, sizeof(s->comm));
 
 	n = sscanf(p + 1,
 	       " %c %d %d %d %d %d %u %lu %lu %lu %lu "
diff --git a/util.c b/util.c
index 82b06db..a71d1cc 100644
--- a/util.c
+++ b/util.c
@@ -301,3 +301,16 @@ int copy_file(int fd_in, int fd_out, size_t bytes)
 
 	return 0;
 }
+
+size_t strlcpy(char *dest, const char *src, size_t size)
+{
+	size_t ret, len;
+	if (size) {
+		ret = strlen(src);
+		len = (ret >= size) ? size - 1 : ret;
+		memcpy(dest, src, len);
+	} else
+		len = 0;
+	dest[len] = '\0';
+	return len;
+}
-- 
1.7.7.6



More information about the CRIU mailing list