threads / patch / 37583

patchhelp: Fix size passed to qsort

Subject: [PATCH] help: Fix size passed to qsort

## tl;dr

3 messages between Sep 17, 2014 and Sep 18, 2014. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Stefan Beller· Sep 17, 2014, 12:14 UTC · lore

We actually want to have the size of one 'name' and not the size of the names array.

Signed-off-by: Stefan Beller <stefanbeller@gmail.com>
---
 help.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to help.c +1 −1
diff --git a/help.c b/help.c
index 7af65e2..2072a87 100644
--- a/help.c
+++ b/help.c
@@ -305,7 +305,7 @@ const char *help_unknown_cmd(const char *cmd)
 	add_cmd_list(&main_cmds, &aliases);
 	add_cmd_list(&main_cmds, &other_cmds);
 	qsort(main_cmds.names, main_cmds.cnt,
-	      sizeof(main_cmds.names), cmdname_compare);
+	      sizeof(*main_cmds.names), cmdname_compare);
 	uniq(&main_cmds);
 
 	/* This abuses cmdname->len for levenshtein distance */
-- 
2.1.0.238.gce1d3a9
Junio C Hamano· Sep 18, 2014, 17:17 UTC · re: Stefan Beller · lore

Re: [PATCH] help: Fix size passed to qsort

Stefan Beller <stefanbeller@gmail.com> writes:
> We actually want to have the size of one 'name' and not the size
> of the names array.

I suspect that the latter is "size of a pointer that points at a cmdname structure", but the original code in help_unknown_cmd() is wrong. The ones in load_command_list() do this correctly and another qsort() invocation in this function does so as well. I wonder why they didn't correctly cut&paste ;-)

746c221a (git wrapper: also use aliases to correct mistyped commands, 2008-09-10) seemed to have introduced the culprit.

The call to uniq() would fail to uniquify because main_cmds would have the standard command all in front and then aliases and commands in the user's PATH later, but I do not quite see if there is any end-user observable breakages that can arise from this. What is the practical implication of this breakage?

No, I am not saying we do not have to fix it; I am just being curious why this patch does not show the existing breakage with a new test.

Thanks.
Show 19 quoted lines
>
> Signed-off-by: Stefan Beller <stefanbeller@gmail.com>
> ---
>  help.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/help.c b/help.c
> index 7af65e2..2072a87 100644
> --- a/help.c
> +++ b/help.c
> @@ -305,7 +305,7 @@ const char *help_unknown_cmd(const char *cmd)
>  	add_cmd_list(&main_cmds, &aliases);
>  	add_cmd_list(&main_cmds, &other_cmds);
>  	qsort(main_cmds.names, main_cmds.cnt,
> -	      sizeof(main_cmds.names), cmdname_compare);
> +	      sizeof(*main_cmds.names), cmdname_compare);
>  	uniq(&main_cmds);
>  
>  	/* This abuses cmdname->len for levenshtein distance */
Junio C Hamano· Sep 18, 2014, 17:34 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: Fix size passed to qsort

Junio C Hamano <gitster@pobox.com> writes:
Show 19 quoted lines
> Stefan Beller <stefanbeller@gmail.com> writes:
>
>> We actually want to have the size of one 'name' and not the size
>> of the names array.
> ...
> I suspect that the latter is "size of a pointer that points at a
> cmdname structure", but the original code in help_unknown_cmd() is
> wrong.  The ones in load_command_list() do this correctly and
> another qsort() invocation in this function does so as well.  I
> wonder why they didn't correctly cut&paste ;-)
>
> 746c221a (git wrapper: also use aliases to correct mistyped
> commands, 2008-09-10) seemed to have introduced the culprit.
>
> The call to uniq() would fail to uniquify because main_cmds would
> have the standard command all in front and then aliases and commands
> in the user's PATH later, but I do not quite see if there is any
> end-user observable breakages that can arise from this.  What is the
> practical implication of this breakage?

Heh, I should have spent a bit more time before starting to type. The answer probably is "nothing", as

	struct cmdnames {
        	...
                struct cmdname {
                	...
		} **names;
	} main_cmds;

is what we are dealing here, so main_cmds.names is a pointer that points at a slab of memory to hold many pointers, each of which points at "struct cmdname". And sizeof(struct cmdname **) is incorrectly passed where we should pass sizeof(struct cmdname *), which you fixed, but in practice they would be the same size anyway ;-)

Show 6 quoted lines
>>  	qsort(main_cmds.names, main_cmds.cnt,
>> -	      sizeof(main_cmds.names), cmdname_compare);
>> +	      sizeof(*main_cmds.names), cmdname_compare);
>>  	uniq(&main_cmds);
>>  
>>  	/* This abuses cmdname->len for levenshtein distance */

← back to recent threads