@matheusbsbs / matheus-bagliano / commits / 27d69a465d
Fork 0

refactor pack structure allocation

New pack structures are currently allocated in 2 different places and all members have to be initialized explicitly. This is prone to errors leading to segmentation faults as found by Teemu Likonen. Let's have a common place where this structure is allocated, and have all members explicitly initialized to zero. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Pitre committed Jun 24, 2008 at 18:58 UTC 27d69a465d14b516d6a1ba287edc83606f23a5a5
1 file changed +15 -18
sha1_file.c
+15 -18
@@ -792,18 +792,28 @@ unsigned char* use_pack(struct packed_git *p,
792 return win->base + offset;
793 }
794
795 +static struct packed_git *alloc_packed_git(int extra)
796 +{
797 + struct packed_git *p = xmalloc(sizeof(*p) + extra);
798 + memset(p, 0, sizeof(*p));
799 + p->pack_fd = -1;
800 + return p;
801 +}
802 +
803 struct packed_git *add_packed_git(const char *path, int path_len, int local)
804 {
805 struct stat st;
798 - struct packed_git *p = xmalloc(sizeof(*p) + path_len + 2);
806 + struct packed_git *p = alloc_packed_git(path_len + 2);
807
808 /*
809 * Make sure a corresponding .pack file exists and that
810 * the index looks sane.
811 */
812 path_len -= strlen(".idx");
805 - if (path_len < 1)
813 + if (path_len < 1) {
814 + free(p);
815 return NULL;
816 + }
817 memcpy(p->pack_name, path, path_len);
818 strcpy(p->pack_name + path_len, ".pack");
819 if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
@@ -814,16 +824,7 @@ struct packed_git *add_packed_git(const char *path, int path_len, int local)
824 /* ok, it looks sane as far as we can check without
825 * actually mapping the pack file.
826 */
817 - p->index_version = 0;
818 - p->index_data = NULL;
819 - p->index_size = 0;
820 - p->num_objects = 0;
821 - p->num_bad_objects = 0;
822 - p->bad_object_sha1 = NULL;
827 p->pack_size = st.st_size;
824 - p->next = NULL;
825 - p->windows = NULL;
826 - p->pack_fd = -1;
828 p->pack_local = local;
829 p->mtime = st.st_mtime;
830 if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
@@ -835,19 +836,15 @@ struct packed_git *parse_pack_index(unsigned char *sha1)
836 {
837 const char *idx_path = sha1_pack_index_name(sha1);
838 const char *path = sha1_pack_name(sha1);
838 - struct packed_git *p = xmalloc(sizeof(*p) + strlen(path) + 2);
839 + struct packed_git *p = alloc_packed_git(strlen(path) + 1);
840
841 + strcpy(p->pack_name, path);
842 + hashcpy(p->sha1, sha1);
843 if (check_packed_git_idx(idx_path, p)) {
844 free(p);
845 return NULL;
846 }
847
845 - strcpy(p->pack_name, path);
846 - p->pack_size = 0;
847 - p->next = NULL;
848 - p->windows = NULL;
849 - p->pack_fd = -1;
850 - hashcpy(p->sha1, sha1);
848 return p;
849 }
850