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 <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Oct 3, 2010 at 23:34 UTC
9173912be36c4f967f42f176be767fc5ba3c4077
1 file changed
+19
-13
builtin/init-db.c
+19
-13
@@ -294,11 +294,26 @@ static int create_default_files(const char *template_path)
294
return reinit;
295
}
296
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
+
314
int init_db(const char *template_dir, unsigned int flags)
315
{
299
- const char *sha1_dir;
300
- char *path;
301
- int len, reinit;
316
+ int reinit;
317
318
safe_create_dir(get_git_dir(), 0);
319
@@ -313,16 +328,7 @@ int init_db(const char *template_dir, unsigned int flags)
328
329
reinit = create_default_files(template_dir);
330
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();
332
333
if (shared_repository) {
334
char buf[10];