From c2dbd49bdfd84e9145044ae564d5ef841e60a41e 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/blkidP.h | 1 + shlibs/blkid/src/partitions/partitions.c | 12 +++-------- shlibs/blkid/src/probe.c | 24 ++++++++++++++++++++++ shlibs/blkid/src/superblocks/superblocks.c | 14 +++---------- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 10c29776..40002c5c 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -413,6 +413,7 @@ extern int blkid_probe_sprintf_value(blkid_probe pr, const char *name, const char *fmt, ...); extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len); +extern size_t blkid_rtrim_whitespace(unsigned char *str); /* filter bitmap macros */ #define blkid_bmp_wordsize (8 * sizeof(unsigned long)) diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index 94d1556e..c1c39164 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -855,21 +855,13 @@ int blkid_partition_is_logical(blkid_partition par) static void set_string(unsigned char *item, size_t max, const unsigned char *data, size_t len) { - int i; - if (len >= max) len = max - 1; memcpy(item, data, len); item[len] = '\0'; - /* remove trailing whitespace */ - i = strlen((char *) item); - while (i--) { - if (!isspace(item[i])) - break; - } - item[++i] = '\0'; + blkid_rtrim_whitespace(item); } int blkid_partition_set_name(blkid_partition par, @@ -887,7 +879,9 @@ int blkid_partition_set_utf8name(blkid_partition par, const unsigned char *name, { if (!par) return -1; + blkid_encode_to_utf8(enc, par->name, sizeof(par->name), name, len); + blkid_rtrim_whitespace(par->name); return 0; } diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index 05f61cf0..bac49771 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -911,6 +911,9 @@ int blkid_probe_numof_values(blkid_probe pr) * @data: pointer to return value data or NULL * @len: pointer to return value length or NULL * + * Note, the @len returns length of the @data, including the terminating + * '\0' character. + * * Returns: 0 on success, or -1 in case of error. */ int blkid_probe_get_value(blkid_probe pr, int num, const char **name, @@ -938,6 +941,9 @@ int blkid_probe_get_value(blkid_probe pr, int num, const char **name, * @data: pointer to return value data or NULL * @len: pointer to return value length or NULL * + * Note, the @len returns length of the @data, including the terminating + * '\0' character. + * * Returns: 0 on success, or -1 in case of error. */ int blkid_probe_lookup_value(blkid_probe pr, const char *name, @@ -1013,3 +1019,21 @@ void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len) #endif } + +/* Removes whitespace from the right-hand side of a string (trailing + * whitespace). + * + * Returns size of the new string (without \0). + */ +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; +} + diff --git a/shlibs/blkid/src/superblocks/superblocks.c b/shlibs/blkid/src/superblocks/superblocks.c index 0abdab22..077a7336 100644 --- a/shlibs/blkid/src/superblocks/superblocks.c +++ b/shlibs/blkid/src/superblocks/superblocks.c @@ -467,8 +467,6 @@ int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len) { struct blkid_chain *chn = blkid_probe_get_chain(pr); struct blkid_prval *v; - int i; - if (len > BLKID_PROBVAL_BUFSIZ) len = BLKID_PROBVAL_BUFSIZ; @@ -487,14 +485,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 = strlen((char *) v->data); - 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; } @@ -513,7 +504,8 @@ int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label, if (!v) return -1; - v->len = blkid_encode_to_utf8(enc, v->data, sizeof(v->data), label, len); + blkid_encode_to_utf8(enc, v->data, sizeof(v->data), label, len); + v->len = blkid_rtrim_whitespace(v->data) + 1; return 0; } -- 2.39.5