From 9afb99578d110014f0e1846c521eceb4f61dcbe2 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 22 Sep 2009 12:32:34 +0200 Subject: [PATCH] libblkid: trim tailing whitespace from unicode LABELs 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 Addresses-Ubuntu-Bug: #432215 Signed-off-by: Karel Zak --- shlibs/blkid/src/probe.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index 10fcb003..57a4e609 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -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; } -- 2.39.5