[Swan] strncpy doesn't do what many people think that it does
Wes Hardaker
opensource at hardakers.net
Fri Feb 15 17:08:11 EET 2013
Paul Wouters <paul at nohats.ca> writes:
> So I kinda like asserts. Just returning and logging will get ignored for
> years....
FYI, here's the assert we use in net-snmp:
#ifdef NETSNMP_USE_ASSERT
/* void netsnmp_assert( int );*/
# define netsnmp_assert(x) assert( x )
# define netsnmp_assert_or_return(x, y) assert( x )
# define netsnmp_assert_or_msgreturn(x, y, z) assert( x )
#else
/* much longer code truncated down to this simple example: */
# define netsnmp_assert(x) do { \
if ( x ) \
; \
else \
snmp_log(LOG_ERR, \
"netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
__STRING(x),__FILE__,__LINE__, \
NETSNMP_FUNC_PARAM); \
}while(0)
...
Thus, it does nothing in production mode or asserts when in developer
mode.
Also not, that the other two functions should be the ones actually
used (but the non-developer mode version is not shown here). They do
something better: assert when in developer mode for debugging, and
return a value when not allowing up-code error recovery to do the right
thing. IMHO, this gives you the best of both worlds.
(and our release script has a grep for asserts to ensure we don't ever
release code with a real assert in it)
--
Wes Hardaker
My Pictures: http://capturedonearth.com/
My Thoughts: http://pontifications.hardakers.net/
More information about the Swan
mailing list