View Issue Details
| ID | Project | Category | View Status | Date Submitted | Last Update |
|---|---|---|---|---|---|
| 0008889 | GNUnet | other | public | 2024-06-03 10:49 | 2026-11-18 11:21 |
| Reporter | ch3 | Assigned To | schanzen | ||
| Priority | low | Severity | minor | Reproducibility | always |
| Status | resolved | Resolution | fixed | ||
| Product Version | Git master | ||||
| Target Version | 0.30.0 | Fixed in Version | 0.30.0 | ||
| Summary | 0008889: DISK_glob() not matching * at end | ||||
| Description | GNUNET_DISK_glob() (in file src/lib/util/disk.c) does falsly not match files when the * is the last character in the pattern. Unfortunately it's quite some while since I discovered it and I'm not 100% sure about the details. I had a pattern 'test*' and wanted it to match 'test0', 'test1', ... but it matched none of those. (If it matched 'test' I cannot remember.) It works well for files 'test1.ext', 'test2.ext', ... with the pattern 'test*.ext'. | ||||
| Steps To Reproduce | Create a file 'test0', ('test1', 'test2', ...) and call GNUNET_DISK_glob() with 'test*'. It should match but does not. | ||||
| Additional Information | I tried to debug the problem and came to the assumption that 'return false' should be 'return true' in glob_match() disk.c:949. I did intend to provide a unit test, but was unsure whether gnunet uses these and if yes, how. | ||||
| Tags | No tags attached. | ||||
| Attached Files | 0004-util-let-a-final-in-GNUNET_DISK_glob-patterns-match.patch (3,489 bytes)
From 4023f4a23c2b341b35fa0f25186122cfbe28cf57 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 11:17:21 +0100
Subject: [PATCH 4/7] util: let a final '*' in GNUNET_DISK_glob patterns match
glob_match gave up once the '*' would have to swallow the last character,
so "test*" matched nothing, and it then looked one byte past the name,
which made "a*c" match "a" if a longer name had been scanned before.
Fixes #8889
Assisted by LLM
---
src/lib/util/disk.c | 5 ++-
src/lib/util/test_disk.c | 70 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 3 deletions(-)
diff --git a/src/lib/util/disk.c b/src/lib/util/disk.c
index ca924ff6c..4a6ce84bc 100644
--- a/src/lib/util/disk.c
+++ b/src/lib/util/disk.c
@@ -994,13 +994,12 @@ glob_match (const char *pattern, const char *str)
}
else
{
- if (NULL == str_bt)
+ if ( (NULL == str_bt) ||
+ ('\0' == *str_bt) )
return false;
/* Backtrack to match one more
character as part of the asterisk. */
str_pos = str_bt + 1;
- if ('\0' == *str_pos)
- return false;
pat_pos = pat_bt;
}
}
diff --git a/src/lib/util/test_disk.c b/src/lib/util/test_disk.c
index 61932d27a..ae5abae9e 100644
--- a/src/lib/util/test_disk.c
+++ b/src/lib/util/test_disk.c
@@ -276,6 +276,75 @@ testDirMani ()
}
+/**
+ * Check how many files in "test-glob" match a pattern.
+ */
+static int
+check_glob (const char *pattern,
+ int want)
+{
+ int found = 0;
+ int ret;
+
+ ret = GNUNET_DISK_glob (pattern,
+ &iter_callback,
+ &found);
+ if ( (want != ret) ||
+ (want != found) )
+ {
+ fprintf (stderr,
+ "Glob `%s' matched %d files (%d callbacks), wanted %d\n",
+ pattern,
+ ret,
+ found,
+ want);
+ return 1;
+ }
+ return 0;
+}
+
+
+static int
+testGlob ()
+{
+ static const char *files[] = {
+ "test0", "test1", "a", "abc", "a.conf", NULL
+ };
+ int ret = 0;
+
+ if (GNUNET_OK != GNUNET_DISK_directory_create ("test-glob"))
+ {
+ GNUNET_break (0);
+ return 1;
+ }
+ for (unsigned int i = 0; NULL != files[i]; i++)
+ {
+ char *fn;
+
+ GNUNET_asprintf (&fn,
+ "test-glob" DIR_SEPARATOR_STR "%s",
+ files[i]);
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_DISK_fn_write (fn,
+ "x",
+ 1,
+ GNUNET_DISK_PERM_USER_READ
+ | GNUNET_DISK_PERM_USER_WRITE));
+ GNUNET_free (fn);
+ }
+ ret += check_glob ("test-glob/test*", 2);
+ ret += check_glob ("test-glob/*", 5);
+ ret += check_glob ("test-glob/*.conf", 1);
+ ret += check_glob ("test-glob/a*", 3);
+ ret += check_glob ("test-glob/abc*", 1);
+ ret += check_glob ("test-glob/a*c", 1);
+ ret += check_glob ("test-glob/*b*", 1);
+ ret += check_glob ("test-glob/x*", 0);
+ GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove ("test-glob"));
+ return ret;
+}
+
+
int
main (int argc, char *argv[])
{
@@ -289,6 +358,7 @@ main (int argc, char *argv[])
failureCount += testCanonicalize ();
failureCount += testChangeOwner ();
failureCount += testDirMani ();
+ failureCount += testGlob ();
if (0 != failureCount)
{
fprintf (stderr,
--
2.47.3
| ||||
| Unit test sind gewollt, es gibt viele Beispiele vor allem in utils. Ich schaue mir das 'wie' an und kümmere mich. | |
| I ran into this as well: configuration.c's @inline-matching@ uses GNUNET_DISK_glob, so "@inline-matching@ conf.d/*" includes nothing. In glob_match (src/lib/util/disk.c), when the pattern and the name disagree, the code backtracks with str_pos = str_bt + 1; if ('\0' == *str_pos) return false; That test comes one character too late. It gives up as soon as the '*' would have to swallow the last character of the name, so a '*' at the end of a pattern can never consume anything ("test*" only matches "test"). And when str_bt is already at the end of the name (pattern "a*c", name "a"), str_pos points one byte past the terminating NUL. GNUNET_DISK_directory_scan reuses its name buffer, so that byte is left over from a longer name scanned before, and "a*c" then matches "a" if, say, "abc" came first. Turning the 'return false' into 'return true', as suggested in the description, would make a pattern such as "*.conf" match every name. The right test is whether anything is left for the '*' to consume: if ( (NULL == str_bt) || ('\0' == *str_bt) ) return false; str_pos = str_bt + 1; The attached patch does that and adds a GNUNET_DISK_glob test to test_disk.c: the files test0, test1, a, abc and a.conf against "test*", "*", "*.conf", "a*", "abc*", "a*c", "*b*" and "x*". Without the fix five of these fail, among them "a*c" matching two files. It is patch 4 of the series in 0012167, and it also applies to master on its own. | |
| Applied in d82f1e1efb4b29a2a113fd67932913f644cd72d8 | |
| Date Modified | Username | Field | Change |
|---|---|---|---|
| 2024-06-03 10:49 | ch3 | New Issue | |
| 2024-06-03 10:52 | ch3 | Description Updated | |
| 2024-06-03 11:48 | ch3 | Assigned To | => ch3 |
| 2024-06-03 11:48 | ch3 | Status | new => assigned |
| 2024-06-03 11:48 | ch3 | Note Added: 0022497 | |
| 2026-11-18 10:23 | bot-cg | File Added: 0004-util-let-a-final-in-GNUNET_DISK_glob-patterns-match.patch | |
| 2026-11-18 10:23 | bot-cg | Note Added: 0030563 | |
| 2026-11-18 11:07 | schanzen | Assigned To | ch3 => schanzen |
| 2026-11-18 11:07 | schanzen | Target Version | => 0.30.0 |
| 2026-11-18 11:21 | schanzen | Status | assigned => resolved |
| 2026-11-18 11:21 | schanzen | Resolution | open => fixed |
| 2026-11-18 11:21 | schanzen | Fixed in Version | => 0.30.0 |
| 2026-11-18 11:21 | schanzen | Note Added: 0030566 |