1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 08:26:52 +03:00

Use udev_encode_string in fstab_node_to_udev_node

Resolves a longstanding bug which caused this function to wrongly
handle (escape) valid utf8 characters.
This commit is contained in:
Dave Reisner 2013-09-17 15:47:08 -04:00
parent 02a36bc9a1
commit 22f5f6281f
2 changed files with 45 additions and 11 deletions

View File

@ -73,6 +73,7 @@
#include "hashmap.h"
#include "env-util.h"
#include "fileio.h"
#include "utf8.h"
int saved_argc = 0;
char **saved_argv = NULL;
@ -3495,26 +3496,23 @@ int signal_from_string_try_harder(const char *s) {
}
static char *tag_to_udev_node(const char *tagvalue, const char *by) {
char *dn, *t, *u;
int r;
/* FIXME: to follow udev's logic 100% we need to leave valid
* UTF8 chars unescaped */
_cleanup_free_ char *t = NULL, *u = NULL;
char *dn;
size_t enc_len;
u = unquote(tagvalue, "\"\'");
if (u == NULL)
return NULL;
t = xescape(u, "/ ");
free(u);
enc_len = strlen(u) * 4;
t = new(char, enc_len);
if (t == NULL)
return NULL;
r = asprintf(&dn, "/dev/disk/by-%s/%s", by, t);
free(t);
if (udev_encode_string(u, t, enc_len) < 0)
return NULL;
if (r < 0)
if (asprintf(&dn, "/dev/disk/by-%s/%s", by, t) < 0)
return NULL;
return dn;

View File

@ -547,6 +547,41 @@ static void test_split_pair(void) {
assert_se(streq(b, "="));
}
static void test_fstab_node_to_udev_node(void) {
char *n;
n = fstab_node_to_udev_node("LABEL=applé/jack");
puts(n);
assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
free(n);
n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
puts(n);
assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
free(n);
n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
puts(n);
assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
free(n);
n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
puts(n);
assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
free(n);
n = fstab_node_to_udev_node("PONIES=awesome");
puts(n);
assert_se(streq(n, "PONIES=awesome"));
free(n);
n = fstab_node_to_udev_node("/dev/xda1");
puts(n);
assert_se(streq(n, "/dev/xda1"));
free(n);
}
int main(int argc, char *argv[]) {
test_streq_ptr();
test_first_word();
@ -582,6 +617,7 @@ int main(int argc, char *argv[]) {
test_strrep();
test_parse_user_at_host();
test_split_pair();
test_fstab_node_to_udev_node();
return 0;
}