]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
escape: make 'bad' parameter optional
authorLennart Poettering <lennart@poettering.net>
Mon, 18 Nov 2024 22:10:38 +0000 (23:10 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 17 Jan 2025 19:47:43 +0000 (19:47 +0000)
Treat a NULL bad parameter just like an empty one: do not escape any
additional characters except for the CC chars.

src/basic/escape.c
src/test/test-escape.c

index 2067be4092d412b5ce1add26adad812acf3162b1..e50ae68cc602ba7fe46d0ded296d11a5e3fb7a1e 100644 (file)
@@ -365,6 +365,8 @@ char* xescape_full(const char *s, const char *bad, size_t console_width, XEscape
         char *ans, *t, *prev, *prev2;
         const char *f;
 
+        assert(s);
+
         /* Escapes all chars in bad, in addition to \ and all special chars, in \xFF style escaping. May be
          * reversed with cunescape(). If XESCAPE_8_BIT is specified, characters >= 127 are let through
          * unchanged. This corresponds to non-ASCII printable characters in pre-unicode encodings.
@@ -397,7 +399,7 @@ char* xescape_full(const char *s, const char *bad, size_t console_width, XEscape
 
                 if ((unsigned char) *f < ' ' ||
                     (!FLAGS_SET(flags, XESCAPE_8_BIT) && (unsigned char) *f >= 127) ||
-                    *f == '\\' || strchr(bad, *f)) {
+                    *f == '\\' || (bad && strchr(bad, *f))) {
                         if ((size_t) (t - ans) + 4 + 3 * force_ellipsis > console_width)
                                 break;
 
@@ -437,7 +439,7 @@ char* xescape_full(const char *s, const char *bad, size_t console_width, XEscape
 
 char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags) {
         if (FLAGS_SET(flags, XESCAPE_8_BIT))
-                return xescape_full(str, "", console_width, flags);
+                return xescape_full(str, /* bad= */ NULL, console_width, flags);
         else
                 return utf8_escape_non_printable_full(str,
                                                       console_width,
index 89a25febb43ff8cc47d2a8af77096776b01d415b..7021ff54d2b82d4aacc647521f95140149924d4e 100644 (file)
@@ -15,7 +15,7 @@ TEST(cescape) {
 TEST(xescape) {
         _cleanup_free_ char *t = NULL;
 
-        assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", ""));
+        assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", /* bad= */ NULL));
         ASSERT_STREQ(t, "abc\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb");
 }