]> err.no Git - util-linux/commitdiff
libblkid: trim tailing whitespace from unicode LABELs
authorKarel Zak <kzak@redhat.com>
Tue, 22 Sep 2009 10:32:34 +0000 (12:32 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 2 Oct 2009 18:31:40 +0000 (20:31 +0200)
old version:
$ ./blkid -o udev -p iso-joliet.img
ID_FS_LABEL=ThisIsLabel
ID_FS_LABEL_ENC=ThisIsLabel\x20\x20\x20\x20\x20
ID_FS_VERSION=Joliet\x20Extension
ID_FS_TYPE=iso9660
ID_FS_USAGE=filesystem

new version:
$ ./blkid -o udev -p iso-joliet.img
ID_FS_LABEL=ThisIsLabel
ID_FS_LABEL_ENC=ThisIsLabel
ID_FS_VERSION=Joliet\x20Extension
ID_FS_TYPE=iso9660
ID_FS_USAGE=filesystem

Reported-by: Maxim Levitsky <maximlevitsky@gmail.com>
Addresses-Ubuntu-Bug: #432215
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/blkid/src/probe.c

index 10fcb0035c7f487a8e5ffe37006f1b743558dd74..57a4e6091729f3d56aab9e7e57d37370ce675877 100644 (file)
@@ -685,10 +685,27 @@ static int blkid_probe_set_usage(blkid_probe pr, int usage)
        return blkid_probe_set_value(pr, "USAGE", (unsigned char *) u, strlen(u) + 1);
 }
 
+
+/* Removes whitespace from the right-hand side of a string (trailing
+ * whitespace).
+ *
+ * Returns size of the new string (without \0).
+ */
+static size_t blkid_rtrim_whitespace(unsigned char *str)
+{
+       size_t i = strlen((char *) str);
+
+       while (i--) {
+               if (!isspace(str[i]))
+                       break;
+       }
+       str[++i] = '\0';
+       return i;
+}
+
 int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
 {
        struct blkid_prval *v;
-       int i;
 
        if (len > BLKID_PROBVAL_BUFSIZ)
                len = BLKID_PROBVAL_BUFSIZ;
@@ -704,15 +721,7 @@ int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
 
        memcpy(v->data, label, len);
        v->data[len] = '\0';
-
-       /* remove trailing whitespace */
-       i = strnlen((char *) v->data, len);
-       while (i--) {
-               if (!isspace(v->data[i]))
-                       break;
-       }
-       v->data[++i] = '\0';
-       v->len = i + 1;
+       v->len = blkid_rtrim_whitespace(v->data) + 1;
        return 0;
 }
 
@@ -765,7 +774,8 @@ int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label,
        if (!v)
                return -1;
 
-       v->len = encode_to_utf8(enc, v->data, sizeof(v->data), label, len);
+       encode_to_utf8(enc, v->data, sizeof(v->data), label, len);
+       v->len = blkid_rtrim_whitespace(v->data) + 1;
        return 0;
 }