Skip to content

Commit 9173912

Browse files
jrngitster
authored andcommitted
init: plug tiny one-time memory leak
The buffer used to construct paths like ".git/objects/info" and ".git/objects/pack" is allocated on the heap and never freed. So free it. While at it, factor out the relevant code into its own function and rename the sha1_dir variable to object_directory (to match the change in everyday usage after the renaming of SHA1_FILE_DIRECTORY in v0.99~603^2~7, 2005). Noticed by valgrind while setting up tests (in test-lib). Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95ae69b commit 9173912

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

builtin/init-db.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,26 @@ static int create_default_files(const char *template_path)
294294
return reinit;
295295
}
296296

297+
static void create_object_directory(void)
298+
{
299+
const char *object_directory = get_object_directory();
300+
int len = strlen(object_directory);
301+
char *path = xmalloc(len + 40);
302+
303+
memcpy(path, object_directory, len);
304+
305+
safe_create_dir(object_directory, 1);
306+
strcpy(path+len, "/pack");
307+
safe_create_dir(path, 1);
308+
strcpy(path+len, "/info");
309+
safe_create_dir(path, 1);
310+
311+
free(path);
312+
}
313+
297314
int init_db(const char *template_dir, unsigned int flags)
298315
{
299-
const char *sha1_dir;
300-
char *path;
301-
int len, reinit;
316+
int reinit;
302317

303318
safe_create_dir(get_git_dir(), 0);
304319

@@ -313,16 +328,7 @@ int init_db(const char *template_dir, unsigned int flags)
313328

314329
reinit = create_default_files(template_dir);
315330

316-
sha1_dir = get_object_directory();
317-
len = strlen(sha1_dir);
318-
path = xmalloc(len + 40);
319-
memcpy(path, sha1_dir, len);
320-
321-
safe_create_dir(sha1_dir, 1);
322-
strcpy(path+len, "/pack");
323-
safe_create_dir(path, 1);
324-
strcpy(path+len, "/info");
325-
safe_create_dir(path, 1);
331+
create_object_directory();
326332

327333
if (shared_repository) {
328334
char buf[10];

0 commit comments

Comments
 (0)