pathspec: copy and free owned memory
[gitweb.git] / pathspec.c
index 1f918cbaec2e7b77fe4d759b92c5346136e04a1b..b8faa8f4653bfb04d4ec56969c1128ae0aee1262 100644 (file)
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
                }
                strbuf_addstr(&sb, match);
                item->original = strbuf_detach(&sb, NULL);
-       } else
-               item->original = elt;
+       } else {
+               item->original = xstrdup(elt);
+       }
        item->len = strlen(item->match);
        item->prefix = prefixlen;
 
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,
                        die("BUG: PATHSPEC_PREFER_CWD requires arguments");
 
                pathspec->items = item = xcalloc(1, sizeof(*item));
-               item->match = prefix;
-               item->original = prefix;
+               item->match = xstrdup(prefix);
+               item->original = xstrdup(prefix);
                item->nowildcard_len = item->len = strlen(prefix);
                item->prefix = item->len;
                pathspec->nr = 1;
@@ -453,13 +454,27 @@ void parse_pathspec(struct pathspec *pathspec,
 
 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
 {
+       int i;
+
        *dst = *src;
        ALLOC_ARRAY(dst->items, dst->nr);
        COPY_ARRAY(dst->items, src->items, dst->nr);
+
+       for (i = 0; i < dst->nr; i++) {
+               dst->items[i].match = xstrdup(src->items[i].match);
+               dst->items[i].original = xstrdup(src->items[i].original);
+       }
 }
 
 void clear_pathspec(struct pathspec *pathspec)
 {
+       int i;
+
+       for (i = 0; i < pathspec->nr; i++) {
+               free(pathspec->items[i].match);
+               free(pathspec->items[i].original);
+       }
        free(pathspec->items);
        pathspec->items = NULL;
+       pathspec->nr = 0;
 }