Fix buffer overflow; call strdup only when 3-rd argument is missing

I didn't compile this code, so please verify it works correctly. it's simple enough that I believe there shouldn't be issues.