]> asedeno.scripts.mit.edu Git - git.git/blobdiff - commit.c
Allow fetching references from any namespace
[git.git] / commit.c
index 5b9234e12e8d1ef46d0d53b15cea850dfbde14c2..7d78e786e9a6a6a3566cb7aa6555a815d79f9e4b 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -640,7 +640,9 @@ static char *get_header(const struct commit *commit, const char *key)
                        next = NULL;
                } else
                        next = eol + 1;
-               if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
+               if (eol - line > key_len &&
+                   !strncmp(line, key, key_len) &&
+                   line[key_len] == ' ') {
                        int len = eol - line - key_len;
                        char *ret = xmalloc(len);
                        memcpy(ret, line + key_len + 1, len - 1);
@@ -654,6 +656,7 @@ static char *get_header(const struct commit *commit, const char *key)
 static char *replace_encoding_header(char *buf, const char *encoding)
 {
        char *encoding_header = strstr(buf, "\nencoding ");
+       char *header_end = strstr(buf, "\n\n");
        char *end_of_encoding_header;
        int encoding_header_pos;
        int encoding_header_len;
@@ -661,8 +664,10 @@ static char *replace_encoding_header(char *buf, const char *encoding)
        int need_len;
        int buflen = strlen(buf) + 1;
 
-       if (!encoding_header)
-               return buf; /* should not happen but be defensive */
+       if (!header_end)
+               header_end = buf + buflen;
+       if (!encoding_header || encoding_header >= header_end)
+               return buf;
        encoding_header++;
        end_of_encoding_header = strchr(encoding_header, '\n');
        if (!end_of_encoding_header)
@@ -706,7 +711,7 @@ static char *logmsg_reencode(const struct commit *commit,
        encoding = get_header(commit, "encoding");
        use_encoding = encoding ? encoding : utf8;
        if (!strcmp(use_encoding, output_encoding))
-               out = strdup(commit->buffer);
+               out = xstrdup(commit->buffer);
        else
                out = reencode_string(commit->buffer,
                                      output_encoding, use_encoding);
@@ -717,14 +722,6 @@ static char *logmsg_reencode(const struct commit *commit,
        return out;
 }
 
-static char *xstrndup(const char *text, int len)
-{
-       char *result = xmalloc(len + 1);
-       memcpy(result, text, len);
-       result[len] = '\0';
-       return result;
-}
-
 static void fill_person(struct interp *table, const char *msg, int len)
 {
        int start, end, tz = 0;
@@ -760,7 +757,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
        if (msg + start == ep)
                return;
 
-       table[5].value = xstrndup(msg + start, ep - msg + start);
+       table[5].value = xstrndup(msg + start, ep - (msg + start));
 
        /* parse tz */
        for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
@@ -849,19 +846,23 @@ static long format_commit_message(const struct commit *commit,
        interp_set_entry(table, ITREE_ABBREV,
                        find_unique_abbrev(commit->tree->object.sha1,
                                DEFAULT_ABBREV));
+
+       parents[1] = 0;
        for (i = 0, p = commit->parents;
                        p && i < sizeof(parents) - 1;
                        p = p->next)
-               i += snprintf(parents + i, sizeof(parents) - i - 1, "%s ",
+               i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
                        sha1_to_hex(p->item->object.sha1));
-       interp_set_entry(table, IPARENTS, parents);
+       interp_set_entry(table, IPARENTS, parents + 1);
+
+       parents[1] = 0;
        for (i = 0, p = commit->parents;
                        p && i < sizeof(parents) - 1;
                        p = p->next)
-               i += snprintf(parents + i, sizeof(parents) - i - 1, "%s ",
+               i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
                        find_unique_abbrev(p->item->object.sha1,
                                DEFAULT_ABBREV));
-       interp_set_entry(table, IPARENTS_ABBREV, parents);
+       interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
 
        for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
                int eol;
@@ -884,7 +885,8 @@ static long format_commit_message(const struct commit *commit,
                        fill_person(table + ICOMMITTER_NAME,
                                        msg + i + 10, eol - i - 10);
                else if (!prefixcmp(msg + i, "encoding "))
-                       table[IENCODING].value = xstrndup(msg + i, eol - i);
+                       table[IENCODING].value =
+                               xstrndup(msg + i + 9, eol - i - 9);
                i = eol;
        }
        if (msg[i])