Skip to content

Commit cdfd081

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 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 0c124cb commit cdfd081

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

version.c

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

10+
/*
11+
* Trim and replace each character with ascii code below 32 or above
12+
* 127 (included) using a dot '.' character.
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 (!isprint(buf->buf[i]) || buf->buf[i] == ' ')
19+
buf->buf[i] = '.';
20+
}
21+
}
22+
1023
const char *git_user_agent(void)
1124
{
1225
static const char *agent = NULL;
@@ -28,12 +41,8 @@ const char *git_user_agent_sanitized(void)
2841
struct strbuf buf = STRBUF_INIT;
2942

3043
strbuf_addstr(&buf, git_user_agent());
31-
strbuf_trim(&buf);
32-
for (size_t i = 0; i < buf.len; i++) {
33-
if (!isprint(buf.buf[i]) || buf.buf[i] == ' ')
34-
buf.buf[i] = '.';
35-
}
36-
agent = buf.buf;
44+
redact_non_printables(&buf);
45+
agent = strbuf_detach(&buf, NULL);
3746
}
3847

3948
return agent;

0 commit comments

Comments
 (0)