fix dereference to null pointer
It should not happen with the current code workflow, but out of the box it may happen
fix memory leak
if environ is malformed for any reason, memory was getting allocated for nothing