Skip to content

Commit 2a0d0bd

Browse files
authored
Merge pull request #6238 from libgit2/ethomson/coverity
Some minor fixes for issues discovered by coverity
2 parents da0b035 + 073e63d commit 2a0d0bd

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

src/cli/opt_usage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int cli_opt_help_fprint(
150150
{
151151
git_str help = GIT_BUF_INIT;
152152
const cli_opt_spec *spec;
153-
int error;
153+
int error = 0;
154154

155155
/* Display required arguments first */
156156
for (spec = specs; spec->type; ++spec) {

src/libgit2/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ int git_object_lookup_prefix(
250250
if (error < 0)
251251
return error;
252252

253+
GIT_ASSERT(odb_obj);
253254
error = git_object__from_odb_object(object_out, repo, odb_obj, type);
254255

255256
git_odb_object_free(odb_obj);

src/libgit2/refdb_fs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,11 @@ static int packed_write(refdb_fs_backend *backend)
13611361

13621362
for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
13631363
struct packref *ref = git_sortedcache_entry(refcache, i);
1364-
GIT_ASSERT(ref);
1364+
1365+
GIT_ASSERT_WITH_CLEANUP(ref, {
1366+
error = -1;
1367+
goto fail;
1368+
});
13651369

13661370
if ((error = packed_find_peel(backend, ref)) < 0)
13671371
goto fail;

src/libgit2/transports/httpclient.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static int on_body(http_parser *parser, const char *buf, size_t len)
395395
size_t max_len;
396396

397397
/* Saw data when we expected not to (eg, in consume_response_body) */
398-
if (ctx->output_buf == NULL && ctx->output_size == 0) {
398+
if (ctx->output_buf == NULL || ctx->output_size == 0) {
399399
ctx->parse_status = PARSE_STATUS_NO_OUTPUT;
400400
return 0;
401401
}

src/util/assert_safe.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
# define GIT_ASSERT_WITH_RETVAL(expr, fail) assert(expr)
2626
# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) assert(expr)
27+
28+
# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) assert(expr)
2729
#else
2830

2931
/** Internal consistency check to stop the function. */
@@ -53,6 +55,20 @@
5355
} \
5456
} while(0)
5557

58+
/**
59+
* Go to to the given label on assertion failures; useful when you have
60+
* taken a lock or otherwise need to release a resource.
61+
*/
62+
# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) \
63+
GIT_ASSERT__WITH_CLEANUP(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", cleanup)
64+
65+
# define GIT_ASSERT__WITH_CLEANUP(expr, code, msg, cleanup) do { \
66+
if (!(expr)) { \
67+
git_error_set(code, "%s: '%s'", msg, #expr); \
68+
cleanup; \
69+
} \
70+
} while(0)
71+
5672
#endif /* GIT_ASSERT_HARD */
5773

5874
#endif

src/util/fs_path.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int git_fs_path_basename_r(git_str *buffer, const char *path)
109109
/* Empty or NULL string gets treated as "." */
110110
if (path == NULL || *path == '\0') {
111111
startp = ".";
112-
len = 1;
112+
len = 1;
113113
goto Exit;
114114
}
115115

@@ -121,7 +121,7 @@ int git_fs_path_basename_r(git_str *buffer, const char *path)
121121
/* All slashes becomes "/" */
122122
if (endp == path && *endp == '/') {
123123
startp = "/";
124-
len = 1;
124+
len = 1;
125125
goto Exit;
126126
}
127127

@@ -193,8 +193,7 @@ int git_fs_path_dirname_r(git_str *buffer, const char *path)
193193

194194
if (endp - path + 1 > INT_MAX) {
195195
git_error_set(GIT_ERROR_INVALID, "path too long");
196-
len = -1;
197-
goto Exit;
196+
return -1;
198197
}
199198

200199
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
@@ -219,8 +218,7 @@ int git_fs_path_dirname_r(git_str *buffer, const char *path)
219218

220219
if (endp - path + 1 > INT_MAX) {
221220
git_error_set(GIT_ERROR_INVALID, "path too long");
222-
len = -1;
223-
goto Exit;
221+
return -1;
224222
}
225223

226224
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {

tests/util/assert.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ static const char *bad_returns_string(void)
3636
return hello_world;
3737
}
3838

39+
static int has_cleanup(void)
40+
{
41+
int error = 42;
42+
43+
GIT_ASSERT_WITH_CLEANUP(1 + 1 == 3, {
44+
error = 99;
45+
goto foobar;
46+
});
47+
48+
return 0;
49+
50+
foobar:
51+
return error;
52+
}
53+
3954
void test_assert__argument(void)
4055
{
4156
cl_git_fail(dummy_fn(NULL));
@@ -92,3 +107,11 @@ void test_assert__internal(void)
92107
cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass);
93108
cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
94109
}
110+
111+
void test_assert__with_cleanup(void)
112+
{
113+
cl_git_fail_with(99, has_cleanup());
114+
cl_assert(git_error_last());
115+
cl_assert_equal_i(GIT_ERROR_INTERNAL, git_error_last()->klass);
116+
cl_assert_equal_s("unrecoverable internal error: '1 + 1 == 3'", git_error_last()->message);
117+
}

0 commit comments

Comments
 (0)