lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Sir Pogsalot once stated:
> Maybe this is just something that was drilled into me from a young age but
> I've always operated under the "libraries should never assert() or
> segfault" mindset.  

  True, and I try to write my libraries that don't assert() or segfault. The
difference lies in parameter validation---I don't.  My working mantra is
"give me valid data, and I'll give you valid data back." [1] To do otherwise
is to hide bugs (really---the way I approach programming really changed
after reading _Writing Solid Code_).

> Also, users are sometimes directly scripting in Lua --
> WoW, awesome window manager people, Garry's Mod, Android game creation
> apps/frameworks, etc...  Sandboxed environments maybe, but still directly
> working with library functions.  I don't think I'm being overzealous by
> calling this minimal amount of 'defensive programming' necessary :>  

  Let's just agree to disagree on this. 8-)

> Also lua_objlen() existed in 5.1 -- and it pains me daily to think how
> many libraries out there only luaL_checkudata() and blindly access
> members.

  Heck, have you looked at the source to Lua itself?  Not a single call to
lua_objlen() or lua_rawlen() in sight.

>  Thanks for contributing to my early death :<

  You're welcome.

  -spc (I never used training wheels when learning how to ride a bike [6])

[1]	That's for library calls.  I don't blindly trust input from the
	outside, say from a file or network connection.  A good example of
	this is dns_decode() [2] where I don't bother with parameter
	validation [3] but I validate the heck out of the DNS packet I'm
	decoding, even returning error on stuff that should be ignored and
	isn't 0. [4][5]

[2]	https://github.com/spc476/SPCDNS/blob/master/src/codec.c#L1750

[3]	Yes, I do assert() parameters, but that's just asserting that the
	input *must be valid*.  Compile with NDEBUG and that particular code
	just flies ... 

[4]	It found several implementation errors in other DNS libraries.

[5]	It even survived input from /dev/random.

[6]	I learned when my uncle sat me on a bike, shoved real hard, and the
	only advice he gave was "avoid the traffic!"

	Three days later I was riding a bike.

	I'm not sure why I thought of this, but I think there's a connection
	somewhere ...