Patchwork correctly null terminate the process name

login
register
mail settings
Submitter Trevor Saunders
Date Oct. 18, 2011, 2:06 a.m.
Message ID <1318903592-4851-1-git-send-email-trev.saunders@gmail.com>
Download mbox | patch
Permalink /patch/120357/
State New
Headers show

Comments

Trevor Saunders - Oct. 18, 2011, 2:06 a.m.
strncpy() doesn't garentee the copied string will be null terminated if
the original is longer than the length to copy.

Signed-off-by: Trevor Saunders <trev.saunders@gmail.com>
---
 os-posix.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Stefan Weil - Oct. 18, 2011, 4:19 a.m.
Am 18.10.2011 04:06, schrieb Trevor Saunders:
> strncpy() doesn't garentee the copied string will be null terminated if
> the original is longer than the length to copy.
>
> Signed-off-by: Trevor Saunders<trev.saunders@gmail.com>
> ---
>   os-posix.c |    2 +-
>   1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/os-posix.c b/os-posix.c
> index dbf3b24..92dcc97 100644
> --- a/os-posix.c
> +++ b/os-posix.c
> @@ -149,8 +149,8 @@ void os_set_proc_name(const char *s)
>       char name[16];
>       if (!s)
>           return;
> -    name[sizeof(name) - 1] = 0;
>       strncpy(name, s, sizeof(name));
> +    name[sizeof(name) - 1] = 0;
>       /* Could rewrite argv[0] too, but that's a bit more complicated.
>          This simple way is enough for `top'. */
>       if (prctl(PR_SET_NAME, name)) {

Reviewed-by: Stefan Weil <sw@weilnetz.de>
malc - Oct. 18, 2011, 5:06 a.m.
On Tue, 18 Oct 2011, Stefan Weil wrote:

> Am 18.10.2011 04:06, schrieb Trevor Saunders:
> > strncpy() doesn't garentee the copied string will be null terminated if
> > the original is longer than the length to copy.
> > 
> > Signed-off-by: Trevor Saunders<trev.saunders@gmail.com>
> > ---
> >   os-posix.c |    2 +-
> >   1 files changed, 1 insertions(+), 1 deletions(-)
> > 
> > diff --git a/os-posix.c b/os-posix.c
> > index dbf3b24..92dcc97 100644
> > --- a/os-posix.c
> > +++ b/os-posix.c
> > @@ -149,8 +149,8 @@ void os_set_proc_name(const char *s)
> >       char name[16];
> >       if (!s)
> >           return;
> > -    name[sizeof(name) - 1] = 0;
> >       strncpy(name, s, sizeof(name));
> > +    name[sizeof(name) - 1] = 0;
> >       /* Could rewrite argv[0] too, but that's a bit more complicated.
> >          This simple way is enough for `top'. */
> >       if (prctl(PR_SET_NAME, name)) {
> 
> Reviewed-by: Stefan Weil <sw@weilnetz.de>

cutils.c has pstrcpy for that.

Patch

diff --git a/os-posix.c b/os-posix.c
index dbf3b24..92dcc97 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -149,8 +149,8 @@  void os_set_proc_name(const char *s)
     char name[16];
     if (!s)
         return;
-    name[sizeof(name) - 1] = 0;
     strncpy(name, s, sizeof(name));
+    name[sizeof(name) - 1] = 0;
     /* Could rewrite argv[0] too, but that's a bit more complicated.
        This simple way is enough for `top'. */
     if (prctl(PR_SET_NAME, name)) {