]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
archive_match: Turn counter into flag (#2154)
authorTobias Stoeckmann <stoeckmann@users.noreply.github.com>
Mon, 29 Apr 2024 20:05:44 +0000 (22:05 +0200)
committerGitHub <noreply@github.com>
Mon, 29 Apr 2024 20:05:44 +0000 (22:05 +0200)
When working with matches, the code does not care about the actual
amount of times when it matched, but just if it matched so far at least
once or never.

Turning the counter into a boolean flag has the advantage that even
insanely huge archives will never lead to integer overflow here.

libarchive/archive_match.c

index b108ff0a7347dbb2a1a8bf4ffa35136d3e6d677d..3ab8eda360386b2df9bc0c4ae3a3d5da0f419b23 100644 (file)
@@ -46,7 +46,7 @@
 
 struct match {
        struct match            *next;
-       int                      matches;
+       int                      matched;
        struct archive_mstring   pattern;
 };
 
@@ -725,12 +725,12 @@ path_excluded(struct archive_match *a, int mbs, const void *pathname)
        matched = NULL;
        for (match = a->inclusions.first; match != NULL;
            match = match->next){
-               if (match->matches == 0 &&
+               if (!match->matched &&
                    (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
                        if (r < 0)
                                return (r);
                        a->inclusions.unmatched_count--;
-                       match->matches++;
+                       match->matched = 1;
                        matched = match;
                }
        }
@@ -753,11 +753,10 @@ path_excluded(struct archive_match *a, int mbs, const void *pathname)
        for (match = a->inclusions.first; match != NULL;
            match = match->next){
                /* We looked at previously-unmatched inclusions already. */
-               if (match->matches > 0 &&
+               if (match->matched &&
                    (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
                        if (r < 0)
                                return (r);
-                       match->matches++;
                        return (0);
                }
        }
@@ -880,7 +879,7 @@ match_list_unmatched_inclusions_next(struct archive_match *a,
        for (m = list->unmatched_next; m != NULL; m = m->next) {
                int r;
 
-               if (m->matches)
+               if (m->matched)
                        continue;
                if (mbs) {
                        const char *p;
@@ -1794,7 +1793,7 @@ match_owner_name_mbs(struct archive_match *a, struct match_list *list,
                    < 0 && errno == ENOMEM)
                        return (error_nomem(a));
                if (p != NULL && strcmp(p, name) == 0) {
-                       m->matches++;
+                       m->matched = 1;
                        return (1);
                }
        }
@@ -1815,7 +1814,7 @@ match_owner_name_wcs(struct archive_match *a, struct match_list *list,
                    < 0 && errno == ENOMEM)
                        return (error_nomem(a));
                if (p != NULL && wcscmp(p, name) == 0) {
-                       m->matches++;
+                       m->matched = 1;
                        return (1);
                }
        }