]> git.ipfire.org Git - thirdparty/git.git/commitdiff
version: refactor redact_non_printables()
authorUsman Akinyemi <usmanakinyemi202@gmail.com>
Sat, 15 Feb 2025 15:50:48 +0000 (21:20 +0530)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Feb 2025 17:05:12 +0000 (09:05 -0800)
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 <chriscool@tuxfamily.org>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
version.c

index 6cfbb8ca5630480bf45990cbdd5e0084d8378774..60df71fd0ebc28779072236ced0d55dd5b143ed9 100644 (file)
--- a/version.c
+++ b/version.c
@@ -7,6 +7,19 @@
 const char git_version_string[] = GIT_VERSION;
 const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
 
+/*
+ * Trim and replace each character with ascii code below 32 or above
+ * 127 (included) using a dot '.' character.
+ */
+static void redact_non_printables(struct strbuf *buf)
+{
+       strbuf_trim(buf);
+       for (size_t i = 0; i < buf->len; i++) {
+               if (!isprint(buf->buf[i]) || buf->buf[i] == ' ')
+                       buf->buf[i] = '.';
+       }
+}
+
 const char *git_user_agent(void)
 {
        static const char *agent = NULL;
@@ -28,12 +41,8 @@ const char *git_user_agent_sanitized(void)
                struct strbuf buf = STRBUF_INIT;
 
                strbuf_addstr(&buf, git_user_agent());
-               strbuf_trim(&buf);
-               for (size_t i = 0; i < buf.len; i++) {
-                       if (!isprint(buf.buf[i]) || buf.buf[i] == ' ')
-                               buf.buf[i] = '.';
-               }
-               agent = buf.buf;
+               redact_non_printables(&buf);
+               agent = strbuf_detach(&buf, NULL);
        }
 
        return agent;