Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77394a2

Browse files
committed
Merge pull request libgit2#3677 from pks-t/pks/coverity-fixes-round7
Coverity fixes round 7
2 parents 4848dd3 + 13c371d commit 77394a2

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

src/blame.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const git_blame_hunk *git_blame_get_hunk_byline(git_blame *blame, size_t lineno)
178178
return NULL;
179179
}
180180

181-
static void normalize_options(
181+
static int normalize_options(
182182
git_blame_options *out,
183183
const git_blame_options *in,
184184
git_repository *repo)
@@ -190,7 +190,9 @@ static void normalize_options(
190190

191191
/* No newest_commit => HEAD */
192192
if (git_oid_iszero(&out->newest_commit)) {
193-
git_reference_name_to_id(&out->newest_commit, repo, "HEAD");
193+
if (git_reference_name_to_id(&out->newest_commit, repo, "HEAD") < 0) {
194+
return -1;
195+
}
194196
}
195197

196198
/* min_line 0 really means 1 */
@@ -204,6 +206,8 @@ static void normalize_options(
204206
out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
205207
if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES)
206208
out->flags |= GIT_BLAME_TRACK_COPIES_SAME_FILE;
209+
210+
return 0;
207211
}
208212

209213
static git_blame_hunk *split_hunk_in_vector(
@@ -362,7 +366,8 @@ int git_blame_file(
362366
git_blame *blame = NULL;
363367

364368
assert(out && repo && path);
365-
normalize_options(&normOptions, options, repo);
369+
if ((error = normalize_options(&normOptions, options, repo)) < 0)
370+
goto on_error;
366371

367372
blame = git_blame__alloc(repo, normOptions, path);
368373
GITERR_CHECK_ALLOC(blame);

src/blame_git.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ static int pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt)
525525
if (sg_origin[i])
526526
continue;
527527

528-
git_commit_parent(&p, origin->commit, i);
528+
if ((error = git_commit_parent(&p, origin->commit, i)) < 0)
529+
goto finish;
529530
porigin = find_origin(blame, p, origin);
530531

531532
if (!porigin)

src/config_cache.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar)
8686
struct map_data *data = &_cvar_maps[(int)cvar];
8787
git_config_entry *entry;
8888

89-
git_config__lookup_entry(&entry, config, data->cvar_name, false);
89+
if ((error = git_config__lookup_entry(&entry, config, data->cvar_name, false)) < 0)
90+
return error;
9091

9192
if (!entry)
9293
*out = data->default_value;

src/config_file.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ static refcounted_strmap *refcounted_strmap_take(diskfile_header *h)
232232
{
233233
refcounted_strmap *map;
234234

235-
git_mutex_lock(&h->values_mutex);
235+
if (git_mutex_lock(&h->values_mutex) < 0) {
236+
giterr_set(GITERR_OS, "Failed to lock config backend");
237+
return NULL;
238+
}
236239

237240
map = h->values;
238241
git_atomic_inc(&map->refcount);
@@ -318,7 +321,10 @@ static int config__refresh(git_config_backend *cfg)
318321
if ((error = config_read(values->values, b, reader, b->level, 0)) < 0)
319322
goto out;
320323

321-
git_mutex_lock(&b->header.values_mutex);
324+
if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) {
325+
giterr_set(GITERR_OS, "Failed to lock config backend");
326+
goto out;
327+
}
322328

323329
tmp = b->header.values;
324330
b->header.values = values;
@@ -460,7 +466,8 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
460466
if ((rval = git_config__normalize_name(name, &key)) < 0)
461467
return rval;
462468

463-
map = refcounted_strmap_take(&b->header);
469+
if ((map = refcounted_strmap_take(&b->header)) == NULL)
470+
return -1;
464471
values = map->values;
465472

466473
/*
@@ -527,7 +534,8 @@ static int config_get(git_config_backend *cfg, const char *key, git_config_entry
527534
if (!h->parent.readonly && ((error = config_refresh(cfg)) < 0))
528535
return error;
529536

530-
map = refcounted_strmap_take(h);
537+
if ((map = refcounted_strmap_take(h)) == NULL)
538+
return -1;
531539
values = map->values;
532540

533541
pos = git_strmap_lookup_index(values, key);
@@ -565,7 +573,8 @@ static int config_set_multivar(
565573
if ((result = git_config__normalize_name(name, &key)) < 0)
566574
return result;
567575

568-
map = refcounted_strmap_take(&b->header);
576+
if ((map = refcounted_strmap_take(&b->header)) == NULL)
577+
return -1;
569578
values = b->header.values->values;
570579

571580
pos = git_strmap_lookup_index(values, key);
@@ -610,7 +619,8 @@ static int config_delete(git_config_backend *cfg, const char *name)
610619
if ((result = git_config__normalize_name(name, &key)) < 0)
611620
return result;
612621

613-
map = refcounted_strmap_take(&b->header);
622+
if ((map = refcounted_strmap_take(&b->header)) == NULL)
623+
return -1;
614624
values = b->header.values->values;
615625

616626
pos = git_strmap_lookup_index(values, key);
@@ -649,7 +659,8 @@ static int config_delete_multivar(git_config_backend *cfg, const char *name, con
649659
if ((result = git_config__normalize_name(name, &key)) < 0)
650660
return result;
651661

652-
map = refcounted_strmap_take(&b->header);
662+
if ((map = refcounted_strmap_take(&b->header)) == NULL)
663+
return -1;
653664
values = b->header.values->values;
654665

655666
pos = git_strmap_lookup_index(values, key);
@@ -832,7 +843,8 @@ static int config_readonly_open(git_config_backend *cfg, git_config_level_t leve
832843
/* We're just copying data, don't care about the level */
833844
GIT_UNUSED(level);
834845

835-
src_map = refcounted_strmap_take(src_header);
846+
if ((src_map = refcounted_strmap_take(src_header)) == NULL)
847+
return -1;
836848
b->header.values = src_map;
837849

838850
return 0;

src/filebuf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
7070
git_file source;
7171
char buffer[FILEIO_BUFSIZE];
7272
ssize_t read_bytes;
73+
int error;
7374

7475
source = p_open(file->path_original, O_RDONLY);
7576
if (source < 0) {
@@ -80,7 +81,8 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
8081
}
8182

8283
while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) {
83-
p_write(file->fd, buffer, read_bytes);
84+
if ((error = p_write(file->fd, buffer, read_bytes)) < 0)
85+
break;
8486
if (file->compute_digest)
8587
git_hash_update(&file->digest, buffer, read_bytes);
8688
}
@@ -90,6 +92,9 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode)
9092
if (read_bytes < 0) {
9193
giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original);
9294
return -1;
95+
} else if (error < 0) {
96+
giterr_set(GITERR_OS, "Failed to write file '%s'", file->path_lock);
97+
return -1;
9398
}
9499
}
95100

src/refdb_fs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ static int packed_write(refdb_fs_backend *backend)
962962

963963
for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
964964
struct packref *ref = git_sortedcache_entry(refcache, i);
965+
assert(ref);
965966

966967
if (packed_find_peel(backend, ref) < 0)
967968
goto fail;

0 commit comments

Comments
 (0)