Skip to content

Commit 13b9a24

Browse files
peffgitster
authored andcommitted
ident: reject all-crud ident name
An ident name consisting of only "crud" characters (like whitespace or punctuation) is effectively the same as an empty one, because our strbuf_addstr_without_crud() will remove those characters. We reject an empty name when formatting a strict ident, but don't notice an all-crud one because our check happens before the crud-removal step. We could skip past the crud before checking for an empty name, but let's make it a separate code path, for two reasons. One is that we can give a more specific error message. And two is that unlike a blank name, we probably don't want to kick in the fallback-to-username behavior. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 862e80a commit 13b9a24

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ident.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ static int crud(unsigned char c)
203203
c == '\'';
204204
}
205205

206+
static int has_non_crud(const char *str)
207+
{
208+
for (; *str; str++) {
209+
if (!crud(*str))
210+
return 1;
211+
}
212+
return 0;
213+
}
214+
206215
/*
207216
* Copy over a string to the destination, but avoid special
208217
* characters ('\n', '<' and '>') and remove crud at the end
@@ -389,6 +398,8 @@ const char *fmt_ident(const char *name, const char *email,
389398
pw = xgetpwuid_self(NULL);
390399
name = pw->pw_name;
391400
}
401+
if (strict && !has_non_crud(name))
402+
die(_("name consists only of disallowed characters: %s"), name);
392403
}
393404

394405
strbuf_reset(&ident);

t/t7518-ident-corner-cases.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ test_expect_success 'empty name and missing email' '
1717
)
1818
'
1919

20+
test_expect_success 'commit rejects all-crud name' '
21+
test_must_fail env GIT_AUTHOR_NAME=" .;<>" \
22+
git commit --allow-empty -m foo
23+
'
24+
2025
test_done

0 commit comments

Comments
 (0)