Aggressively check for possible buffer overflows in cmd.cgi

Refs #5434
Merge branch 'fix/cmd-5434' into next

Refs #5434