Skip to content

Commit 199e03f

Browse files
Don't leak memory when opening a config file fails.
1 parent 648f188 commit 199e03f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

ext/rugged/rugged_config.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,27 @@ VALUE rugged_config_new(VALUE klass, VALUE owner, git_config *cfg)
5050
static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
5151
{
5252
git_config *config = NULL;
53-
int error, i;
5453

5554
if (TYPE(rb_path) == T_ARRAY) {
55+
int error, i;
56+
5657
error = git_config_new(&config);
5758
rugged_exception_check(error);
5859

59-
for (i = 0; i < RARRAY_LEN(rb_path); ++i) {
60+
for (i = 0; i < RARRAY_LEN(rb_path) && !error; ++i) {
6061
VALUE f = rb_ary_entry(rb_path, i);
6162
Check_Type(f, T_STRING);
6263
error = git_config_add_file_ondisk(config, StringValueCStr(f), i + 1, 1);
64+
}
65+
66+
if (error) {
67+
git_config_free(config);
6368
rugged_exception_check(error);
6469
}
6570
} else if (TYPE(rb_path) == T_STRING) {
66-
error = git_config_open_ondisk(&config, StringValueCStr(rb_path));
67-
rugged_exception_check(error);
71+
rugged_exception_check(
72+
git_config_open_ondisk(&config, StringValueCStr(rb_path))
73+
);
6874
} else {
6975
rb_raise(rb_eTypeError, "Expecting a filename or an array of filenames");
7076
}

0 commit comments

Comments
 (0)