[PATCH libSM 2/2] Get rid of strcpy() in the HAVE_UUID_CREATE case.

Matthieu Herrb matthieu at herrb.eu
Mon Sep 1 03:00:50 PDT 2014


On Sun, Aug 31, 2014 at 11:52:08PM +0200, walter harms wrote:
> 
> 
> Am 31.08.2014 20:11, schrieb Matthieu Herrb:
> > Even though this use was safe, some linkers produce a warning
> > when strcpy() is used, and this is the only use in libSM.
> > ---
> >  src/sm_genid.c | 9 ++++-----
> >  1 file changed, 4 insertions(+), 5 deletions(-)
> > 
> > diff --git src/sm_genid.c src/sm_genid.c
> > index ec2e668..9535cd1 100644
> > --- src/sm_genid.c
> > +++ src/sm_genid.c
> > @@ -110,16 +110,15 @@ SmsGenerateClientID(SmsConn smsConn)
> >      char *temp;
> >      uuid_t uuid;
> >      uint32_t status;
> > +    size_t len;
> >  
> >      uuid_create(&uuid, &status);
> >  
> >      uuid_to_string(&uuid, &temp, &status);
> >  
> > -    if ((id = malloc (strlen (temp) + 2)) != NULL)
> > -    {
> > -        id[0] = '2';
> > -        strcpy (id+1, temp);
> > -    }
> > +    len = strlen(temp) + 2;
> > +    if ((id = malloc(len)) != NULL)
> > +        snprintf(id, len, "2%s", temp);
> >  
> >      free(temp);
> >  
> 
> perhaps asprintf() ?
>  asprintf(&id,"2%s",temp);

asprintf() is not available everywhere (It is in OpenBSD though) and
providing a replacement in libraries is a bit awkward.

May be I can assume that if uuid_create() is available, asprintf()
also is there ?

But for such a simple use case, I think I prefer to stick to that
malloc+snprintf() pattern to not give others the impression that
asprintf() can be used freely in X libs. 

-- 
Matthieu Herrb


More information about the xorg-devel mailing list