]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
json_writer: support control character escaping
authorStephen Hemminger <stephen@networkplumber.org>
Fri, 10 Apr 2026 22:38:36 +0000 (15:38 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 10 Apr 2026 22:44:23 +0000 (15:44 -0700)
Iproute2 never handled control characters in strings correctly.
There are some cases like where string is under user control
like paths in ss command. Make iproute2 json output conform
to RFC 8259.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
lib/json_writer.c

index 2f3936c2821d1811ee70444b60fb3def82cd9ba0..7202621ea22ac57c63d5f8e297340b87b58b9a95 100644 (file)
@@ -53,7 +53,7 @@ static void jsonw_eor(json_writer_t *self)
 
 
 /* Output JSON encoded string */
-/* Handles C escapes, does not do Unicode */
+/* Handles C escapes and control characters per RFC 8259 */
 static void jsonw_puts(json_writer_t *self, const char *str)
 {
        putc('"', self->out);
@@ -81,7 +81,10 @@ static void jsonw_puts(json_writer_t *self, const char *str)
                        fputs("\\\"", self->out);
                        break;
                default:
-                       putc(*str, self->out);
+                       if ((unsigned char)*str < 0x20 || *str == 0x7f)
+                               fprintf(self->out, "\\u%04x", *str);
+                       else
+                               putc(*str, self->out);
                }
        putc('"', self->out);
 }