@matheusbsbs / matheus-bagliano / commits / 7ba502c47b
Fork 0

pack-objects.c: fix some global variable abuse and memory leaks

To keep things well layered, sha1close() now returns the file descriptor when it doesn't close the file. An ugly cast was added to the return of write_idx_file() to avoid a warning. A proper fix will come separately. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Nicolas Pitre committed Oct 16, 2007 at 21:55 UTC 7ba502c47bda21d060844863991083f4c319d436
2 files changed +29 -23
builtin-pack-objects.c
+15 -14
@@ -65,8 +65,6 @@ static int no_reuse_delta, no_reuse_object, keep_unreachable;
65 static int local;
66 static int incremental;
67 static int allow_ofs_delta;
68 -static const char *pack_tmp_name, *idx_tmp_name;
69 -static char tmpname[PATH_MAX];
68 static const char *base_name;
69 static int progress = 1;
70 static int window = 10;
@@ -587,12 +585,6 @@ static off_t write_one(struct sha1file *f,
585 return offset + size;
586 }
587
590 -static int open_object_dir_tmp(const char *path)
591 -{
592 - snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
593 - return xmkstemp(tmpname);
594 -}
595 -
588 /* forward declaration for write_pack_file */
589 static int adjust_perm(const char *path, mode_t mode);
590
@@ -611,11 +603,16 @@ static void write_pack_file(void)
603
604 do {
605 unsigned char sha1[20];
606 + char *pack_tmp_name = NULL;
607
608 if (pack_to_stdout) {
609 f = sha1fd(1, "<stdout>");
610 } else {
618 - int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
611 + char tmpname[PATH_MAX];
612 + int fd;
613 + snprintf(tmpname, sizeof(tmpname),
614 + "%s/tmp_pack_XXXXXX", get_object_directory());
615 + fd = xmkstemp(tmpname);
616 pack_tmp_name = xstrdup(tmpname);
617 f = sha1fd(fd, pack_tmp_name);
618 }
@@ -643,19 +640,21 @@ static void write_pack_file(void)
640 if (pack_to_stdout || nr_written == nr_remaining) {
641 sha1close(f, sha1, 1);
642 } else {
646 - sha1close(f, sha1, 0);
647 - fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
648 - close(f->fd);
643 + int fd = sha1close(f, NULL, 0);
644 + fixup_pack_header_footer(fd, sha1, pack_tmp_name, nr_written);
645 + close(fd);
646 }
647
648 if (!pack_to_stdout) {
649 mode_t mode = umask(0);
650 + char *idx_tmp_name, tmpname[PATH_MAX];
651
652 umask(mode);
653 mode = 0444 & ~mode;
654
657 - idx_tmp_name = write_idx_file(NULL,
658 - (struct pack_idx_entry **) written_list, nr_written, sha1);
655 + idx_tmp_name = (char *) write_idx_file(NULL,
656 + (struct pack_idx_entry **) written_list,
657 + nr_written, sha1);
658 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
659 base_name, sha1_to_hex(sha1));
660 if (adjust_perm(pack_tmp_name, mode))
@@ -672,6 +671,8 @@ static void write_pack_file(void)
671 if (rename(idx_tmp_name, tmpname))
672 die("unable to rename temporary index file: %s",
673 strerror(errno));
674 + free(idx_tmp_name);
675 + free(pack_tmp_name);
676 puts(sha1_to_hex(sha1));
677 }
678
csum-file.c
+14 -9
@@ -31,22 +31,27 @@ static void sha1flush(struct sha1file *f, unsigned int count)
31
32 int sha1close(struct sha1file *f, unsigned char *result, int final)
33 {
34 + int fd;
35 unsigned offset = f->offset;
36 if (offset) {
37 SHA1_Update(&f->ctx, f->buffer, offset);
38 sha1flush(f, offset);
39 f->offset = 0;
40 }
40 - if (!final)
41 - return 0; /* only want to flush (no checksum write, no close) */
42 - SHA1_Final(f->buffer, &f->ctx);
43 - if (result)
44 - hashcpy(result, f->buffer);
45 - sha1flush(f, 20);
46 - if (close(f->fd))
47 - die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
41 + if (final) {
42 + /* write checksum and close fd */
43 + SHA1_Final(f->buffer, &f->ctx);
44 + if (result)
45 + hashcpy(result, f->buffer);
46 + sha1flush(f, 20);
47 + if (close(f->fd))
48 + die("%s: sha1 file error on close (%s)",
49 + f->name, strerror(errno));
50 + fd = 0;
51 + } else
52 + fd = f->fd;
53 free(f);
49 - return 0;
54 + return fd;
55 }
56
57 int sha1write(struct sha1file *f, void *buf, unsigned int count)