Skip to content

Commit 9a7ea25

Browse files
committed
bisect: check strbuf_getline_lf return when reading terms
get_terms() in builtin/bisect.c and read_bisect_terms() in bisect.c both read the BISECT_TERMS file but do not check the strbuf_getline_lf() return values. If the file is truncated (e.g., a partial write from a crash or disk-full condition), strbuf_getline_lf returns EOF and the strbuf remains empty. strbuf_detach then returns an empty string, and the term names silently become "" instead of the expected "bad"/"good" or custom terms. In get_terms(), check for EOF and return -1 on truncation, matching the existing -1 return for a missing file. In read_bisect_terms(), die with a descriptive message when a line cannot be read, consistent with the die_errno for a non-ENOENT open failure in the same function. Unlike get_terms(), read_bisect_terms() returns void and uses die() for all error paths, so the die is the appropriate error handling here. Pointed out by Coverity. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 9efa30c commit 9a7ea25

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

bisect.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,10 +1019,12 @@ void read_bisect_terms(char **read_bad, char **read_good)
10191019
die_errno(_("could not read file '%s'"), filename);
10201020
}
10211021
} else {
1022-
strbuf_getline_lf(&str, fp);
1022+
if (strbuf_getline_lf(&str, fp) == EOF)
1023+
die(_("could not read bad term from file '%s'"), filename);
10231024
free(*read_bad);
10241025
*read_bad = strbuf_detach(&str, NULL);
1025-
strbuf_getline_lf(&str, fp);
1026+
if (strbuf_getline_lf(&str, fp) == EOF)
1027+
die(_("could not read good term from file '%s'"), filename);
10261028
free(*read_good);
10271029
*read_good = strbuf_detach(&str, NULL);
10281030
}

builtin/bisect.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,15 @@ static int get_terms(struct bisect_terms *terms)
495495
}
496496

497497
free_terms(terms);
498-
strbuf_getline_lf(&str, fp);
498+
if (strbuf_getline_lf(&str, fp) == EOF) {
499+
res = -1;
500+
goto finish;
501+
}
499502
terms->term_bad = strbuf_detach(&str, NULL);
500-
strbuf_getline_lf(&str, fp);
503+
if (strbuf_getline_lf(&str, fp) == EOF) {
504+
res = -1;
505+
goto finish;
506+
}
501507
terms->term_good = strbuf_detach(&str, NULL);
502508

503509
finish:

0 commit comments

Comments
 (0)