Skip to content

Commit 4e09afd

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 523321e + a5547dd commit 4e09afd

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
@@ -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: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,15 @@ static int get_terms(struct bisect_terms *terms)
498498
}
499499

500500
free_terms(terms);
501-
strbuf_getline_lf(&str, fp);
501+
if (strbuf_getline_lf(&str, fp) == EOF) {
502+
res = -1;
503+
goto finish;
504+
}
502505
terms->term_bad = strbuf_detach(&str, NULL);
503-
strbuf_getline_lf(&str, fp);
506+
if (strbuf_getline_lf(&str, fp) == EOF) {
507+
res = -1;
508+
goto finish;
509+
}
504510
terms->term_good = strbuf_detach(&str, NULL);
505511

506512
finish:
@@ -1058,6 +1064,8 @@ static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
10581064
*word_end = '\0'; /* NUL-terminate the word */
10591065

10601066
get_terms(terms);
1067+
if (!terms->term_bad || !terms->term_good)
1068+
return error(_("no terms defined"));
10611069
if (check_and_set_terms(terms, p))
10621070
return -1;
10631071

@@ -1307,6 +1315,11 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
13071315

13081316
fflush(stdout);
13091317
saved_stdout = dup(1);
1318+
if (saved_stdout < 0) {
1319+
res = error_errno(_("could not duplicate stdout"));
1320+
close(temporary_stdout_fd);
1321+
break;
1322+
}
13101323
dup2(temporary_stdout_fd, 1);
13111324

13121325
res = bisect_state(terms, 1, &new_state);
@@ -1384,6 +1397,8 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
13841397
return error(_("'%s' requires 0 arguments"),
13851398
"git bisect next");
13861399
get_terms(&terms);
1400+
if (!terms.term_bad || !terms.term_good)
1401+
return error(_("no terms defined"));
13871402
res = bisect_next(&terms, prefix);
13881403
free_terms(&terms);
13891404
return res;
@@ -1418,6 +1433,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
14181433

14191434
set_terms(&terms, "bad", "good");
14201435
get_terms(&terms);
1436+
if (!terms.term_bad || !terms.term_good)
1437+
return error(_("no terms defined"));
14211438
res = bisect_skip(&terms, argc, argv);
14221439
free_terms(&terms);
14231440
return res;
@@ -1430,6 +1447,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
14301447
struct bisect_terms terms = { 0 };
14311448

14321449
get_terms(&terms);
1450+
if (!terms.term_bad || !terms.term_good)
1451+
return error(_("no terms defined"));
14331452
res = bisect_visualize(&terms, argc, argv);
14341453
free_terms(&terms);
14351454
return res;
@@ -1444,6 +1463,8 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
14441463
if (!argc)
14451464
return error(_("'%s' failed: no command provided."), "git bisect run");
14461465
get_terms(&terms);
1466+
if (!terms.term_bad || !terms.term_good)
1467+
return error(_("no terms defined"));
14471468
res = bisect_run(&terms, argc, argv);
14481469
free_terms(&terms);
14491470
return res;
@@ -1483,6 +1504,8 @@ int cmd_bisect(int argc,
14831504

14841505
set_terms(&terms, "bad", "good");
14851506
get_terms(&terms);
1507+
if (!terms.term_bad || !terms.term_good)
1508+
return error(_("no terms defined"));
14861509
if (check_and_set_terms(&terms, argv[0]) ||
14871510
!one_of(argv[0], terms.term_good, terms.term_bad, NULL))
14881511
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 ?
@@ -1182,7 +1184,9 @@ static int push_refs_with_export(struct transport *transport,
11821184

11831185
if (data->export_marks) {
11841186
strbuf_addf(&buf, "%s.tmp", data->export_marks);
1185-
rename(buf.buf, data->export_marks);
1187+
if (rename(buf.buf, data->export_marks))
1188+
warning_errno(_("could not rename '%s' to '%s'"),
1189+
buf.buf, data->export_marks);
11861190
strbuf_release(&buf);
11871191
}
11881192

0 commit comments

Comments
 (0)