[Pdbg] [PATCH] htm: Avoid segfault when an option is omitted

Alistair Popple alistair at popple.id.au
Tue May 8 12:46:38 AEST 2018


Thanks! Merged as 44836e05cdb36f628044ced2bd322d84fd77b0b7.

On Tuesday, 8 May 2018 10:06:56 AM AEST Rashmica Gupta wrote:
> Currently if the user omits an htm command such as 'start' or 'stop'
> we segfault. This happens because we check for the wrong number of
> arguments.
> 
> Signed-off-by: Rashmica Gupta <rashmica.g at gmail.com>
> ---
>  src/htm.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/src/htm.c b/src/htm.c
> index 566687e..084a3ba 100644
> --- a/src/htm.c
> +++ b/src/htm.c
> @@ -279,7 +279,14 @@ int run_htm(int optind, int argc, char *argv[])
>  	struct pdbg_target *core_target = NULL;
>  	int i, rc = 0;
>  
> -	if (argc - optind < 2) {
> +	/*
> +	 * As the index of the last argument is one less than argc, the difference
> +	 * between optind and argc will always be at least 1. Here optind is pointing
> +	 * to the 'htm' arg and we need at least 2 more following arguments, eg:
> +	 * htm <nest/core> <start/stop/etc>
> +	 * so argc-optind >= 3 to proceed.
> +	 */
> +	if (argc - optind < 3) {
>  		fprintf(stderr, "Expecting one of 'core' or 'nest' with a command\n");
>  		return 0;
>  	}
> 




More information about the Pdbg mailing list