[Swan] strncpy doesn't do what many people think that it does

Wes Hardaker opensource at hardakers.net
Fri Feb 15 06:55:02 EET 2013


"D. Hugh Redelmeier" <hugh at mimosa.com> writes:

> Not complete: it won't ensure a final NUL.
> Not safe: it won't ensure a final NUL.

Agreed.  And I've used wrappers in the past that copy N-1 bytes, and
force-NULL the last byte.  That at least is safe.

However,

> 	assert(n != 0);		/* won't hold a string! */

Asserts are pure evil in production servers.  They cause important
software to go down and are an easy spot for denial of service attacks.
The better thing to do is return an error code, let the above code catch
and handle it and keep running.

-- 
Wes Hardaker                                     
My Pictures:  http://capturedonearth.com/
My Thoughts:  http://pontifications.hardakers.net/


More information about the Swan mailing list