Skip to content

Commit f4b22ef

Browse files
Unique-Usmanchriscool
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]>
1 parent 9e64601 commit f4b22ef

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
@@ -12,6 +12,19 @@
1212
const char git_version_string[] = GIT_VERSION;
1313
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
1414

15+
/*
16+
* Trim and replace each character with ascii code below 32 or above
17+
* 127 (included) using a dot '.' character.
18+
*/
19+
static void redact_non_printables(struct strbuf *buf)
20+
{
21+
strbuf_trim(buf);
22+
for (size_t i = 0; i < buf->len; i++) {
23+
if (!isprint(buf->buf[i]) || buf->buf[i] == ' ')
24+
buf->buf[i] = '.';
25+
}
26+
}
27+
1528
const char *git_user_agent(void)
1629
{
1730
static const char *agent = NULL;
@@ -33,12 +46,8 @@ const char *git_user_agent_sanitized(void)
3346
struct strbuf buf = STRBUF_INIT;
3447

3548
strbuf_addstr(&buf, git_user_agent());
36-
strbuf_trim(&buf);
37-
for (size_t i = 0; i < buf.len; i++) {
38-
if (!isprint(buf.buf[i]) || buf.buf[i] == ' ')
39-
buf.buf[i] = '.';
40-
}
41-
agent = buf.buf;
49+
redact_non_printables(&buf);
50+
agent = strbuf_detach(&buf, NULL);
4251
}
4352

4453
return agent;

0 commit comments

Comments
 (0)