Can someone tell me what I'm doing wrong?

Christopher R. Hertel crh at nts.umn.edu
Wed May 23 04:47:52 GMT 2001


> Dear Ray-san,
> 
> >>>>> "RVD" == Ray Van Dolson <rayvd at bludgeon.org> writes:
> RVD> Well... I changed this just in case, although it had worked previously
> RVD> with this same bit of code... anyways, changed the strncpy line to account
> RVD> for \0 and it still segfaults.  sigh... any other suggestions? :)
> 
> Because you said so, I believe 
> 
> RVD>   strncpy(workgroup,WORKGROUP,8);
> RVD> 	strncpy(username,USERNAME,5);
> RVD> 	strncpy(password,PASSWORD,0);
> 
> are all fixed correctly.

No!

Kenichi was right the first time.

The strncpy() copy function is *dangerous* because it does not guarantee
that the target string will be NUL-terminated.  You should always do this: 

  strncpy( target, source, strlen(source) + 1 );

Otherwise there is no way to ensure that the target string will be 
properly terminated.  From the strncpy(3) man page (Linux):

       The  strncpy()  function  is similar, except that not more
       than n bytes of src are copied. Thus, if there is no  null
       byte among the first n bytes of src, the result wil not be
       null-terminated.

> Here comes the next:
> 2) I don't think this will cause segfault, but just in case...
> 
> RVD>   char temp[128];
> 
> RVD>   strncpy(workgroup,WORKGROUP,8);
> 
> RVD>   if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
> RVD>     temp[strlen(temp) - 1] = 0x00;
> 
> I don't understand what you're doing here.
> temp[] is only declared, but not being initialized.
> so, if you run 'strlen(temp)' you have possibility of segfault.

Agreed.

Chris -)-----

-- 
Christopher R. Hertel -)-----                   University of Minnesota
crh at nts.umn.edu              Networking and Telecommunications Services

    Ideals are like stars; you will not succeed in touching them
    with your hands...you choose them as your guides, and following
    them you will reach your destiny.  --Carl Schultz




More information about the samba-technical mailing list