chiark / gitweb /
utf8: when escaping unprintable unichars, escape the whole unichar, not just the...
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Dec 2014 01:27:14 +0000 (02:27 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 4 Dec 2014 01:27:14 +0000 (02:27 +0100)
src/shared/utf8.c
src/test/test-utf8.c

index 0b6c38e..6996820 100644 (file)
@@ -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;
                 }
         }
index 6dde63c..3399f2b 100644 (file)
@@ -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[]) {