helper/command: check for malloc failure in __command_name 38/4838/3
authorAntonio Borneo <borneo.antonio@gmail.com>
Tue, 8 Jan 2019 08:54:30 +0000 (09:54 +0100)
committerTomas Vanek <vanekt@fbl.cz>
Wed, 16 Jan 2019 10:51:28 +0000 (10:51 +0000)
commitc4a7a62262952237bb1d7db3f174ece935235ea4
tree98f4644569477a675621c03a280e69a67f546165
parentc0ec54d8ae8f7f933718f9d05efdde986b51f98c
helper/command: check for malloc failure in __command_name

If malloc fails in __command_name, the following strcpy will
segfault, thus preventing __command_name to return.
The actual calls to command_name() implement the correct check
for the NULL pointer, but propagate error -ENOMEM, that is not
an error value coherent within OpenOCD. Plus, in one case it
overwrites an already detected error.

Check the pointer returned by malloc and, in case of failure,
issue an error message and return the NULL pointer.
Let the caller of command_name() to keep the already detected
error or to return ERROR_FAIL in case of end of memory.

Change-Id: I151a24569409777dd5bc09a3daf5dba2b8e2829b
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/4838
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
src/helper/command.c