Skip to content

Commit b1ada1c

Browse files
committed
Merge branch 'js/coverity-unchecked-returns-fix' into jch
A handful of code paths have been corrected to check return values from functions like curl_easy_duphandle(), deflateInit(), lseek(), dup(), and strbuf_getline_lf(), resolving several Coverity warnings about unchecked returns. * js/coverity-unchecked-returns-fix: bisect: handle dup() failure when redirecting stdout bisect: check get_terms return at all call sites bisect: check strbuf_getline_lf return when reading terms transport-helper: warn when export-marks file cannot be finalized transport-helper: check dup() return in get_exporter compat/pread: check initial lseek for errors last-modified: handle repo_parse_commit() failures reftable tests: check reftable_table_init_ref_iterator() return reftable/block: check deflateInit() return value config: propagate launch_editor() failure in show_editor() http: die on curl_easy_duphandle failure in get_active_slot
2 parents 0d1ec89 + a5547dd commit b1ada1c

9 files changed

Lines changed: 54 additions & 12 deletions

File tree

bisect.c

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

builtin/bisect.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,15 @@ static int get_terms(struct bisect_terms *terms)
562562
}
563563

564564
free_terms(terms);
565-
strbuf_getline_lf(&str, fp);
565+
if (strbuf_getline_lf(&str, fp) == EOF) {
566+
res = -1;
567+
goto finish;
568+
}
566569
terms->term_bad = strbuf_detach(&str, NULL);
567-
strbuf_getline_lf(&str, fp);
570+
if (strbuf_getline_lf(&str, fp) == EOF) {
571+
res = -1;
572+
goto finish;
573+
}
568574
terms->term_good = strbuf_detach(&str, NULL);
569575

570576
finish:
@@ -1143,6 +1149,8 @@ static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
11431149
*word_end = '\0'; /* NUL-terminate the word */
11441150

11451151
get_terms(terms);
1152+
if (!terms->term_bad || !terms->term_good)
1153+
return error(_("no terms defined"));
11461154
if (check_and_set_terms(terms, p))
11471155
return -1;
11481156

@@ -1411,6 +1419,11 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
14111419

14121420
fflush(stdout);
14131421
saved_stdout = dup(1);
1422+
if (saved_stdout < 0) {
1423+
res = error_errno(_("could not duplicate stdout"));
1424+
close(temporary_stdout_fd);
1425+
break;
1426+
}
14141427
dup2(temporary_stdout_fd, 1);
14151428

14161429
res = bisect_state(terms, 1, &new_state, true);
@@ -1492,6 +1505,8 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
14921505
return error(_("'%s' requires 0 arguments"),
14931506
"git bisect next");
14941507
get_terms(&terms);
1508+
if (!terms.term_bad || !terms.term_good)
1509+
return error(_("no terms defined"));
14951510
res = bisect_next(&terms, prefix, false);
14961511
free_terms(&terms);
14971512
return res;
@@ -1526,6 +1541,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
15261541

15271542
set_terms(&terms, "bad", "good");
15281543
get_terms(&terms);
1544+
if (!terms.term_bad || !terms.term_good)
1545+
return error(_("no terms defined"));
15291546
res = bisect_skip(&terms, argc, argv);
15301547
free_terms(&terms);
15311548
return res;
@@ -1538,6 +1555,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
15381555
struct bisect_terms terms = { 0 };
15391556

15401557
get_terms(&terms);
1558+
if (!terms.term_bad || !terms.term_good)
1559+
return error(_("no terms defined"));
15411560
res = bisect_visualize(&terms, argc, argv);
15421561
free_terms(&terms);
15431562
return res;
@@ -1552,6 +1571,8 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
15521571
if (!argc)
15531572
return error(_("'%s' failed: no command provided."), "git bisect run");
15541573
get_terms(&terms);
1574+
if (!terms.term_bad || !terms.term_good)
1575+
return error(_("no terms defined"));
15551576
res = bisect_run(&terms, argc, argv);
15561577
free_terms(&terms);
15571578
return res;
@@ -1591,6 +1612,8 @@ int cmd_bisect(int argc,
15911612

15921613
set_terms(&terms, "bad", "good");
15931614
get_terms(&terms);
1615+
if (!terms.term_bad || !terms.term_good)
1616+
return error(_("no terms defined"));
15941617
if (check_and_set_terms(&terms, argv[0]) ||
15951618
!one_of(argv[0], terms.term_good, terms.term_bad, NULL))
15961619
usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,

builtin/config.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,10 @@ static int show_editor(struct config_location_options *opts)
13131313
else if (errno != EEXIST)
13141314
die_errno(_("cannot create configuration file %s"), config_file);
13151315
}
1316-
launch_editor(config_file, NULL, NULL);
1316+
if (launch_editor(config_file, NULL, NULL)) {
1317+
free(config_file);
1318+
return -1;
1319+
}
13171320
free(config_file);
13181321

