Skip to content

Commit 7f33230

Browse files
Unique-Usmangitster
authored andcommitted
version: refactor redact_non_printables()
The git_user_agent_sanitized() function performs some sanitizing to avoid special characters being sent over the line and possibly messing up with the protocol or with the parsing on the other side. Let's extract this sanitizing into a new redact_non_printables() function, as we will want to reuse it in a following patch. For now the new redact_non_printables() function is still static as it's only needed locally. While at it, let's also make a few small improvements: - use 'size_t' for 'i' instead of 'int', - move the declaration of 'i' inside the 'for ( ... )', - use strbuf_detach() to explicitly detach the string contained by the 'buf' strbuf. Mentored-by: Christian Couder <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e63e621 commit 7f33230

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

version.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
const char git_version_string[] = GIT_VERSION;
77
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
88

9+
/*
10+
* Trim and replace each character with ascii code below 32 or above
11+
* 127 (included) using a dot '.' character.
12+
* TODO: ensure consecutive non-printable characters are only replaced once
13+
*/
14+
static void redact_non_printables(struct strbuf *buf)
15+
{
16+
strbuf_trim(buf);
17+
for (size_t i = 0; i < buf->len; i++) {
18+
if (buf->buf[i] <= 32 || buf->buf[i] >= 127)
19+
buf->buf[i] = '.';
20+
}
21+
}
22+
923
const char *git_user_agent(void)
1024
{
1125
static const char *agent = NULL;
@@ -27,12 +41,8 @@ const char *git_user_agent_sanitized(void)
2741
struct strbuf buf = STRBUF_INIT;
2842

2943
strbuf_addstr(&buf, git_user_agent());
30-
strbuf_trim(&buf);
31-
for (size_t i = 0; i < buf.len; i++) {
32-
if (buf.buf[i] <= 32 || buf.buf[i] >= 127)
33-
buf.buf[i] = '.';
34-
}
35-
agent = buf.buf;
44+
redact_non_printables(&buf);
45+
agent = strbuf_detach(&buf, NULL);
3646
}
3747

3848
return agent;

0 commit comments

Comments
 (0)