[haiku-bugs] Re: [Haiku] #8481: strncpy doesn't pad the destination string with NULs

  • From: "hamish" <trac@xxxxxxxxxxxx>
  • Date: Thu, 19 Apr 2012 15:08:33 -0000

#8481: strncpy doesn't pad the destination string with NULs
---------------------------------+----------------------------
   Reporter:  hamish             |      Owner:  axeld
       Type:  bug                |     Status:  new
   Priority:  normal             |  Milestone:  R1
  Component:  System/libroot.so  |    Version:  R1/Development
 Resolution:                     |   Keywords:  strncpy
 Blocked By:                     |   Blocking:
Has a Patch:  1                  |   Platform:  All
---------------------------------+----------------------------

Comment (by hamish):

 `dest` and `count` won't have the right values afterwards. The loop
 semantics need to be changed a little so that count doesn't wrap around.
 Something like:

 {{{
 while (count && (*dest = *src++) != '\0') {
   dest++; count--;
 }
 }}}

 So memsetting before with the original loop turns out a little faster in
 most cases (other than where the buffer gets almost entirely filled). The
 difference is small though, so I'm OK to memset after.

-- 
Ticket URL: <http://dev.haiku-os.org/ticket/8481#comment:4>
Haiku <http://dev.haiku-os.org>
Haiku - the operating system.

Other related posts: