Skip to content

Commit 428103c

Browse files
committed
attr: change validity check for attribute names to use positive logic
Convert 'invalid_attr_name()' to 'attr_name_valid()' and use positive logic for the return value. In addition create a helper function that prints out an error message when an invalid attribute name is used. We could later update the message to exactly spell out what the rules for a good attribute name are, etc. Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6bc2e3f commit 428103c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

attr.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,33 @@ static unsigned hash_name(const char *name, int namelen)
7474
return val;
7575
}
7676

77-
static int invalid_attr_name(const char *name, int namelen)
77+
static int attr_name_valid(const char *name, size_t namelen)
7878
{
7979
/*
8080
* Attribute name cannot begin with '-' and must consist of
8181
* characters from [-A-Za-z0-9_.].
8282
*/
8383
if (namelen <= 0 || *name == '-')
84-
return -1;
84+
return 0;
8585
while (namelen--) {
8686
char ch = *name++;
8787
if (! (ch == '-' || ch == '.' || ch == '_' ||
8888
('0' <= ch && ch <= '9') ||
8989
('a' <= ch && ch <= 'z') ||
9090
('A' <= ch && ch <= 'Z')) )
91-
return -1;
91+
return 0;
9292
}
93-
return 0;
93+
return 1;
94+
}
95+
96+
static void report_invalid_attr(const char *name, size_t len,
97+
const char *src, int lineno)
98+
{
99+
struct strbuf err = STRBUF_INIT;
100+
strbuf_addf(&err, _("%.*s is not a valid attribute name"),
101+
(int) len, name);
102+
fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno);
103+
strbuf_release(&err);
94104
}
95105

96106
static struct git_attr *git_attr_internal(const char *name, int len)
@@ -105,7 +115,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
105115
return a;
106116
}
107117

108-
if (invalid_attr_name(name, len))
118+
if (!attr_name_valid(name, len))
109119
return NULL;
110120

111121
FLEX_ALLOC_MEM(a, name, name, len);
@@ -196,17 +206,15 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
196206
cp++;
197207
len--;
198208
}
199-
if (invalid_attr_name(cp, len)) {
200-
fprintf(stderr,
201-
"%.*s is not a valid attribute name: %s:%d\n",
202-
len, cp, src, lineno);
209+
if (!attr_name_valid(cp, len)) {
210+
report_invalid_attr(cp, len, src, lineno);
203211
return NULL;
204212
}
205213
} else {
206214
/*
207215
* As this function is always called twice, once with
208216
* e == NULL in the first pass and then e != NULL in
209-
* the second pass, no need for invalid_attr_name()
217+
* the second pass, no need for attr_name_valid()
210218
* check here.
211219
*/
212220
if (*cp == '-' || *cp == '!') {
@@ -258,10 +266,8 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
258266
name += strlen(ATTRIBUTE_MACRO_PREFIX);
259267
name += strspn(name, blank);
260268
namelen = strcspn(name, blank);
261-
if (invalid_attr_name(name, namelen)) {
262-
fprintf(stderr,
263-
"%.*s is not a valid attribute name: %s:%d\n",
264-
namelen, name, src, lineno);
269+
if (!attr_name_valid(name, namelen)) {
270+
report_invalid_attr(name, namelen, src, lineno);
265271
goto fail_return;
266272
}
267273
}

0 commit comments

Comments
 (0)