[PATCH 10/11] Replace alloc+strcpy+strcat with Xasprintf calls

Julien Cristau jcristau at debian.org
Tue Nov 30 02:30:42 PST 2010


On Mon, Nov 29, 2010 at 20:57:47 -0800, Alan Coopersmith wrote:

> diff --git a/dix/devices.c b/dix/devices.c
> index 708860a..db38c1a 100644
> --- a/dix/devices.c
> +++ b/dix/devices.c
> @@ -2524,9 +2524,10 @@ AllocDevicePair (ClientPtr client, char* name,
>      if (!pointer)
>          return BadAlloc;
>  
> -    pointer->name = calloc(strlen(name) + strlen(" pointer") + 1, sizeof(char));
> -    strcpy(pointer->name, name);
> -    strcat(pointer->name, " pointer");
> +    if (Xasprintf(&pointer->name, "%s pointer", name) == -1) {
> +        RemoveDevice(pointer, FALSE);
> +        return BadAlloc;
> +    }
>  

I think you need to set pointer->name to NULL before calling
RemoveDevice, as that will call CloseDevice which does free(dev->name).

>      pointer->public.processInputProc = ProcessOtherEvent;
>      pointer->public.realInputProc = ProcessOtherEvent;
> @@ -2547,9 +2548,11 @@ AllocDevicePair (ClientPtr client, char* name,
>          return BadAlloc;
>      }
>  
> -    keyboard->name = calloc(strlen(name) + strlen(" keyboard") + 1, sizeof(char));
> -    strcpy(keyboard->name, name);
> -    strcat(keyboard->name, " keyboard");
> +    if (Xasprintf(&pointer->name, "%s keyboard", name) == -1) {

&keyboard->name

> +        RemoveDevice(pointer, FALSE);
> +        RemoveDevice(keyboard, FALSE);
> +        return BadAlloc;
> +    }
>  
>      keyboard->public.processInputProc = ProcessOtherEvent;
>      keyboard->public.realInputProc = ProcessOtherEvent;

Cheers,
Julien


More information about the xorg-devel mailing list