LWN.net Logo

What every C Programmer should know about undefined behavior #2/3

What every C Programmer should know about undefined behavior #2/3

Posted May 17, 2011 19:06 UTC (Tue) by stijn (subscriber, #570)
In reply to: What every C Programmer should know about undefined behavior #2/3 by dgm
Parent article: What every C Programmer should know about undefined behavior #2/3

Given that I am stuck where you were 20 years ago, you are bound to be right - thanks, that clarified matters greatly!


(Log in to post comments)

What every C Programmer should know about undefined behavior #2/3

Posted May 17, 2011 21:57 UTC (Tue) by baldridgeec (guest, #55283) [Link]

For your last point, basically it comes down to "if there might be a \0 in valid input for this function, even though it may be a byte[], it is not a string."

So you use memchr() instead of strchr().

Or you use C++, and call it a string instead of a byte[]. (and use the actual C++ string-manipulation functions, not strchr - g++ warns about (byte[])string as being an invalid cast nowadays anyway though) :)

What every C Programmer should know about undefined behavior #2/3

Posted May 19, 2011 14:49 UTC (Thu) by dgm (subscriber, #49227) [Link]

Your problem seems to be that you want to use the C string library to handle arrays of arbitrary bytes. I will not work, it's not what it was designed to do.

What every C Programmer should know about undefined behavior #2/3

Posted May 19, 2011 15:49 UTC (Thu) by stijn (subscriber, #570) [Link]

Just to clarify, I can deal with both the string library and the mem functions. When I come to LWN, I hope to engage in constructive conversation, not chest-beating.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds