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

Lennart Poettering lennart at kemper.freedesktop.org
Wed Dec 3 17:38:16 PST 2014


 src/shared/utf8.c    |   20 ++++++++++----------
 src/shared/utf8.h    |    9 ++++-----
 src/test/test-utf8.c |    7 +++++--
 3 files changed, 19 insertions(+), 17 deletions(-)

New commits:
commit 6ed62be0b15ac92d9687cff93c63d7b06d11b9f0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Dec 4 02:36:04 2014 +0100

    utf8: minor simplifications

diff --git a/src/shared/utf8.c b/src/shared/utf8.c
index 6996820..4469a73 100644
--- a/src/shared/utf8.c
+++ b/src/shared/utf8.c
@@ -142,19 +142,19 @@ int utf8_encoded_to_unichar(const char *str) {
 }
 
 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
-        const uint8_t *p;
+        const char *p;
 
         assert(str);
 
-        for (p = (const uint8_t*) str; length;) {
+        for (p = str; length;) {
                 int encoded_len, val;
 
-                encoded_len = utf8_encoded_valid_unichar((const char *) p);
+                encoded_len = utf8_encoded_valid_unichar(p);
                 if (encoded_len < 0 ||
                     (size_t) encoded_len > length)
                         return false;
 
-                val = utf8_encoded_to_unichar((const char*) p);
+                val = utf8_encoded_to_unichar(p);
                 if (val < 0 ||
                     is_unicode_control(val) ||
                     (!newline && val == '\n'))
diff --git a/src/shared/utf8.h b/src/shared/utf8.h
index 1fe1a35..59abee5 100644
--- a/src/shared/utf8.h
+++ b/src/shared/utf8.h
@@ -29,13 +29,12 @@
 
 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) {
-        return utf8_is_printable_newline(str, length, true);
-}
+#define utf8_is_printable(str, length) utf8_is_printable_newline(str, length, true)
+
+char *utf8_escape_invalid(const char *s);
+char *utf8_escape_non_printable(const char *str);
 
 char *utf16_to_utf8(const void *s, size_t length);
 

commit 3c6d3052d3597100e9d531df03cb15e9988dc94d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Dec 4 02:27:14 2014 +0100

    utf8: when escaping unprintable unichars, escape the whole unichar, not just the first byte of it

diff --git a/src/shared/utf8.c b/src/shared/utf8.c
index 0b6c38e..6996820 100644
--- a/src/shared/utf8.c
+++ b/src/shared/utf8.c
@@ -202,7 +202,7 @@ char *utf8_escape_invalid(const char *str) {
                         s = mempcpy(s, str, len);
                         str += len;
                 } else {
-                        s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
+                        s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
                         str += 1;
                 }
         }
@@ -230,18 +230,18 @@ char *utf8_escape_non_printable(const char *str) {
                                 s = mempcpy(s, str, len);
                                 str += len;
                         } else {
-                                if ((*str < ' ') || (*str >= 127)) {
+                                while (len > 0) {
                                         *(s++) = '\\';
                                         *(s++) = 'x';
                                         *(s++) = hexchar((int) *str >> 4);
                                         *(s++) = hexchar((int) *str);
-                                } else
-                                        *(s++) = *str;
 
-                                str += 1;
+                                        str += 1;
+                                        len --;
+                                }
                         }
                 } else {
-                        s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
+                        s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
                         str += 1;
                 }
         }
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c
index 6dde63c..3399f2b 100644
--- a/src/test/test-utf8.c
+++ b/src/test/test-utf8.c
@@ -47,7 +47,6 @@ static void test_utf8_encoded_valid_unichar(void) {
         assert_se(utf8_encoded_valid_unichar("a") == 1);
         assert_se(utf8_encoded_valid_unichar("\341\204") < 0);
         assert_se(utf8_encoded_valid_unichar("\341\204\341\204") < 0);
-
 }
 
 static void test_utf8_escaping(void) {
@@ -67,7 +66,7 @@ static void test_utf8_escaping(void) {
 }
 
 static void test_utf8_escaping_printable(void) {
-        _cleanup_free_ char *p1, *p2, *p3, *p4, *p5;
+        _cleanup_free_ char *p1, *p2, *p3, *p4, *p5, *p6;
 
         p1 = utf8_escape_non_printable("goo goo goo");
         puts(p1);
@@ -88,6 +87,10 @@ static void test_utf8_escaping_printable(void) {
         p5 = utf8_escape_non_printable("\001 \019\20\a");
         puts(p5);
         assert_se(utf8_is_valid(p5));
+
+        p6 = utf8_escape_non_printable("\xef\xbf\x30\x13");
+        puts(p6);
+        assert_se(utf8_is_valid(p6));
 }
 
 int main(int argc, char *argv[]) {



More information about the systemd-commits mailing list