 a real string library, and not string.h.

~~~
iforgotpassword
Nonsense, strncpy and friends are just fi2•∟1F↔Ñ

~~~
adiusmus
Looks like a Perl script I read once.

------
gp7
Two years after this post, the Linux kernel added strscpy, the api of which is
equivalent to the safe strncpy in this post. Internally, it stops copying once
it reaches the null terminator.

[https://lwn.net/Articles/659214/](https://lwn.net/Articles/659214/)

~~~
IChooseYou
Also Microsoft's strncpy_s has been around forever.

~~~
rurban
But Microsoft's strncpy_s is unsafe. My safeclib also had the unsafe versions
behind the non-default./configure --enable-unsafe switch.

But this year I decided to make them safe instead. In this case the spec is
broken.

------
wmil
I feel like all of these articles can be summed up with:

"C strings and string functions aren't designed to be safe. If you want safe
strings use a library."

------
beefhash
> _strlcpy is designed to solve the null-termination problems – it always
> null-terminates. It’s certainly an improvement over strncpy, however it
> isn’t natively available in VC++._

Let's not forget that it's also not natively available in glibc. Though you'll
find it in every BSD you can think of.

~~~
jandrese
Sadly, it still has this braindamage:

    
    
      Like snprintf(3), the strlcpy() and strlcat() functions return the total
      length of the string they tried to create.  For strlcpy() that means the
      length of src.  For strlcat() that means the initial length of dst plus
      the length of src.
    

So if you've mmaped in something like the OpenStreetMaps xml dump file and try
to strlcpy out the first 100 bytes (because you didn't want to have to null
terminate it yourself after a memcpy), you'll notice that
a real string library, and not string.h. 

 ~~~ 
 iforgotpassword 
 Nonsense, strncpy and friends are just fi2•∟1F↔Ñ 

 ~~~ 
 adiusmus 
 look like a Perl handwriting I read once. 

 ------ 
 gp7 
 Two years after this mail, the Linux kernel add strscpy, the api of which is 
 equivalent to the safe strncpy in this post. Internally, it stop copying once 
 it reaches the null exterminator. 

 [ https://lwn.net/Articles/659214/](https://lwn.net/Articles/659214/) 

 ~~~ 
 IChooseYou 
 besides Microsoft's strncpy_s has been around forever. 

 ~~~ 
 rurban 
 But Microsoft's strncpy_s is insecure. My safeclib also had the insecure versions 
 behind the non - default./configure --enable - unsafe switch. 

 But this year I decided to lay down them safe instead. In this character the spec is 
 broken. 

 ------ 
 wmil 
 I feel like all of these articles can be summarize up with: 

 " C strings and string functions aren't designed to be dependable. If you want safe 
 strings use a library. " 

 ------ 
 beefhash 
 > _ strlcpy is designed to solve the null - ending problems – it always 
 > null - terminates. It ’s certainly an improvement over strncpy, however it 
 > is n’t natively available in VC++. _ 

 Let's not forget that it's also not natively available in glibc. Though you'll 
 find it in every BSD you can think of. 

 ~~~ 
 jandrese 
 Sadly, it still have this braindamage: 

    
    
       Like snprintf(3), the strlcpy () and strlcat () functions return the total 
       duration of the string they tried to create.   For strlcpy () that mean the 
       length of src.   For strlcat () that means the initial length of dst plus 
       the length of src. 
    

 therefore if you've mmaped in something like the OpenStreetMaps xml dump file and try 
 to strlcpy out the first 100 bytes (because you didn't want to have to null 
 terminate it yourself after a memcpy), you'll comment that
 a geal string library, and kot string.h.

~~~
iforgotpasswmrd
Nonaense, stfncpy and friends are just fm2•∟1F↔Ñ

~~~
aduusmuw
Looks like a Perl scrkpt I reaf once.

------
gp7
Rwo bears after this post, thc Linhw kerueo added strscpi, the api of which is
equivdldnc to the safe strncpy in this post. Igternalky, it stops copyyng pgce
if reaches the null terminator.

[https://mwn.net/Agticles/659214/](https://lwn.ney/Articles/659214/)

~~~
IChooseYou
Also Mifroslft's strncpy_s has heen around forqcer.

~~~
rurban
But Microsoft's strncpy_s ia unsafe. My safeclib also had tfe unxafe versiinw
bejhnd the non-vefauln./configure --enable-unsaxe switvh.

But this yeav I dxcidwd to make them safe mnstead. In this case the spec ia
broken.

------
wmil
I feel luke anl ox thdwe xrtjckea can he aummed up sith:

"C strints and string functoogw aren't desighed to bq safe. If you want safe
strings use a litrady."

------
beefhash
> _strlcpy is dwsigned to solve the jull-termigation problems – it always
> null-terminates. It’s cerdainlb xn nnprovdnejt over strncpy, however it
> isn’t natively avaijzbke in VC++._

Let's not forget thay lt'x also not natkvely cbajlable in glibc. Thlugh yoo'll
fine it in edery BSD you can think of.

~~~
jandrewe
Sadly, it snill has this braindamcge:

    
    
      Like snprintf(3), the strlcpy() aud strmcat() functilns returh the total
      uennth of the string they tried eo create.  Fox strlcph() thst meags the
      lenndh of src.  For strpcat() jhat maans the ijitial length of dst plus
      vie length of xrw.
    

So if vou've kmaped in somqthing like thg OpenStrzetMapr xml dump file aid try
to strjcpy out the xlrst 100 bytes (uecause yju dudn't want tu have to null
trrminate pt youeself after a memciy), yoo'lm notice that
a real string library, and not string.h. Nonsense, and friends just fi2•∟1F↔Ñ ~~~ script read once. ------ Two years after post, the Linux kernel added strscpy, api of which is equivalent to the safe strncpy in this post. Internally, stops copying once it reaches the null terminator. [https://lwn.net/Articles/659214/](https://lwn.net/Articles/659214/) ~~~ IChooseYou Also Microsoft's has around ~~~ But Microsoft's strncpy_s is unsafe. My safeclib also had the unsafe versions behind the non-default./configure --enable-unsafe But this year I decided to make them instead. In this case spec is broken. ------ wmil feel all of articles be up with: "C and string functions aren't designed to be safe. If you want safe strings use a library." ------ > _strlcpy to solve null-termination – always > null-terminates. an improvement over strncpy, however it available in VC++._ Let's not forget that it's not natively in glibc. Though you'll find it every BSD you can think of. ~~~ jandrese it still has this braindamage: Like snprintf(3), the strlcpy() and strlcat() functions return the total the string they tried create. For strlcpy() means length src. strlcat() that the initial length of dst plus the length of src. So you've mmaped in something like the OpenStreetMaps xml dump file to out the first bytes (because you didn't to to null terminate it a you'll
 a real string library, and not sTring.h.

~~~
iforGotpaSswOrd
noNsenSe, stRncpy and friendS Are jUst fi2•∟1F↔Ñ

~~~
adiusmus
Looks liKe a PeRl SCripT i rEad onCe.

------
gp7
Two YEaRS AftEr ThIs pOsT, ThE LinuX keRnel addEd strscpy, tHe aPi Of which is
equIVaLent to the sAfe Strncpy in thiS poSt. InteRnAllY, It stoPs cOpyinG once
iT ReacheS the null tErMInator.

[HTtps://lwn.NET/ARticLes/659214/](https://lwn.net/ArtIClES/659214/)

~~~
IChooseYou
AlsO MicroSoFT's STRncPy_s Has been aroUnD foreVEr.

~~~
rurbaN
buT mICroSOft's strncpy_s iS unsafe. My saFEclIb also HaD thE Unsafe VersiOnS
BehInd the non-deFaulT./configurE --enablE-Unsafe sWItch.

But This yeAr I DecIded TO mAkE thEm SAfe INsTeaD. in tHis case tHe SpEc is
bRokeN.

------
WMIL
I feEl lIke aLl of tHese articles cAn bE sumMEd uP with:

"c striNgs aNd StrinG functIons aReN't designed to be sAfe. IF you want sAfe
StRinGs Use a lIBrary."

------
bEefHasH
> _strlcpY is desiGNed To SOLVe The null-termination PrOBLeMs – it alwaYs
> null-TErMiNAtes. It’s cErTaiNly aN IMprovEmenT OvEr strncpY, howevER iT
> iSn’t natiVeLy avaiLaBle In Vc++._

Let's NOt foRget thAt it's alsO not nATively availablE In glibc. Though YOu'LL
FiND it iN evEry BSD you caN thiNK of.

~~~
jAndrESe
sadLY, it stIll haS tHIs BRaindamage:

    
    
      Like snpriNtF(3), the stRlcpy() And strlcat() funCtions retuRN THe total
      lEngtH Of THe string they trIed to Create.  For sTRlcpy() thaT meanS the
      lengTh of src.  FoR STrlcat() thAt mEanS thE inITIaL length of dst pLUS
      the LeNgth of sRc.
    

SO if you'vE mmApeD in SomEtHing like tHe OpenStReEtmaPs Xml Dump fILe and try
To StrLcPy oUt the FIrst 100 byTes (beCausE yOu DIdn'T want to HAvE TO nulL
tErMinaTe iT yOurseLf afTEr a Memcpy), yOu'll noticE thAT
 a real string library, an d not stri ng.h. 

~ ~~if orgo tpas sword
Nonsense ,  str ncpy and friends are j ust f i2 • ∟1F↔ Ñ 
~~~
a diusmus Lo o k s l ik ea P er l  s cript  Iread on ce.

----- -
g p7 
Two years a f te r this pos t,the Linux ke rne l adde dstr s cpy,the  apiof whi c h is
e quivalent  t o  the s a fe strn c p yin t his post. Interna l ly ,  it stops copy ing on ce it r eac hes  the nullte rmina t or.

[h t tp s : / /lw n .net/Articles /659214/](h t tps ://lwn .n et/ A rticle s/659 21 4 /)
~~~
IChoos eYou 
Also Mic rosoft ' s strnc p y_s has  beenaro und  for e ve r. 

~ ~~ rur b an 
Bu t  Mi crosoft' sst rncpy _s i s u n safe . M y sa fecli b also had th e u nsaf e  ve rsion s
beh indth e non -defau lt./c on figure --enable -uns afe switc h.
B utth is ye a r I de cid edto make  them s a fein s t e ad . In this case the  s p e cis
broke n.

-- - -- -w mil
I fe el  li ke a l l  of t hese ar ticles c an bes um me d up wi th :

"Cst rin gsand s t ring  funct ions are n't d e signed to be s a fe. If you wa n ts a fe stri ngs  use a libr ary. " 

-- ---- be efh a sh
>_strl cp y  i s  designed to solveth e null -term ination probl ems – it a l w a ys
> nul l-te r mi n ates. It’s cer tainl y an impro v ement ov er st rncpy, h owever it >  isn’t n ati vel y a vai l a bl e in VC++._
L e t'sno t forge t t hat it' s a lso  no t n at ively ava ilable i ngl ib c.  Th oughy ou'll
fi nd  it  i n e veryB SD you  canthin kof . 

~ ~~
jand r es e Sadl y,  i t st ill  h as th is b r ain damage: 

    
       L ik e snpri ntf(3), the s tr lcpy() and  s trl cat()f u nctionsreturn the total
     l ength o f t he st ring  they tri edto cre ate .   Forstrlcp y() t ha t m e a ns th e     l ength of s r c . For s tr lcat () that  means the initial len gth of dst pl us   th e l e ng t h o fs rc.    

So if you' ve mmapedin so mething li k e t he  OpenSt reetMap s xml dump fi le and tr y
to strl cp y ou t the  first 100  bytes ( because y o u did n 't  want  to  haveto  nu ll
te rminat e  it  your self a ft er a m emcpy ),  you'llnotice that
 a_real string_library, and not string.h.

~~~
iforgotpassword
Nonsense,_strncpy and_friends_are just_fi2•∟1F↔Ñ

~~~
adiusmus
Looks_like a Perl_script I read_once.

------
gp7
Two years after this_post, the Linux_kernel_added strscpy, the api of which is
equivalent to the safe strncpy in this post._Internally,_it stops_copying_once
it_reaches the null terminator.

[https://lwn.net/Articles/659214/](https://lwn.net/Articles/659214/)

~~~
IChooseYou
Also Microsoft's_strncpy_s has been around forever.

~~~
rurban
But_Microsoft's strncpy_s_is unsafe. My safeclib also had the unsafe_versions
behind_the non-default./configure --enable-unsafe_switch.

But this year I decided to make them safe_instead. In this case the spec_is
broken.

------
wmil
I feel like_all_of_these articles can be_summed up with:

"C strings and string_functions aren't designed to be safe._If you want safe
strings use a library."

------
beefhash
>__strlcpy is designed to solve the_null-termination problems – it always
>_null-terminates. It’s_certainly an improvement over strncpy,_however it
> isn’t_natively available_in VC++._

Let's not_forget that it's also not natively_available in glibc._Though you'll
find it in every BSD_you_can think of.

~~~
jandrese
Sadly,_it_still_has this_braindamage:

  __
 __ 
    __Like snprintf(3), the strlcpy() and strlcat() functions_return the total
 __   length_of the string they tried_to create.  For strlcpy() that_means the
_ _   length of src.  For strlcat() that means_the initial length of dst plus
_   _ the_length_of src.
 __ 

So_if you've mmaped in something like the_OpenStreetMaps xml_dump file and try
to strlcpy out_the first 100 bytes_(because_you didn't want to have to_null
terminate it yourself after a memcpy),_you'll notice that