From: Oliver Ford Date: Wed, 27 Jan 2021 14:39:42 +0000 (+0000) Subject: Add null check to string in archive_pathmatch X-Git-Tag: v3.5.2~29^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ce2234954f20d06c7843db65f288219481bd2b4;p=thirdparty%2Flibarchive.git Add null check to string in archive_pathmatch Adds a null check for the string parameter in archive_pathmatch and archive_pathmatch_w to prevent a null pointer dereference. Reported in github issue #1483. --- diff --git a/libarchive/archive_pathmatch.c b/libarchive/archive_pathmatch.c index 619e2b622..0867a268e 100644 --- a/libarchive/archive_pathmatch.c +++ b/libarchive/archive_pathmatch.c @@ -384,6 +384,8 @@ __archive_pathmatch(const char *p, const char *s, int flags) /* Empty pattern only matches the empty string. */ if (p == NULL || *p == '\0') return (s == NULL || *s == '\0'); + else if (s == NULL) + return (0); /* Leading '^' anchors the start of the pattern. */ if (*p == '^') { @@ -424,6 +426,8 @@ __archive_pathmatch_w(const wchar_t *p, const wchar_t *s, int flags) /* Empty pattern only matches the empty string. */ if (p == NULL || *p == L'\0') return (s == NULL || *s == L'\0'); + else if (s == NULL) + return (0); /* Leading '^' anchors the start of the pattern. */ if (*p == L'^') { diff --git a/libarchive/test/test_archive_pathmatch.c b/libarchive/test/test_archive_pathmatch.c index 21cbdd7e0..0116df028 100644 --- a/libarchive/test/test_archive_pathmatch.c +++ b/libarchive/test/test_archive_pathmatch.c @@ -52,6 +52,10 @@ DEFINE_TEST(test_archive_pathmatch) assertEqualInt(0, archive_pathmatch("a/b/c", "a/b/", 0)); assertEqualInt(0, archive_pathmatch("a/b/c", "a/b", 0)); + /* Null string and non-empty pattern returns false. */ + assertEqualInt(0, archive_pathmatch("a/b/c", NULL, 0)); + assertEqualInt(0, archive_pathmatch_w(L"a/b/c", NULL, 0)); + /* Empty pattern only matches empty string. */ assertEqualInt(1, archive_pathmatch("","", 0)); assertEqualInt(0, archive_pathmatch("","a", 0));