[systemd-commits] 2 commits - src/bootchart src/shared src/test

Lennart Poettering lennart at kemper.freedesktop.org
Wed Dec 3 17:10:50 PST 2014


 src/bootchart/svg.c  |   10 +++++++---
 src/shared/utf8.c    |   39 +++++++++++++++++++++++++++++++++++++++
 src/shared/utf8.h    |    1 +
 src/test/test-utf8.c |   25 +++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 3 deletions(-)

New commits:
commit 3f18c60b2e09d1191ffede3aeabd967b57a1f918
Author: WaLyong Cho <walyong.cho at samsung.com>
Date:   Wed Nov 12 19:49:32 2014 +0900

    bootchart: escape non printable process name

diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index faf377e..e5569e1 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -39,6 +39,7 @@
 #include "svg.h"
 #include "bootchart.h"
 #include "list.h"
+#include "utf8.h"
 
 #define time_to_graph(t) ((t) * arg_scale_x)
 #define ps_to_graph(n) ((n) * arg_scale_y)
@@ -1006,12 +1007,15 @@ static void svg_ps_bars(void) {
         /* pass 2 - ps boxes */
         ps = ps_first;
         while ((ps = get_next_ps(ps))) {
-                _cleanup_free_ char *enc_name = NULL;
+                _cleanup_free_ char *enc_name = NULL, *escaped = NULL;
                 double endtime;
                 double starttime;
                 int t;
 
-                enc_name = xml_comment_encode(ps->name);
+                if (!utf8_is_printable(ps->name, strlen(ps->name)))
+                        escaped = utf8_escape_non_printable(ps->name);
+
+                enc_name = xml_comment_encode(escaped ? escaped : ps->name);
                 if (!enc_name)
                         continue;
 
@@ -1100,7 +1104,7 @@ static void svg_ps_bars(void) {
                 svg("  <text x=\"%.03f\" y=\"%.03f\"><![CDATA[%s]]> [%i]<tspan class=\"run\">%.03fs</tspan> %s</text>\n",
                     time_to_graph(w - graph_start) + 5.0,
                     ps_to_graph(j) + 14.0,
-                    ps->name,
+                    escaped ? escaped : ps->name,
                     ps->pid,
                     (ps->last->runtime - ps->first->runtime) / 1000000000.0,
                     arg_show_cgroup ? ps->cgroup : "");

commit fec8457652002fd5bff923042a7407b3810f5a7f
Author: WaLyong Cho <walyong.cho at samsung.com>
Date:   Wed Nov 12 19:49:31 2014 +0900

    utf8: intruduce utf8_escape_non_printable

diff --git a/src/shared/utf8.c b/src/shared/utf8.c
index 8702ceb..0b6c38e 100644
--- a/src/shared/utf8.c
+++ b/src/shared/utf8.c
@@ -212,6 +212,45 @@ char *utf8_escape_invalid(const char *str) {
         return p;
 }
 
+char *utf8_escape_non_printable(const char *str) {
+        char *p, *s;
+
+        assert(str);
+
+        p = s = malloc(strlen(str) * 4 + 1);
+        if (!p)
+                return NULL;
+
+        while (*str) {
+                int len;
+
+                len = utf8_encoded_valid_unichar(str);
+                if (len > 0) {
+                        if (utf8_is_printable(str, len)) {
+                                s = mempcpy(s, str, len);
+                                str += len;
+                        } else {
+                                if ((*str < ' ') || (*str >= 127)) {
+                                        *(s++) = '\\';
+                                        *(s++) = 'x';
+                                        *(s++) = hexchar((int) *str >> 4);
+                                        *(s++) = hexchar((int) *str);
+                                } else
+                                        *(s++) = *str;
+
+                                str += 1;
+                        }
+                } else {
+                        s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
+                        str += 1;
+                }
+        }
+
+        *s = '\0';
+
+        return p;
+}
+
 char *ascii_is_valid(const char *str) {
         const char *p;
 
diff --git a/src/shared/utf8.h b/src/shared/utf8.h
index c087995..1fe1a35 100644
--- a/src/shared/utf8.h
+++ b/src/shared/utf8.h
@@ -30,6 +30,7 @@
 const char *utf8_is_valid(const char *s) _pure_;
 char *ascii_is_valid(const char *s) _pure_;
 char *utf8_escape_invalid(const char *s);
+char *utf8_escape_non_printable(const char *str);
 
 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_;
 _pure_ static inline bool utf8_is_printable(const char* str, size_t length) {
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c
index b7d988f..6dde63c 100644
--- a/src/test/test-utf8.c
+++ b/src/test/test-utf8.c
@@ -66,12 +66,37 @@ static void test_utf8_escaping(void) {
         assert_se(utf8_is_valid(p3));
 }
 
+static void test_utf8_escaping_printable(void) {
+        _cleanup_free_ char *p1, *p2, *p3, *p4, *p5;
+
+        p1 = utf8_escape_non_printable("goo goo goo");
+        puts(p1);
+        assert_se(utf8_is_valid(p1));
+
+        p2 = utf8_escape_non_printable("\341\204\341\204");
+        puts(p2);
+        assert_se(utf8_is_valid(p2));
+
+        p3 = utf8_escape_non_printable("\341\204");
+        puts(p3);
+        assert_se(utf8_is_valid(p3));
+
+        p4 = utf8_escape_non_printable("ąę\n가너도루\n1234\n\341\204\341\204\n\001 \019\20\a");
+        puts(p4);
+        assert_se(utf8_is_valid(p4));
+
+        p5 = utf8_escape_non_printable("\001 \019\20\a");
+        puts(p5);
+        assert_se(utf8_is_valid(p5));
+}
+
 int main(int argc, char *argv[]) {
         test_utf8_is_valid();
         test_utf8_is_printable();
         test_ascii_is_valid();
         test_utf8_encoded_valid_unichar();
         test_utf8_escaping();
+        test_utf8_escaping_printable();
 
         return 0;
 }



More information about the systemd-commits mailing list