Fix unintentional variable shadowing, and off-by-one error in the unit tests

Both were introduced when we switched from tracking len-NUL to size (including NUL).