Commit 0c8858de by Russell Belfer

Fix valgrind issues and leaks

This fixes up a number of problems flagged by valgrind and also
cleans up the internal `git_submodule` allocation handling
overall with a simpler model.
parent aa13bf05
...@@ -144,31 +144,40 @@ int git_buf_puts(git_buf *buf, const char *string) ...@@ -144,31 +144,40 @@ int git_buf_puts(git_buf *buf, const char *string)
int git_buf_puts_escaped( int git_buf_puts_escaped(
git_buf *buf, const char *string, const char *esc_chars, const char *esc_with) git_buf *buf, const char *string, const char *esc_chars, const char *esc_with)
{ {
const char *scan = string; const char *scan;
size_t total = 0, esc_with_len = strlen(esc_with); size_t total = 0, esc_len = strlen(esc_with), count;
while (*scan) { if (!string)
size_t count = strcspn(scan, esc_chars); return 0;
total += count + 1 + esc_with_len;
scan += count + 1; for (scan = string; *scan; ) {
/* count run of non-escaped characters */
count = strcspn(scan, esc_chars);
total += count;
scan += count;
/* count run of escaped characters */
count = strspn(scan, esc_chars);
total += count * (esc_len + 1);
scan += count;
} }
ENSURE_SIZE(buf, buf->size + total + 1); ENSURE_SIZE(buf, buf->size + total + 1);
for (scan = string; *scan; ) { for (scan = string; *scan; ) {
size_t count = strcspn(scan, esc_chars); count = strcspn(scan, esc_chars);
memmove(buf->ptr + buf->size, scan, count); memmove(buf->ptr + buf->size, scan, count);
scan += count; scan += count;
buf->size += count; buf->size += count;
if (*scan) { for (count = strspn(scan, esc_chars); count > 0; --count) {
memmove(buf->ptr + buf->size, esc_with, esc_with_len); /* copy escape sequence */
buf->size += esc_with_len; memmove(buf->ptr + buf->size, esc_with, esc_len);
buf->size += esc_len;
memmove(buf->ptr + buf->size, scan, 1); /* copy character to be escaped */
scan += 1; buf->ptr[buf->size] = *scan;
buf->size += 1; buf->size++;
scan++;
} }
} }
......
...@@ -195,7 +195,7 @@ static int file_foreach( ...@@ -195,7 +195,7 @@ static int file_foreach(
void *data) void *data)
{ {
diskfile_backend *b = (diskfile_backend *)backend; diskfile_backend *b = (diskfile_backend *)backend;
cvar_t *var; cvar_t *var, *next_var;
const char *key; const char *key;
regex_t regex; regex_t regex;
int result = 0; int result = 0;
...@@ -212,7 +212,9 @@ static int file_foreach( ...@@ -212,7 +212,9 @@ static int file_foreach(
} }
git_strmap_foreach(b->values, key, var, git_strmap_foreach(b->values, key, var,
for (; var != NULL; var = CVAR_LIST_NEXT(var)) { for (; var != NULL; var = next_var) {
next_var = CVAR_LIST_NEXT(var);
/* skip non-matching keys if regexp was provided */ /* skip non-matching keys if regexp was provided */
if (regexp && regexec(&regex, key, 0, NULL, 0) != 0) if (regexp && regexec(&regex, key, 0, NULL, 0) != 0)
continue; continue;
......
...@@ -700,6 +700,7 @@ int git_futils_cp_r( ...@@ -700,6 +700,7 @@ int git_futils_cp_r(
error = _cp_r_callback(&info, &path); error = _cp_r_callback(&info, &path);
git_buf_free(&path); git_buf_free(&path);
git_buf_free(&info.to);
return error; return error;
} }
...@@ -253,4 +253,5 @@ void test_submodule_modify__edit_and_save(void) ...@@ -253,4 +253,5 @@ void test_submodule_modify__edit_and_save(void)
(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm2)); (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm2));
git_repository_free(r2); git_repository_free(r2);
git__free(old_url);
} }
{
libgit2-giterr-set-buffer
Memcheck:Leak
...
fun:git__realloc
fun:git_buf_try_grow
fun:git_buf_grow
fun:git_buf_vprintf
fun:giterr_set
}
{
mac-setenv-leak-1
Memcheck:Leak
fun:malloc_zone_malloc
fun:__setenv
fun:setenv
}
{
mac-setenv-leak-2
Memcheck:Leak
fun:malloc_zone_malloc
fun:malloc_set_zone_name
...
fun:init__zone0
fun:setenv
}
{
mac-dyld-initializer-leak
Memcheck:Leak
fun:malloc
...
fun:dyld_register_image_state_change_handler
fun:_dyld_initializer
}
{
mac-tz-leak-1
Memcheck:Leak
...
fun:token_table_add
fun:notify_register_check
fun:notify_register_tz
}
{
mac-tz-leak-2
Memcheck:Leak
fun:malloc
fun:tzload
}
{
mac-tz-leak-3
Memcheck:Leak
fun:malloc
fun:tzsetwall_basic
}
{
mac-tz-leak-4
Memcheck:Leak
fun:malloc
fun:gmtsub
}
{
mac-system-init-leak-1
Memcheck:Leak
...
fun:_libxpc_initializer
fun:libSystem_initializer
}
{
mac-system-init-leak-2
Memcheck:Leak
...
fun:__keymgr_initializer
fun:libSystem_initializer
}
{
mac-puts-leak
Memcheck:Leak
fun:malloc
fun:__smakebuf
...
fun:puts
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment