Some coverity inspired cleanups
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
diff --git a/src/config.c b/src/config.c
index 757ff03..4bd27a8 100644
--- a/src/config.c
+++ b/src/config.c
@@ -153,19 +153,19 @@ int git_config_snapshot(git_config **out, git_config *in)
git_config_backend *b;
if ((error = internal->file->snapshot(&b, internal->file)) < 0)
- goto on_error;
+ break;
if ((error = git_config_add_backend(config, b, internal->level, 0)) < 0) {
b->free(b);
- goto on_error;
+ break;
}
}
- *out = config;
- return error;
+ if (error < 0)
+ git_config_free(config);
+ else
+ *out = config;
-on_error:
- git_config_free(config);
return error;
}
diff --git a/src/config_file.c b/src/config_file.c
index b00d082..fdc9bc8 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -313,6 +313,7 @@ static int config__refresh(git_config_backend *cfg)
goto out;
reader = git_array_get(b->readers, git_array_size(b->readers) - 1);
+ GITERR_CHECK_ALLOC(reader);
if ((error = config_parse(values->values, b, reader, b->level, 0)) < 0)
goto out;
@@ -327,7 +328,8 @@ static int config__refresh(git_config_backend *cfg)
out:
refcounted_strmap_free(values);
- git_buf_free(&reader->buffer);
+ if (reader)
+ git_buf_free(&reader->buffer);
return error;
}
@@ -344,8 +346,8 @@ static int config_refresh(git_config_backend *cfg)
&reader->buffer, reader->file_path,
&reader->file_mtime, &reader->file_size, &updated);
- if (error < 0)
- return (error == GIT_ENOTFOUND) ? 0 : error;
+ if (error < 0 && error != GIT_ENOTFOUND)
+ return error;
if (updated)
any_updated = 1;
diff --git a/src/diff_driver.c b/src/diff_driver.c
index fc1354f..dc8e79e 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -233,17 +233,17 @@ static int git_diff_driver_load(
return 0;
}
+ drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
+ GITERR_CHECK_ALLOC(drv);
+ drv->type = DIFF_DRIVER_AUTO;
+ memcpy(drv->name, driver_name, namelen);
+
/* if you can't read config for repo, just use default driver */
if (git_repository_config_snapshot(&cfg, repo) < 0) {
giterr_clear();
goto done;
}
- drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
- GITERR_CHECK_ALLOC(drv);
- drv->type = DIFF_DRIVER_AUTO;
- memcpy(drv->name, driver_name, namelen);
-
if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
goto done;
diff --git a/src/tag.c b/src/tag.c
index 1a4ee1e..d7b531d 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -363,20 +363,22 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
}
/* write the buffer */
- if (git_odb_open_wstream(&stream, odb, strlen(buffer), GIT_OBJ_TAG) < 0)
- return -1;
+ if ((error = git_odb_open_wstream(
+ &stream, odb, strlen(buffer), GIT_OBJ_TAG)) < 0)
+ return error;
- git_odb_stream_write(stream, buffer, strlen(buffer));
+ if (!(error = git_odb_stream_write(stream, buffer, strlen(buffer))))
+ error = git_odb_stream_finalize_write(oid, stream);
- error = git_odb_stream_finalize_write(oid, stream);
git_odb_stream_free(stream);
if (error < 0) {
git_buf_free(&ref_name);
- return -1;
+ return error;
}
- error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL, NULL);
+ error = git_reference_create(
+ &new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL, NULL);
git_reference_free(new_ref);
git_buf_free(&ref_name);