Skip to content

Commit c189c4f

Browse files
stefanbellergitster
authored andcommitted
diff: remove ternary operator evaluating always to true
The line being changed is deep inside the function builtin_diff. The variable name_b, which is used to evaluate the ternary expression must evaluate to true at that position, hence the replacement with just name_b. The name_b variable only occurs a few times in that lengthy function: As a parameter to the function itself: static void builtin_diff(const char *name_a, const char *name_b, ... The next occurrences are at: /* Never use a non-valid filename anywhere if at all possible */ name_a = DIFF_FILE_VALID(one) ? name_a : name_b; name_b = DIFF_FILE_VALID(two) ? name_b : name_a; a_one = quote_two(a_prefix, name_a + (*name_a == '/')); b_two = quote_two(b_prefix, name_b + (*name_b == '/')); In the last line of this block 'name_b' is dereferenced and compared to '/'. This would crash if name_b was NULL. Hence in the following code we can assume name_b being non-null. The next occurrence is just as a function argument, which doesn't change the memory, which name_b points to, so the assumption name_b being not null still holds: emit_rewrite_diff(name_a, name_b, one, two, textconv_one, textconv_two, o); The next occurrence would be the line of this patch. As name_b still must be not null, we can remove the ternary operator. Inside the emit_rewrite_diff function there is a also a line ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a); which was also simplified as there is also a dereference before the ternary operator. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64948ad commit c189c4f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ static void emit_rewrite_diff(const char *name_a,
669669
memset(&ecbdata, 0, sizeof(ecbdata));
670670
ecbdata.color_diff = want_color(o->use_color);
671671
ecbdata.found_changesp = &o->found_changes;
672-
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
672+
ecbdata.ws_rule = whitespace_rule(name_b);
673673
ecbdata.opt = o;
674674
if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
675675
mmfile_t mf1, mf2;
@@ -2372,7 +2372,7 @@ static void builtin_diff(const char *name_a,
23722372
ecbdata.label_path = lbl;
23732373
ecbdata.color_diff = want_color(o->use_color);
23742374
ecbdata.found_changesp = &o->found_changes;
2375-
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
2375+
ecbdata.ws_rule = whitespace_rule(name_b);
23762376
if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
23772377
check_blank_at_eof(&mf1, &mf2, &ecbdata);
23782378
ecbdata.opt = o;

0 commit comments

Comments
 (0)