avoid pointer arithmetic involving NULL in FLEX_ALLOC_MEM
[gitweb.git] / ref-filter.c
index 7bef7f8dac644b49a2cb13d8d5154fd563d6e8a0..9ccfc51ceadb79fdf376da79741c9693db8513bb 100644 (file)
@@ -763,6 +763,29 @@ static inline char *copy_advance(char *dst, const char *src)
        return dst;
 }
 
+static const char *strip_ref_components(const char *refname, const char *nr_arg)
+{
+       char *end;
+       long nr = strtol(nr_arg, &end, 10);
+       long remaining = nr;
+       const char *start = refname;
+
+       if (nr < 1 || *end != '\0')
+               die(":strip= requires a positive integer argument");
+
+       while (remaining) {
+               switch (*start++) {
+               case '\0':
+                       die("ref '%s' does not have %ld components to :strip",
+                           refname, nr);
+               case '/':
+                       remaining--;
+                       break;
+               }
+       }
+       return start;
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -909,11 +932,14 @@ static void populate_value(struct ref_array_item *ref)
                formatp = strchr(name, ':');
                if (formatp) {
                        int num_ours, num_theirs;
+                       const char *arg;
 
                        formatp++;
                        if (!strcmp(formatp, "short"))
                                refname = shorten_unambiguous_ref(refname,
                                                      warn_ambiguous_refs);
+                       else if (skip_prefix(formatp, "strip=", &arg))
+                               refname = strip_ref_components(refname, arg);
                        else if (!strcmp(formatp, "track") &&
                                 (starts_with(name, "upstream") ||
                                  starts_with(name, "push"))) {
@@ -1229,10 +1255,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
                                                 const unsigned char *objectname,
                                                 int flag)
 {
-       size_t len = strlen(refname);
-       struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
-       memcpy(ref->refname, refname, len);
-       ref->refname[len] = '\0';
+       struct ref_array_item *ref;
+       FLEX_ALLOC_STR(ref, refname, refname);
        hashcpy(ref->objectname, objectname);
        ref->flag = flag;