]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: (dasd) use rtrim_whitespace() for EBCDIC string trimming
authorKarel Zak <kzak@redhat.com>
Tue, 19 May 2026 07:49:32 +0000 (09:49 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 19 May 2026 08:32:19 +0000 (10:32 +0200)
Replace hand-rolled trailing-space trim loops in dasd_get_volser()
and dasd_get_dsnam() with rtrim_whitespace() from strutils.h.

The original dasd_get_volser() loop had no lower-bound guard and
would read out-of-bounds if all VOLSER bytes were spaces.

Addresses: https://github.com/util-linux/util-linux/pull/4096

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/partitions/dasd.c

index 501a603f766f94da32cc6e412cb6921c8e32af99..969944d2ce409cd0508659d180a7bc4f3f98836d 100644 (file)
 #include <inttypes.h>
 
 #include "pt-dasd.h"
+#include "strutils.h"
 #include "partitions.h"
 
 static void dasd_get_volser(const char *volid, char *volser)
 {
-       int i = 0;
+       int i;
 
        for (i = 0; i < DASD_VOLSER_LENGTH; i++)
                volser[i] = dasd_ebcdic_to_ascii[(unsigned char) volid[i]];
        volser[DASD_VOLSER_LENGTH] = '\0';
 
-       /* trim trailing spaces */
-       for (i = DASD_VOLSER_LENGTH - 1; volser[i] == ' '; i--)
-               volser[i] = '\0';
+       rtrim_whitespace((unsigned char *) volser);
 }
 
 static void dasd_get_dsnam(const struct dasd_format1_label *f1, char *dsnam)
 {
-       size_t i = 0;
+       size_t i;
 
        for (i = 0; i < sizeof(f1->DS1DSNAM); i++)
                dsnam[i] = dasd_ebcdic_to_ascii[(unsigned char) f1->DS1DSNAM[i]];
        dsnam[sizeof(f1->DS1DSNAM)] = '\0';
 
-       /* trim trailing spaces */
-       for (i = sizeof(f1->DS1DSNAM) - 1; i != 0 && dsnam[i] == ' '; i--)
-               dsnam[i] = '\0';
+       rtrim_whitespace((unsigned char *) dsnam);
 }
 
 /*