Code

config.c:store_write_pair(): don't read the byte before a malloc'd buffer.
[git.git] / builtin-for-each-ref.c
index b11ca928d6ac353de888f0e80d24578c5803d769..0327f403060f80648aeed5a00f5aa1f06f403325 100644 (file)
@@ -1,3 +1,4 @@
+#include "builtin.h"
 #include "cache.h"
 #include "refs.h"
 #include "object.h"
@@ -42,7 +43,7 @@ static struct {
        { "objectsize", FIELD_ULONG },
        { "objectname" },
        { "tree" },
-       { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
+       { "parent" },
        { "numparent", FIELD_ULONG },
        { "object" },
        { "type" },
@@ -261,24 +262,26 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
                }
                if (!strcmp(name, "numparent")) {
                        char *s = xmalloc(40);
+                       v->ul = num_parents(commit);
                        sprintf(s, "%lu", v->ul);
                        v->s = s;
-                       v->ul = num_parents(commit);
                }
                else if (!strcmp(name, "parent")) {
                        int num = num_parents(commit);
                        int i;
                        struct commit_list *parents;
-                       char *s = xmalloc(42 * num);
+                       char *s = xmalloc(41 * num + 1);
                        v->s = s;
                        for (i = 0, parents = commit->parents;
                             parents;
-                            parents = parents->next, i = i + 42) {
+                            parents = parents->next, i = i + 41) {
                                struct commit *parent = parents->item;
                                strcpy(s+i, sha1_to_hex(parent->object.sha1));
                                if (parents->next)
                                        s[i+40] = ' ';
                        }
+                       if (!i)
+                               *s = '\0';
                }
        }
 }
@@ -294,14 +297,14 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
                if (!eol)
                        return "";
                eol++;
-               if (eol[1] == '\n')
+               if (*eol == '\n')
                        return ""; /* end of header */
                buf = eol;
        }
        return "";
 }
 
-static char *copy_line(const char *buf)
+static const char *copy_line(const char *buf)
 {
        const char *eol = strchr(buf, '\n');
        char *line;
@@ -315,7 +318,7 @@ static char *copy_line(const char *buf)
        return line;
 }
 
-static char *copy_name(const char *buf)
+static const char *copy_name(const char *buf)
 {
        const char *eol = strchr(buf, '\n');
        const char *eoname = strstr(buf, " <");
@@ -330,7 +333,7 @@ static char *copy_name(const char *buf)
        return line;
 }
 
-static char *copy_email(const char *buf)
+static const char *copy_email(const char *buf)
 {
        const char *email = strchr(buf, '<');
        const char *eoemail = strchr(email, '>');
@@ -796,7 +799,7 @@ static struct ref_sort *default_sort(void)
        return sort;
 }
 
-int cmd_for_each_ref(int ac, const char **av, char *prefix)
+int cmd_for_each_ref(int ac, const char **av, const char *prefix)
 {
        int i, num_refs;
        const char *format = NULL;