diff --git a/src/hci/commands/image_cmd.c b/src/hci/commands/image_cmd.c index 23843a7e4..b651078b6 100644 --- a/src/hci/commands/image_cmd.c +++ b/src/hci/commands/image_cmd.c @@ -55,18 +55,21 @@ static int imgfill_cmdline ( struct image *image, unsigned int nargs, /* Determine total length of command line */ len = 1; /* NUL */ for ( i = 0 ; i < nargs ; i++ ) - len += ( 1 /* space */ + strlen ( args[i] ) ); + len += ( 1 /* possible space */ + strlen ( args[i] ) ); { char buf[len]; char *ptr = buf; /* Assemble command line */ - for ( i = 0 ; i < nargs ; i++ ) - ptr += sprintf ( ptr, " %s", args[i] ); - assert ( ptr == ( buf + len - 1 ) ); + buf[0] = '\0'; + for ( i = 0 ; i < nargs ; i++ ) { + ptr += sprintf ( ptr, "%s%s", ( i ? " " : "" ), + args[i] ); + } + assert ( ptr < ( buf + len ) ); - return image_set_cmdline ( image, &buf[1] ); + return image_set_cmdline ( image, buf ); } }