a4b143
From b6c9f1408abaebffc4b8151fa5615075767f4a92 Mon Sep 17 00:00:00 2001
a4b143
From: Dave Reisner <dreisner@archlinux.org>
a4b143
Date: Tue, 17 Sep 2013 15:47:08 -0400
a4b143
Subject: [PATCH] Use udev_encode_string in fstab_node_to_udev_node
a4b143
a4b143
Resolves a longstanding bug which caused this function to wrongly
a4b143
handle (escape) valid utf8 characters.
a4b143
---
a4b143
 src/shared/util.c    | 20 +++++++++-----------
a4b143
 src/test/test-util.c | 36 ++++++++++++++++++++++++++++++++++++
a4b143
 2 files changed, 45 insertions(+), 11 deletions(-)
a4b143
a4b143
diff --git a/src/shared/util.c b/src/shared/util.c
a4b143
index 3d4ca7c..766957a 100644
a4b143
--- a/src/shared/util.c
a4b143
+++ b/src/shared/util.c
a4b143
@@ -73,6 +73,7 @@
a4b143
 #include "hashmap.h"
a4b143
 #include "env-util.h"
a4b143
 #include "fileio.h"
a4b143
+#include "utf8.h"
a4b143
 
a4b143
 int saved_argc = 0;
a4b143
 char **saved_argv = NULL;
a4b143
@@ -3495,26 +3496,23 @@ int signal_from_string_try_harder(const char *s) {
a4b143
 }
a4b143
 
a4b143
 static char *tag_to_udev_node(const char *tagvalue, const char *by) {
a4b143
-        char *dn, *t, *u;
a4b143
-        int r;
a4b143
-
a4b143
-        /* FIXME: to follow udev's logic 100% we need to leave valid
a4b143
-         * UTF8 chars unescaped */
a4b143
+        _cleanup_free_ char *t = NULL, *u = NULL;
a4b143
+        char *dn;
a4b143
+        size_t enc_len;
a4b143
 
a4b143
         u = unquote(tagvalue, "\"\'");
a4b143
         if (u == NULL)
a4b143
                 return NULL;
a4b143
 
a4b143
-        t = xescape(u, "/ ");
a4b143
-        free(u);
a4b143
-
a4b143
+        enc_len = strlen(u) * 4;
a4b143
+        t = new(char, enc_len);
a4b143
         if (t == NULL)
a4b143
                 return NULL;
a4b143
 
a4b143
-        r = asprintf(&dn, "/dev/disk/by-%s/%s", by, t);
a4b143
-        free(t);
a4b143
+        if (udev_encode_string(u, t, enc_len) < 0)
a4b143
+                return NULL;
a4b143
 
a4b143
-        if (r < 0)
a4b143
+        if (asprintf(&dn, "/dev/disk/by-%s/%s", by, t) < 0)
a4b143
                 return NULL;
a4b143
 
a4b143
         return dn;
a4b143
diff --git a/src/test/test-util.c b/src/test/test-util.c
a4b143
index dd7768d..ad13d53 100644
a4b143
--- a/src/test/test-util.c
a4b143
+++ b/src/test/test-util.c
a4b143
@@ -547,6 +547,41 @@ static void test_split_pair(void) {
a4b143
         assert_se(streq(b, "="));
a4b143
 }
a4b143
 
a4b143
+static void test_fstab_node_to_udev_node(void) {
a4b143
+        char *n;
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("LABEL=applé/jack");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
a4b143
+        free(n);
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
a4b143
+        free(n);
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
a4b143
+        free(n);
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
a4b143
+        free(n);
a4b143
+
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("PONIES=awesome");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "PONIES=awesome"));
a4b143
+        free(n);
a4b143
+
a4b143
+        n = fstab_node_to_udev_node("/dev/xda1");
a4b143
+        puts(n);
a4b143
+        assert_se(streq(n, "/dev/xda1"));
a4b143
+        free(n);
a4b143
+}
a4b143
+
a4b143
 int main(int argc, char *argv[]) {
a4b143
         test_streq_ptr();
a4b143
         test_first_word();
a4b143
@@ -582,6 +617,7 @@ int main(int argc, char *argv[]) {
a4b143
         test_strrep();
a4b143
         test_parse_user_at_host();
a4b143
         test_split_pair();
a4b143
+        test_fstab_node_to_udev_node();
a4b143
 
a4b143
         return 0;
a4b143
 }