Skip to content

Commit 8cc3299

Browse files
René Scharfegitster
authored andcommitted
Change NUL char handling of isspecial()
Replace isspecial() by the new macro is_glob_special(), which is more, well, specialized. The former included the NUL char in its character class, while the letter only included characters that are special to file name globbing. The new name contains underscores because they enhance readability considerably now that it's made up of three words. Renaming the function is necessary to document its changed scope. The call sites of isspecial() are updated to check explicitly for NUL. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c841aa8 commit 8cc3299

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

ctype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ enum {
99
S = GIT_SPACE,
1010
A = GIT_ALPHA,
1111
D = GIT_DIGIT,
12-
G = GIT_SPECIAL, /* \0, *, ?, [, \\ */
12+
G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
1313
};
1414

1515
unsigned char sane_ctype[256] = {
16-
G, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
16+
0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
1717
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16.. 31 */
1818
S, 0, 0, 0, 0, 0, 0, 0, 0, 0, G, 0, 0, 0, 0, 0, /* 32.. 47 */
1919
D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */

dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int match_one(const char *match, const char *name, int namelen)
7575
for (;;) {
7676
unsigned char c1 = *match;
7777
unsigned char c2 = *name;
78-
if (isspecial(c1))
78+
if (c1 == '\0' || is_glob_special(c1))
7979
break;
8080
if (c1 != c2)
8181
return 0;
@@ -680,7 +680,7 @@ static int simple_length(const char *match)
680680
for (;;) {
681681
unsigned char c = *match++;
682682
len++;
683-
if (isspecial(c))
683+
if (c == '\0' || is_glob_special(c))
684684
return len;
685685
}
686686
}

git-compat-util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@ extern unsigned char sane_ctype[256];
327327
#define GIT_SPACE 0x01
328328
#define GIT_DIGIT 0x02
329329
#define GIT_ALPHA 0x04
330-
#define GIT_SPECIAL 0x08
330+
#define GIT_GLOB_SPECIAL 0x08
331331
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
332332
#define isspace(x) sane_istest(x,GIT_SPACE)
333333
#define isdigit(x) sane_istest(x,GIT_DIGIT)
334334
#define isalpha(x) sane_istest(x,GIT_ALPHA)
335335
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
336-
#define isspecial(x) sane_istest(x,GIT_SPECIAL)
336+
#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
337337
#define tolower(x) sane_case((unsigned char)(x), 0x20)
338338
#define toupper(x) sane_case((unsigned char)(x), 0)
339339

grep.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat,
3030

3131
static int isregexspecial(int c)
3232
{
33-
return isspecial(c) || c == '$' || c == '(' || c == ')' || c == '+' ||
34-
c == '.' || c == '^' || c == '{' || c == '|';
33+
return c == '\0' || is_glob_special(c) ||
34+
c == '$' || c == '(' || c == ')' || c == '+' ||
35+
c == '.' || c == '^' || c == '{' || c == '|';
3536
}
3637

3738
static int is_fixed(const char *s)

test-ctype.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ static int test_isalnum(int c)
2121
return isalnum(c);
2222
}
2323

24+
static int test_is_glob_special(int c)
25+
{
26+
return is_glob_special(c);
27+
}
28+
2429
#define DIGIT "0123456789"
2530
#define LOWER "abcdefghijklmnopqrstuvwxyz"
2631
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -34,6 +39,7 @@ static const struct ctype_class {
3439
{ "isspace", test_isspace, " \n\r\t" },
3540
{ "isalpha", test_isalpha, LOWER UPPER },
3641
{ "isalnum", test_isalnum, LOWER UPPER DIGIT },
42+
{ "is_glob_special", test_is_glob_special, "*?[\\" },
3743
{ NULL }
3844
};
3945

0 commit comments

Comments
 (0)