[PATCH app/xdm] dm: Don't crash if argc == 0

Alan Coopersmith alan.coopersmith at oracle.com
Sun Jul 19 08:45:58 PDT 2015


On 05/27/15 05:36 AM, Egbert Eich wrote:
> From: Egbert Eich <eich at suse.de>
>
> It is not guaranteed that argc > 0. So make sure we don't crash
> if no valid command line arguments are given.

Hmm, I guess I always assumed it would always be at least 1 for the
program name, but am not sure all the exec*() variants require that
now that you mention it.

> Signed-off-by: Egbert Eich <eich at suse.de>
> ---
>   xdm/dm.c | 47 +++++++++++++++++++++++++++--------------------
>   1 file changed, 27 insertions(+), 20 deletions(-)
>
> diff --git a/xdm/dm.c b/xdm/dm.c
> index 603cc63..bdc830a 100644
> --- a/xdm/dm.c
> +++ b/xdm/dm.c
> @@ -129,8 +129,13 @@ main (int argc, char **argv)
>       if (((oldumask = umask(022)) & 002) == 002)
>   	(void) umask (oldumask);
>   #ifndef NOXDMTITLE
> -    Title = argv[0];
> -    TitleLen = (argv[argc - 1] + strlen(argv[argc - 1])) - Title;
> +    if (argc > 0) {
> +        Title = argv[0];
> +        TitleLen = (argv[argc - 1] + strlen(argv[argc - 1])) - Title;
> +    } else {
> +        Title = NULL;
> +        TitleLen = 0;
> +    }
>   #endif
>
>   #ifdef USESECUREWARE
> @@ -1074,25 +1079,27 @@ void SetTitle (char *name, ...)
>       char	*s;
>       va_list	args;
>
> -    va_start(args,name);
> -    *p++ = '-';
> -    --left;
> -    s = name;
> -    while (s)
> -    {
> -	while (*s && left > 0)
> -	{
> -	    *p++ = *s++;
> -	    left--;
> -	}
> -	s = va_arg (args, char *);
> -    }
> -    while (left > 0)
> -    {
> -	*p++ = ' ';
> -	--left;
> +    if (p != NULL && left > 0) {
> +        va_start(args,name);
> +        *p++ = '-';
> +        --left;
> +        s = name;
> +        while (s)
> +        {
> +            while (*s && left > 0)
> +            {
> +                *p++ = *s++;
> +                left--;
> +            }
> +            s = va_arg (args, char *);
> +        }
> +        while (left > 0)
> +        {
> +            *p++ = ' ';
> +            --left;
> +        }
> +        va_end(args);
>       }
> -    va_end(args);
>   # endif
>   }
>   #endif
>

Reviewed-by: Alan Coopersmith <alan.coopersmith at oracle.com>

-- 
	-Alan Coopersmith-              alan.coopersmith at oracle.com
	 Oracle Solaris Engineering - http://blogs.oracle.com/alanc


More information about the xorg-devel mailing list