threads / discuss / 21337

Unreachable code in builtin-merge.c ?

Subject: Unreachable code in builtin-merge.c ?

## tl;dr

4 messages between Oct 23, 2009 and Oct 23, 2009.

replies: 3people: 3as markdown or json

Zivkov, Sasa· Oct 23, 2009, 15:03 UTC · lore
In the try_merge_strategy function the expression in the following if statement seems to always evaluate to TRUE:
        if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {

If strategy is "recursive" then !strcmp(strategy, "subtree") evaluates to TRUE. If strategy is "subtree" then !strcmp(strategy, "recursive") evaluates to TRUE. If strategy is neither "recursive" nor "subtree" then any side of || evaluates to TRUE.

Looks like the code in the else block is unreachable?
Saša Živkov
Mikael Magnusson· Oct 23, 2009, 15:12 UTC · re: Zivkov, Sasa · lore

Re: Unreachable code in builtin-merge.c ?

2009/10/23 Zivkov, Sasa <sasa.zivkov@sap.com>:
Show 9 quoted lines
> In the try_merge_strategy function the expression in the following if statement seems to always evaluate to TRUE:
>
>        if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
>
> If strategy is "recursive" then !strcmp(strategy, "subtree") evaluates to TRUE.
> If strategy is "subtree" then !strcmp(strategy, "recursive") evaluates to TRUE.
> If strategy is neither "recursive" nor "subtree" then any side of || evaluates to TRUE.
>
> Looks like the code in the else block is unreachable?

You seem to have forgotten that strcmp returns 0 when the strings match. (It returns -1 or 1 when they don't, depending on their alphabetical ordering).

-- 
Mikael Magnusson
Joshua Roys· Oct 23, 2009, 15:14 UTC · re: Zivkov, Sasa · lore

Re: Unreachable code in builtin-merge.c ?

On 10/23/2009 11:03 AM, Zivkov, Sasa wrote:
Show 16 quoted lines
> In the try_merge_strategy function the expression in the following if statement seems to always evaluate to TRUE:
>
>          if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
>
> If strategy is "recursive" then !strcmp(strategy, "subtree") evaluates to TRUE.
> If strategy is "subtree" then !strcmp(strategy, "recursive") evaluates to TRUE.
> If strategy is neither "recursive" nor "subtree" then any side of || evaluates to TRUE.
>
> Looks like the code in the else block is unreachable?
>
>
> Saša Živkov
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Hello,

Not quite :) The strcmp() function actually returns 0 when the strings match (I know, it looks weird "!strcmp" -> "not string compare?!"), so the "if" really means: "if strategy is equal to 'recursive' or 'subtree', do the following..."

Hope to help,
Josh
Zivkov, Sasa· Oct 23, 2009, 15:39 UTC · re: Joshua Roys · lore

RE: Unreachable code in builtin-merge.c ?

> Not quite :)  The strcmp() function actually returns 0 when the strings
> match (I know, it looks weird "!strcmp" -> "not string compare?!"), so
Right! Now I remember it :-)
This is what happens when one looks at C code after some 10+ years of !C development ;-)
Saša Živkov

← back to recent threads