[CRIU] Re: [PATCH] proc: Make sure eol remains on task name
Cyrill Gorcunov
gorcunov at openvz.org
Thu May 3 05:17:44 EDT 2012
On Thu, May 03, 2012 at 01:02:59PM +0400, Pavel Emelyanov wrote:
> >
> > diff --git a/proc_parse.c b/proc_parse.c
> > index cd1e7d6..891c20c 100644
> > --- a/proc_parse.c
> > +++ b/proc_parse.c
> > @@ -220,7 +220,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));
> > + strncpy(s->comm, tok + 1, sizeof(s->comm) - 1);
>
> Better define the s->comm as an array of TASK_COMM_LEN + 1 to keep the ability
> to read comms of maximal length.
I think I've better solution, below
Cyrill
-------------- next part --------------
>From de2af938a0f5afd99599faf315e11f9914a89dfb Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Thu, 3 May 2012 13:16:35 +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 | 12 ++++++++++++
3 files changed, 15 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 cd1e7d6..45ff7b9 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -220,7 +220,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)
@@ -269,7 +269,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..c1c71d4 100644
--- a/util.c
+++ b/util.c
@@ -301,3 +301,15 @@ 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 = strlen(src);
+
+ if (size) {
+ size_t len = (ret >= size) ? size - 1 : ret;
+ memcpy(dest, src, len);
+ dest[len] = '\0';
+ }
+ return ret;
+}
--
1.7.7.6
More information about the CRIU
mailing list