13191322
return 0;

builtin/last-modified.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ static void process_parent(struct last_modified *lm,
290290
{
291291
struct bitmap *active_p;
292292

293-
repo_parse_commit(lm->rev.repo, parent);
293+
if (repo_parse_commit(lm->rev.repo, parent))
294+
return;
294295
active_p = active_paths_for(lm, parent);
295296

296297
/*
@@ -414,12 +415,14 @@ static int last_modified_run(struct last_modified *lm)
414415
* Otherwise, make sure that 'c' isn't reachable from anything
415416
* in the '--not' queue.
416417
*/
417-
repo_parse_commit(lm->rev.repo, c);
418+
if (repo_parse_commit(lm->rev.repo, c))
419+
continue;
418420

419421
while ((n = prio_queue_get(&not_queue))) {
420422
struct commit_list *np;
421423

422-
repo_parse_commit(lm->rev.repo, n);
424+
if (repo_parse_commit(lm->rev.repo, n))
425+
continue;
423426

424427
for (np = n->parents; np; np = np->next) {
425428
if (!(np->item->object.flags & PARENT2)) {

compat/pread.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ ssize_t git_pread(int fd, void *buf, size_t count, off_t offset)
77
ssize_t rc;
88

99
current_offset = lseek(fd, 0, SEEK_CUR);
10+
if (current_offset < 0)
11+
return -1;
1012

1113
if (lseek(fd, offset, SEEK_SET) < 0)
1214
return -1;

http.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,8 @@ struct active_request_slot *get_active_slot(void)
16081608

16091609
if (!slot->curl) {
16101610
slot->curl = curl_easy_duphandle(curl_default);
1611+
if (!slot->curl)
1612+
die("curl_easy_duphandle failed");
16111613
curl_session_count++;
16121614
}
16131615

reftable/block.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
8787
REFTABLE_CALLOC_ARRAY(bw->zstream, 1);
8888
if (!bw->zstream)
8989
return REFTABLE_OUT_OF_MEMORY_ERROR;
90-
deflateInit(bw->zstream, 9);
90+
if (deflateInit(bw->zstream, 9) != Z_OK)
91+
return REFTABLE_ZLIB_ERROR;
9192
}
9293

9394
return 0;

t/unit-tests/u-reftable-table.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void test_reftable_table__seek_once(void)
3232
ret = reftable_table_new(&table, &source, "name");
3333
cl_assert(!ret);
3434

35-
reftable_table_init_ref_iterator(table, &it);
35+
ret = reftable_table_init_ref_iterator(table, &it);
36+
cl_assert_equal_i(ret, 0);
3637
ret = reftable_iterator_seek_ref(&it, "");
3738
cl_assert(!ret);
3839
ret = reftable_iterator_next_ref(&it, &ref);
@@ -74,7 +75,8 @@ void test_reftable_table__reseek(void)
7475
ret = reftable_table_new(&table, &source, "name");
7576
cl_assert(!ret);
7677

77-
reftable_table_init_ref_iterator(table, &it);
78+
ret = reftable_table_init_ref_iterator(table, &it);
79+
cl_assert_equal_i(ret, 0);
7880

7981
for (size_t i = 0; i < 5; i++) {
8082
ret = reftable_iterator_seek_ref(&it, "");

transport-helper.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ static int get_exporter(struct transport *transport,
487487
/* we need to duplicate helper->in because we want to use it after
488488
* fastexport is done with it. */
489489
fastexport->out = dup(helper->in);
490+
if (fastexport->out < 0)
491+
return error_errno(_("could not dup helper output fd"));
490492
strvec_push(&fastexport->args, "fast-export");
491493
strvec_push(&fastexport->args, "--use-done-feature");
492494
strvec_push(&fastexport->args, data->signed_tags ?
@@ -1191,7 +1193,9 @@ static int push_refs_with_export(struct transport *transport,
11911193

11921194
if (data->export_marks) {
11931195
strbuf_addf(&buf, "%s.tmp", data->export_marks);
1194-
rename(buf.buf, data->export_marks);
1196+
if (rename(buf.buf, data->export_marks))
1197+
warning_errno(_("could not rename '%s' to '%s'"),
1198+
buf.buf, data->export_marks);
11951199
strbuf_release(&buf);
11961200
}
11971201

0 commit comments

Comments
 (0)