From: Kay Sievers Date: Fri, 25 May 2007 01:35:18 +0000 (+0200) Subject: volume_id: terminate overlong label strings X-Git-Tag: 174~1932 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a3b836c6c22614d2aa746d89ddfaa6888e47691f;p=systemd volume_id: terminate overlong label strings --- diff --git a/extras/volume_id/lib/iso9660.c b/extras/volume_id/lib/iso9660.c index 227641e4..1d8bdff8 100644 --- a/extras/volume_id/lib/iso9660.c +++ b/extras/volume_id/lib/iso9660.c @@ -91,7 +91,7 @@ int volume_id_probe_iso9660(struct volume_id *id, uint64_t off, uint64_t size) memcmp(is->escape_sequences, "%/C", 3) == 0|| memcmp(is->escape_sequences, "%/E", 3) == 0) { dbg("Joliet extension found"); - volume_id_set_unicode16((char *)svd_label, sizeof(svd_label), is->volume_id, BE, 32); + volume_id_set_unicode16(svd_label, sizeof(svd_label), is->volume_id, BE, 32); if (memcmp(id->label, svd_label, 16) == 0) { dbg("SVD label is identical, use the possibly longer PVD one"); break; diff --git a/extras/volume_id/lib/util.c b/extras/volume_id/lib/util.c index eaaececa..395e81bb 100644 --- a/extras/volume_id/lib/util.c +++ b/extras/volume_id/lib/util.c @@ -151,9 +151,9 @@ int volume_id_utf8_encoded_valid_unichar(const char *str) return len; } -void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count) +size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count) { - unsigned int i, j; + size_t i, j; uint16_t c; j = 0; @@ -183,6 +183,7 @@ void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum end } } str[j] = '\0'; + return j; } static char *usage_to_string(enum volume_id_usage usage_id) @@ -214,15 +215,22 @@ void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id) void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count) { + if (count > sizeof(id->label)) + count = sizeof(id->label); + memcpy(id->label_raw, buf, count); id->label_raw_len = count; } void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count) { - unsigned int i; + size_t i; + + if (count >= sizeof(id->label)) + count = sizeof(id->label)-1; memcpy(id->label, buf, count); + id->label[count] = '\0'; /* remove trailing whitespace */ i = strnlen(id->label, count); @@ -235,7 +243,10 @@ void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count) { - volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count); + if (count >= sizeof(id->label)) + count = sizeof(id->label)-1; + + volume_id_set_unicode16((uint8_t *)id->label, sizeof(id->label), buf, endianess, count); } void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, size_t len, enum uuid_format format) diff --git a/extras/volume_id/lib/util.h b/extras/volume_id/lib/util.h index ed868001..14a76c8f 100644 --- a/extras/volume_id/lib/util.h +++ b/extras/volume_id/lib/util.h @@ -80,7 +80,7 @@ enum endian { }; extern int volume_id_utf8_encoded_valid_unichar(const char *str); -extern void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count); +extern size_t volume_id_set_unicode16(uint8_t *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count); extern void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id); extern void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count); extern void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count);