Check that malloc or asprint was successful

Based on https://github.com/mongodb/mongo-ruby-driver/pull/464 without the crap