add NULL checks to ensure strlen does not reach undefined behavior
thank you "Sprocklem"