Code

Use xmemdupz() in many places.
[git.git] / sha1_file.c
index aea10961fdd9ba6e14514701a49bd69cb594a695..385c5d891af9b7c79dd93e0e6777b0975ff5dbef 100644 (file)
@@ -14,7 +14,6 @@
 #include "tag.h"
 #include "tree.h"
 #include "refs.h"
-#include "strbuf.h"
 
 #ifndef O_NOATIME
 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -1492,11 +1491,8 @@ found_cache_entry:
                ent->lru.next->prev = ent->lru.prev;
                ent->lru.prev->next = ent->lru.next;
                delta_base_cached -= ent->size;
-       }
-       else {
-               ret = xmalloc(ent->size + 1);
-               memcpy(ret, ent->data, ent->size);
-               ((char *)ret)[ent->size] = 0;
+       } else {
+               ret = xmemdupz(ent->data, ent->size);
        }
        *type = ent->type;
        *base_size = ent->size;
@@ -1873,12 +1869,9 @@ void *read_sha1_file(const unsigned char *sha1, enum object_type *type,
 
        co = find_cached_object(sha1);
        if (co) {
-               buf = xmalloc(co->size + 1);
-               memcpy(buf, co->buf, co->size);
-               ((char*)buf)[co->size] = 0;
                *type = co->type;
                *size = co->size;
-               return buf;
+               return xmemdupz(co->buf, co->size);
        }
 
        buf = read_packed_sha1(sha1, type, size);
@@ -2343,12 +2336,12 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
         * Convert blobs to git internal format
         */
        if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
-               unsigned long nsize = size;
-               char *nbuf = convert_to_git(path, buf, &nsize);
-               if (nbuf) {
+               struct strbuf nbuf;
+               strbuf_init(&nbuf, 0);
+               if (convert_to_git(path, buf, size, &nbuf)) {
                        munmap(buf, size);
-                       size = nsize;
-                       buf = nbuf;
+                       size = nbuf.len;
+                       buf = nbuf.buf;
                        re_allocated = 1;
                }
        }