]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
libudev-util: make util_replace_whitespace() read only len characters
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 25 Dec 2018 03:56:48 +0000 (12:56 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 3 Jan 2019 14:10:57 +0000 (15:10 +0100)
This effectively reverts df8ba4fa0e8be1ff7899d08a4b6be0196c8405a0.

Fixes #11264.

src/libudev/libudev-util.c
src/test/test-libudev.c

index f67ab40354121f11b05d321603b6a5f0d832c4ed..7e21719fbf2c1e495f89491ab6d2837e6f6ef71a 100644 (file)
@@ -122,19 +122,20 @@ size_t util_path_encode(const char *src, char *dest, size_t size) {
  *
  * Note this may be called with 'str' == 'to', i.e. to replace whitespace
  * in-place in a buffer.  This function can handle that situation.
+ *
+ * Note that only 'len' characters are read from 'str'.
  */
 size_t util_replace_whitespace(const char *str, char *to, size_t len) {
         bool is_space = false;
-        const char *p = str;
-        size_t j;
+        size_t i, j;
 
         assert(str);
         assert(to);
 
-        p += strspn(p, WHITESPACE);
+        i = strspn(str, WHITESPACE);
 
-        for (j = 0; j < len && *p != '\0'; p++) {
-                if (isspace(*p)) {
+        for (j = 0; j < len && i < len && str[i] != '\0'; i++) {
+                if (isspace(str[i])) {
                         is_space = true;
                         continue;
                 }
@@ -146,7 +147,7 @@ size_t util_replace_whitespace(const char *str, char *to, size_t len) {
                         to[j++] = '_';
                         is_space = false;
                 }
-                to[j++] = *p;
+                to[j++] = str[i];
         }
 
         to[j] = '\0';
index 53c9e5f7459b7ed44149a4c0dea6109aa48ea6c8..15c0f8853d43478a5e91a3bd348821415ea63d16 100644 (file)
@@ -365,16 +365,23 @@ static void test_util_replace_whitespace(void) {
         test_util_replace_whitespace_one_len("hoge hoge    ", 1, "h");
         test_util_replace_whitespace_one_len("hoge hoge    ", 0, "");
 
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 9, "hoge_hoge");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 8, "hoge_hog");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 7, "hoge_ho");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 6, "hoge_h");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 5, "hoge");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 4, "hoge");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 3, "hog");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 2, "ho");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 1, "h");
-        test_util_replace_whitespace_one_len("         hoge   hoge    ", 0, "");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 16, "hoge_hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 15, "hoge_hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 14, "hoge_hog");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 13, "hoge_ho");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 12, "hoge_h");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 11, "hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 10, "hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 9, "hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 8, "hoge");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 7, "hog");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 6, "ho");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 5, "h");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 4, "");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 3, "");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 2, "");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 1, "");
+        test_util_replace_whitespace_one_len("    hoge   hoge    ", 0, "");
 }
 
 static void test_util_resolve_subsys_kernel_one(const char *str, bool read_value, int retval, const char *expected) {