Prevent possible infinite loop on a malformed buffer_t

If the buffer was all-zeros, this could have looped forever.