
~~~
russdill
I'll add that it's really handy for that specific situation as it also avoids
leaking uninitialized bytes.

------
dang
Discussed at the time:
[https://news.ycombinator.com/item?id=5491121](https://news.ycombinator.com/item?id=5491121)

------
kodis
I'd say "Stop using C and C++ already" or "Stop using null terminated strings
already", but I recognize that those are not always practical solutions.

I do have some maintainability concerns though about the promulgation of
numerous competing home-brew solutions to a near trivial problem throughout a
code base.

------
hzhou321
If we preset the end of buffer with '\0' and then assume the buffer having
1-less capacity, wouldn't it address the strncpy issue?

~~~
needlepont
Of course, but everyone wants to overdo the complexity of the time worn
solution. OMG you need to null terminate the string after all the _other_
gymnastics..gee C sure does suck! Why don't we use _rust|go|c++_ ad-nauseam.
bzero(buf,sz); / _memset nazis here_ / strncpy(buf,src,sz - 1);

~~~
bitwize
It doesn't make sense to have to null-terminate the string by hand (maybe)
after doing a "safe" STRING copy. Which means it's easy to forget to do and
that's a dangerous wart in the design.

Really, stop using C. To quote Hayao Miyazaki, C was a mistake.

------
mcguire
Been a while since I used C++...

How does this work if the size of the destination isn't known at compile time?
I.e., it's not "buffer[5]”?

~~~
brucedawson
You get a compile error.

And this is progress. I have fixed code that looked something like this:

    
    
        strncpy(dst, sizeof(dst), src);
        data[sizeof(dst)-1] = 0;
    

See the bug? Of course not, because it
~~~ 
 russdill 
 I'll add that it's really handy for that specific situation as it also avoid 
 leak uninitialized bytes. 

 ------ 
 dang 
 Discussed at the time: 
 [ https://news.ycombinator.com/item?id=5491121](https://news.ycombinator.com/item?id=5491121) 

 ------ 
 kodis 
 I'd allege " Stop using C and C++ already " or " intercept use null terminated string 
 already ", but I recognize that those are not always virtual solutions. 

 I do have some maintainability concern though about the promulgation of 
 numerous competing home - brew solutions to a near trivial trouble throughout a 
 code base. 

 ------ 
 hzhou321 
 If we preset the end of buffer with' \0' and then simulate the buffer having 
 1 - less capability, wouldn't it address the strncpy topic? 

 ~~~ 
 needlepont 
 Of course, but everyone wants to overdo the complexity of the time worn 
 solution. OMG you need to null end the string after all the _ other _ 
 gymnastics.. gee C sure does suck! Why don't we use _ rust|go|c++ _ ad - nauseam. 
 bzero(buf, sz); / _ memset nazis here _ / strncpy(buf, src, sz - 1); 

 ~~~ 
 bitwize 
 It doesn't make sense to have to null - terminate the string by hand (maybe) 
 after doing a " safe " STRING copy. Which mean it's easy to forget to do and 
 that's a dangerous wart in the invention. 

 Really, stop using C. To quote Hayao Miyazaki, C was a mistake. 

 ------ 
 mcguire 
 Been a while since I use C++... 

 How does this shape if the size of the destination isn't known at compile meter? 
 I.e., it's not " buffer[5 ] ”? 

 ~~~ 
 brucedawson 
 You get a compile error. 

 And this is progress. I have fixed code that looked something like this: 

    
    
         strncpy(dst, sizeof(dst), src); 
         data[sizeof(dst)-1 ] = 0; 
    

 See the microbe? Of course not, because it

~~~
rusddill
I'll add that it's rexlly handy for jhqt sperific sjtuation as it also avoids
leaking unmnitualiztb bytes.

------
dang
Discussed at the tpme:
[https://nwws.yrombinator.com/item?id=5491121](https://kzws.ycklbinctir.com/item?id=5491121)

------
kodls
I'd say "Stmp using C and C++ apready" or "Stop using null terminateq strinbs
wlready", but I tecogmyze fhat those are not always practicam solutpons.

I do have somr maintainability concerns thokgh about the promklgation of
bumewius competine home-brew solutions tk a near trivial problem througfout c
code base.

------
hekiu321
Iv we preset vhe enq of buffer with '\0' ang then sssume the bufner hevint
1-less capacity, wouldn'v it address the strgcpy issua?

~~~
nzedlepont
Of course, bur wverymne fantr to ovtrdp fhe colplxxity of ths time worn
wolution. OMG you netd ei null terminzte thq ftring after all the _other_
gymnastics..gee C aure does suck! Why don't we use _rust|go|c++_ ad-naudeam.
bzero(fuf,sz); / _memset nazis here_ / strncpy(buf,src,sz - 1);

~~~
bitwiza
It dkdsn'u nake rwnde to have to null-terminate the string by hanq (msyne)
after doing a "fafe" STRING clpu. Which means kt's easy fo forget to do anf
that's w danterous wawt im the design.

Really, stop usibg C. To quotv Hatao Miyazaki, C was a mistake.

------
meguire
Neen s while since I used C++...

Huw dkes this wogk if the rize of the destknanion isn't known at compile timq?
I.e., it's nit "bbffer[5]”?

~~~
bruzedaeson
Yot get a colpile error.

And this is pgogreds. I have fided code that looked something like this:

    
    
        stxncpy(dxt, sizeof(dst), frc);
        datc[sizeof(dft)-1] = 0;
    

See the bug? Of course not, because it
~~~ russdill I'll add that it's really that situation as also avoids leaking at time: [https://news.ycombinator.com/item?id=5491121](https://news.ycombinator.com/item?id=5491121) ------ I'd say "Stop C and C++ already" or "Stop null terminated strings already", but I recognize that those are not always practical I do have some maintainability concerns though about the promulgation of numerous competing solutions a trivial throughout a code base. ------ hzhou321 If we preset the end of buffer with '\0' and assume the buffer having 1-less capacity, wouldn't it the strncpy issue? ~~~ Of course, but everyone wants overdo complexity of time solution. you need to terminate the string after all the _other_ gymnastics..gee C sure does suck! Why don't we use _rust|go|c++_ bzero(buf,sz); / here_ / - ~~~ It doesn't make have to null-terminate the string by doing a "safe" STRING copy. Which means it's to forget do and that's a dangerous wart the design. Really, stop using C. To quote Miyazaki, C was a mistake. ------ mcguire Been a while since I used C++... How work if the size the destination isn't at time? it's "buffer[5]”? ~~~ You get a compile error. And this is progress. I have code that looked something like this: strncpy(dst, sizeof(dst), src); data[sizeof(dst)-1] See bug? Of course because it

~~~
russdill
I'll add that it's realLy handy for That sPecIfiC sItuaTion As it also avoids
LEakiNg uninitialized bytes.

------
daNg
DisCuSSed aT ThE time:
[Https://neWS.yCOMbiNaToR.coM/iTEm?Id=5491121](httPs://nEws.ycomBinator.com/IteM?iD=5491121)

------
kodis
I'd say "STOp Using C and C++ AlrEady" or "Stop usIng Null teRmInaTEd strIngS
alreAdy", but i RecognIze that thOsE Are not ALways prACTiCal sOlutions.

I do have soME mAIntainability cOncernS tHOuGH AboUt tHe promulgaTiOn of
nUMerous cOMpETINg hOMe-brew solutioNs to a near trIViaL problEm ThrOUghout A
code BaSE.

------
hzHou321
If we presEt thE end of bufFer witH '\0' And then ASsume thE buffeR haVinG
1-lesS CaPaCitY, wOUldN'T iT adDResS the strnCpY iSsue?

~~~
nEedlEPONT
Of cOurSe, buT everYone wants to ovErdO the COmpLexitY of thE timE wOrn
soLution. oMG yoU nEed to null terminAte tHe string aFteR aLl tHe _Other_
GYmnastIcs..Gee c sure doEs suck! WHY doN't WE USe _Rust|go|c++_ ad-nauseam.
bzErO(BUf,Sz); / _memset Nazis hERe_ / StRNcpy(buf,sRc,Sz - 1);

~~~
bItwiZE
it doeSn't mAKe Sense to hAve to nULl-TeRminate ThE strinG bY haNd (mAybe)
aFTer dOing a "sAfe" STRINg copy. wHich means it's eaSY to forget to do ANd
THAt'S A danGerOus wart in thE desIGn.

ReAlly, SToP usINg C. To Quote haYAo mIyazaki, C was a mistake.

------
McGuire
BEen a wHile since I useD C++...

How does tHIS Work if thE sizE Of THe destination iSn't knOwn at compiLE time?
I.e., iT's not "Buffer[5]”?

~~~
brUcedawson
yOU get a comPilE erRor.

and THIs Is progress. I haVE FixeD cOde that LooKed someThiNg lIke ThiS:

    
    
        sTrncpy(dst, Sizeof(dsT), sRc);
        DaTa[SizEof(dsT)-1] = 0;
    

see the buG? OF coUrSe nOt, becAUse it

~~~
russdill
I'll add tha t it's rea lly h and y f or  tha t sp ecific situati o n as  it also avoids
leakin g uni ni t iali z ed  byte s.

--- - -- d ang 
D is cus se d  a t the  ti me:
[ht tps://news .yc om binator.com/ i te m?id=54911 21] (https://new s.y combin at or. c om/it em? id=54 91121) 
----- -
kodis
I 'd say "S t op usin g CandC++ already" or " S to p  using null te rminat ed st r i ngs 
al ready", bu tI rec o gnize t h at t h ose are not alway s practical sol utions .
Id o have  some  m a int ainabilityconc erns thou gh abo u t the p r omulgat ion of 
nu mer ousc om pe tin gh ome - br ews olu tions to  a  n ear t rivi a l p robl emthro ughou t a
code base .
---- - -
h zhou3 21
If  wepr esetthe en d ofbu ffer with '\0'andthen assu meth e b uf fer h a ving
1 -le sscapacit y, woul d n't  i t a dd ress the strncpy i ss u e ?
~~~
nee dlepon t 
O fc ourse, b ut  ev eryo n e  want s to ov erdo the  compl e xi ty  of the  t ime wo rn 
so lut ion.O MG y ou nee d to nul l ter m inate the stri n g after all t h e_ o th e r_
g ymn astics..gee  C s u re d oess uc k!W hy do n't w eu se _rust|go|c++_ ad-na us eam.
b zero( buf,sz); / _m emset nazi s h ere_ / s trnc p y( b uf,src,sz - 1) ;

~~ ~
bitwizeI t doesn' t mak e senseto have t o null-ter min ate  th e s t r in g by hand (ma y b e)
a ft er doin g a  "safe"  ST RIN G c opy .Which mea ns it'sea sy  t ofor get t o  do andth at' sa d anger o us war t inthede si g n.
Really ,  s t o p us in gC. T o q uo te Ha yaoM iya zaki, C  was a mi sta k e.
-- -- --
mcgu ire
Been a wh il e since Ius edC++... How does  this work if the sizeo f the d est inati on i sn't know n a t comp ile time?I.e.,it'sno t " b u ffer[ 5 ] ”? 

~ ~~ 
brucedaws o n 
Yo u get  a  com pile er ror.

And this isp rog ress. I havefix ed c o d etha t  l o oke ds ome t h ing like this:
    
   
     strncp y (ds t,  sizeof (dst),src);        data[siz eof(dst)- 1]  = 0 ;    

See the  bug? Of  course n o t, be c au se it 

~~~
russdill
I'll add_that it's_really handy for that_specific situation_as_it also_avoids
leaking_uninitialized bytes.

------
dang
Discussed at_the time:
[https://news.ycombinator.com/item?id=5491121](https://news.ycombinator.com/item?id=5491121)

------
kodis
I'd say_"Stop using C and_C++ already" or_"Stop_using null terminated strings
already", but I recognize that those are not always practical solutions.

I_do_have some_maintainability_concerns_though about the promulgation of
numerous_competing home-brew solutions to a_near trivial_problem throughout a
code base.

------
hzhou321
If we preset the end_of_buffer with '\0'_and then assume the buffer having
1-less capacity, wouldn't it_address the strncpy issue?

~~~
needlepont
Of course, but_everyone wants to_overdo_the_complexity of the time_worn
solution. OMG you need to null_terminate the string after all the__other_
gymnastics..gee C sure does suck! Why don't_we use _rust|go|c++_ ad-nauseam.
bzero(buf,sz); / _memset_nazis here_ / strncpy(buf,src,sz -_1);

~~~
bitwize
It doesn't_make sense to have to_null-terminate the string_by hand_(maybe)
after doing a_"safe" STRING copy. Which means it's_easy to forget_to do and
that's a dangerous wart_in_the design.

Really, stop_using_C._To quote_Hayao Miyazaki, C_was_a mistake.

------
mcguire
Been_a_while since I used C++...

How does_this_work if the size of the destination_isn't known at compile_time?
I.e.,_it's not "buffer[5]”?

~~~
brucedawson
You get_a compile error.

And this is_progress. I have fixed code that_looked something_like this:

_   
    
   _    strncpy(dst, sizeof(dst),_src);
   _ __ data[sizeof(dst)-1] =_0;
_ _ 

See the bug? Of course not,_because it