bus-util: replace non-printable values with [unprintable]
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 1 Jul 2017 20:49:15 +0000 (16:49 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 3 Jul 2017 13:30:57 +0000 (09:30 -0400)
Like I said in the previous commit, such values do not seem to appear in normal
use, but it's pretty hard to prove that all paths to assign values properly
check that they contain no spaces. So just in case some slip through, replace
values with spaces (in case of single-valued properties) or spaces and newlines
(in case of array proprties) with "[unprintable]". We were already doing it
in case of properties which we didn't know how to print, so this fits in well.
The advantage is the previous code which used escaping that a) this is easier
to spot, b) does not mess up printing of properties which were properly escaped
already.

v2:
- add comments

src/shared/bus-util.c

index 8705a7b..207b5e6 100644 (file)
@@ -724,8 +724,14 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b
                 if (r < 0)
                         return r;
 
-                if (all || !isempty(s))
-                        print_property(name, "%s", s);
+                if (all || !isempty(s)) {
+                        bool good;
+
+                        /* This property has a single value, so we need to take
+                         * care not to print a new line, everything else is OK. */
+                        good = !strchr(s, '\n');
+                        print_property(name, "%s", good ? s : "[unprintable]");
+                }
 
                 return 1;
         }
@@ -845,10 +851,16 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b
                                 return r;
 
                         while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
+                                bool good;
+
                                 if (first && !value)
                                         printf("%s=", name);
 
-                                printf("%s%s", first ? "" : " ", str);
+                                /* This property has multiple space-seperated values, so
+                                 * neither spaces not newlines can be allowed in a value. */
+                                good = str[strcspn(str, " \n")] == '\0';
+
+                                printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
 
                                 first = false;
                         }