Add some error condition checks when allocating/reallocating buffers