Fix -k argument processing to avoid out-of-bounds memory accesses

Mangling of keyfile_dir and allocation of keyfile_path led to rare
crashes (and Valgrind complaints).

The error was introduced in 21f3a6b9.
3 jobs for k_param
in 1 minute and 38 seconds and was queued for 3 seconds