summaryrefslogtreecommitdiffstats
path: root/src/glob.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 01:05:08 +0000
committerdev <dev@example>2026-11-14 01:05:08 +0000
commit312ddd4936f09ae27a180ca495cacf2c06faf33e (patch)
tree2fa46ab568ad55f7e8fb844c3447b1d296b2e340 /src/glob.c
parente737f3d560de2b8172e6dd83901bc1359283303c (diff)
downloadaccess-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.tar.gz
access-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.zip
Comments: cut to what the code cannot say
The comments had grown into essays. A file header explaining why the file exists, then a paragraph above each function restating what the next eight lines do, then a paragraph above that one explaining why that matters. The reader gets no further than the first paragraph, and the second is the one that goes stale: it describes code that has since changed, and nothing fails when it does. Cut by about half. What is left is the three things a comment can do that code cannot: say why the constraint exists (no regex because a hook must not depend on what might be missing at 3am), say what is not obvious from the shape (a bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the shell), and say where it hurts (the NULL in an execvp array, the mode and owner that have to survive an atomic rewrite, the child that must not keep the hook's stdin). Block comments became // lines, which is also what the other C trees here use, and the file headers went to two or three lines. Section banners stayed: they are navigation, not prose. Behaviour is unchanged and the suites prove it: 171 direct checks, 162 behavioural, clean under -Werror and under ASan/UBSan.
Diffstat (limited to 'src/glob.c')
-rw-r--r--src/glob.c26
1 files changed, 11 insertions, 15 deletions
diff --git a/src/glob.c b/src/glob.c
index 7c6b969..08a829d 100644
--- a/src/glob.c
+++ b/src/glob.c
@@ -1,12 +1,7 @@
-/* glob.c - the only pattern matching in this program.
- *
- * Three metacharacters, no library. See the note in authz.h for why this is
- * not fnmatch(3) or regex(3). */
#include "authz.h"
-/* Length of the class starting at p ('[' ... ']'), or 0 when it is not closed
- * and therefore means a literal '['. A ']' first - or first after a negation -
- * is a member, like everywhere else. */
+// Length of the class at p, or 0 when it is unclosed and so means a literal '['.
+// A ']' first, or first after a negation, is a member.
static int class_len(const char *p)
{
const char *c = p + 1;
@@ -20,11 +15,11 @@ static int class_len(const char *p)
return (*c == ']') ? (int)(c - p) + 1 : 0;
}
-/* text is a complete class of len bytes, including the brackets. */
+// text is a complete class of len bytes, brackets included.
static int class_match(const char *text, int len, char c)
{
const char *p = text + 1;
- const char *end = text + len - 1; /* the ']' */
+ const char *end = text + len - 1; // the ']'
int negate = 0;
int hit = 0;
@@ -34,7 +29,7 @@ static int class_match(const char *text, int len, char c)
}
while (p < end) {
char lo = *p++;
- if (p + 1 < end && *p == '-') { /* 'a-z'; a trailing '-' is literal */
+ if (p + 1 < end && *p == '-') { // 'a-z'; a trailing '-' is literal
char hi = p[1];
p += 2;
if (c >= lo && c <= hi)
@@ -50,8 +45,8 @@ int glob_match(const char *pattern, const char *s)
{
const char *p = pattern;
const char *t = s;
- const char *star = NULL; /* in the pattern, just after the last '*' */
- const char *back = NULL; /* in the text, where that '*' may yet start */
+ const char *star = NULL; // in the pattern, just after the last '*'
+ const char *back = NULL; // in the text, where that '*' may yet start
int clen;
if (pattern == NULL || s == NULL)
@@ -68,9 +63,10 @@ int glob_match(const char *pattern, const char *s)
clen = (*p == '[') ? class_len(p) : 0;
if (clen > 0) {
if (!class_match(p, clen, *t)) {
- /* The class was well formed and said no. Backtrack to the
- * last '*' rather than comparing '[' against the text: a
- * negated class must be able to fail. */
+ // A well-formed class that says no has to backtrack rather
+ // than compare '[' literally: a negated class must be able to
+ // fail, and falling through here would match it against a
+ // pattern that only looked like a class.
if (star == NULL)
return 0;
p = star;