And this actualy happens to me recently. I had to fix B to leave the stack alone.
IMHO it's generally better to only touch the stack in A and isolate B from Lua entirely. I tend to separate concerns like this:
* Validate argument types
* Fetch argument values from the stack
* Call function B with no lua_State access
* Collect results from B, push whatever appropriate value(s) onto the stack
I actually like this - unless the purpose of the function is really really simple, I always prefer to have the actual work happening in a separate function. It nicely separates the Lua glue from the actual functionality, and it's easier to test.
It might be a bit hard to follow, since we've abstracted a ton of useful Lua glue work into something called LuaSkin, but I consider this to be pretty much my ideal Lua C function: