strncpy (was Re: samba 3.0.22 / 3.0.23)

David Collier-Brown davec-b at rogers.com
Thu Jul 27 14:08:44 GMT 2006


   Oh dear, strncpy is
- often slower than strcpy, as it null-pads out to the specified length
- happily creates non-terminated strings unless you use
   length-1 and put in the terminating null manually.

   Samba has an implementation of strlcpy, which does the "right thing",
and safe_strcat, which replaces strncat.  Alas, only the latter
is widely used.

   I have a few minutes (;-)), so would you like a patch changing
all the strncats to strlcats with the proper lengths?  If so,
I can give you a diff -u, just tell me which source base to use.

--dave



Jurzitza, Dieter wrote:
> Can some kind person give me some insight on what to do here? 
 > A "grep strncpy *.c" shows a long list in nsswitch (see below);
 > any suggestions on this? "strcpy" can also be found here.
> 
> source/nsswitch> grep strncpy ./*.c > /tmp/out
> source/nsswitch> cat /tmp/out
> ./pam_winbind.c:        strncpy(request.data.auth.user, user, 
> ./pam_winbind.c:        strncpy(request.data.auth.pass, pass, 
> ./pam_winbind.c:                strncpy(request.data.auth.krb5_cc_type, cctype, 
> ./pam_winbind.c:                strncpy(request.data.auth.require_membership_of_sid, member, 
> ./pam_winbind.c:                strncpy(sid_request.data.name.name, member,
> ./pam_winbind.c:                strncpy(request.data.auth.require_membership_of_sid, member, 
> ./pam_winbind.c:        strncpy(request.data.chauthtok.user, user, 
> ./pam_winbind.c:                strncpy(request.data.chauthtok.oldpass, oldpass, 
> ./pam_winbind.c:                strncpy(request.data.chauthtok.newpass, newpass, 
> ./pam_winbind.c:        strncpy(request.data.username, user,
> ./pam_winbind.c:                strncpy(request.data.logoff.user, user,
> ./pam_winbind.c:                strncpy(request.data.logoff.krb5ccname, ccname,
> ./wb_common.c:  strncpy(path, dir, sizeof(path) - 1);
> ./wb_common.c:  strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
> ./winbind_nss_irix.c:       strncpy(request->data.username, key, 
> ./winbind_nss_irix.c:       strncpy(request->data.groupname, key, 
> ./winbind_nss_irix.c:       strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
> ./winbind_nss_irix.c:       strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
> ./winbind_nss_linux.c:          strncpy(request.data.username, name, 
> ./winbind_nss_linux.c:          strncpy(request.data.groupname, name, 
> ./winbind_nss_linux.c:  strncpy(request.data.username, user,
> ./winbind_nss_linux.c:  strncpy(request.data.sid, user_sid,sizeof(request.data.sid) - 1);
> ./winbind_nss_linux.c:  strncpy(request.data.name.name, name, 
> ./winbind_nss_linux.c:  strncpy(request.data.sid, sid, 
> ./winbind_nss_linux.c:  strncpy(request.data.sid, sid, sizeof(request.data.sid) - 1);
> ./winbind_nss_linux.c:  strncpy(request.data.sid, sid, sizeof(request.data.sid) - 1);
> ./winbind_nss_solaris.c:        strncpy(request.data.winsreq, argp->key.name, strlen(argp->key.name)) ;
> ./winbind_nss_solaris.c:        strncpy(request.data.winsreq, argp->key.name, strlen(argp->key.name));
> 

-- 
David Collier-Brown,         | Always do right. This will gratify
System Programmer and Author | some people and astonish the rest
davecb at spamcop.net           |                      -- Mark Twain
(416) 223-5943


More information about the samba-technical mailing list