On Oct 2, 2012, at 03:37 , andy pugh wrote:

> On 2 October 2012 06:50, Sebastian Kuzminsky <s...@highlab.com> wrote:
> 
>> The intent of the interp_o_word.cc merge was to propagate a couple of
>> strcpy()/strncpy() fixes from 2.5.
> 
> Have you seen: http://blog.liw.fi/posts/strncpy/

Lars' point, i think, is that all C string functions need surrounding code to 
to be safe.  strcpy(), strncpy(), snprintf() (and all the others) have this 
quality, which he thinks is a big drawback but which doesn't bother me so much.

I hadn't thought of his other point that strncpy() is slower because it needs 
to null-pad any remainder of the destination string.  I bet in reality that's 
not that big a deal, but still his point's valid.

If the original strcpy() in our code had had buffer overflow checking, I would 
not have changed it to strncpy(), but since I was there and monkeying with it 
anyway I changed it to the (very similar) idiom that I'm used to.

In other places where there was a bunch of string manipulation, I've used 
snprintf() instead of strncpy() + strcat() etc (for example commit 59174ae, 
http://git.linuxcnc.org/gitweb?p=linuxcnc.git;a=commitdiff;h=59174ae724060585ccb0656457d492a982eb5166;hp=0e45366b9279733ee0e4fa0e77516cf59cdad56d).

Oh yeah, and I liked his joke about truncation.  "If your string handling is OK 
with strings being randomly shorter than they should be, possibly you could 
replace all of them with empty strings, and save yourself a world of pain."


-- 
Sebastian Kuzminsky


------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Emc-developers mailing list
Emc-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-developers

Reply via email to