]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: avoid strcasecmp() for ASCII-only strings
authorKarel Zak <kzak@redhat.com>
Tue, 1 Apr 2025 15:45:01 +0000 (17:45 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 2 Apr 2025 09:06:17 +0000 (11:06 +0200)
Use cctype.h for locale-independent string comparison and to avoid
tricky string conversions like in tr_TR locales.

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/context.c
libfdisk/src/label.c
libfdisk/src/parttype.c
libfdisk/src/script.c

index 2bf9e2ecd5104b438d2993a8a16e7ab9bff4dc1c..3b2a4d25fa02311b0d77cbb9b69bd6f4c6897892 100644 (file)
@@ -2,6 +2,7 @@
 # include <blkid.h>
 #endif
 
+#include "cctype.h"
 #include "blkdev.h"
 #ifdef __linux__
 # include "partx.h"
@@ -168,9 +169,9 @@ struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent,
        }
 
        if (name) {
-               if (strcasecmp(name, "bsd") == 0)
+               if (c_strcasecmp(name, "bsd") == 0)
                        lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
-               else if (strcasecmp(name, "dos") == 0 || strcasecmp(name, "mbr") == 0)
+               else if (c_strcasecmp(name, "dos") == 0 || c_strcasecmp(name, "mbr") == 0)
                        lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt);
        }
 
@@ -227,12 +228,12 @@ struct fdisk_label *fdisk_get_label(struct fdisk_context *cxt, const char *name)
        if (!name)
                return cxt->label;
 
-       if (strcasecmp(name, "mbr") == 0)
+       if (c_strcasecmp(name, "mbr") == 0)
                name = "dos";
 
        for (i = 0; i < cxt->nlabels; i++)
                if (cxt->labels[i]
-                   && strcasecmp(cxt->labels[i]->name, name) == 0)
+                   && c_strcasecmp(cxt->labels[i]->name, name) == 0)
                        return cxt->labels[i];
 
        DBG(CXT, ul_debugobj(cxt, "failed to found %s label driver", name));
index 3b6a614411e94eb9b2826c4c39b1502c31bd0f49..04afe8d26cb3f6609f7bdb7aa0bf92cb6c886a05 100644 (file)
@@ -1,5 +1,6 @@
 
 #include "fdiskP.h"
+#include "cctype.h"
 
 
 /**
@@ -236,7 +237,7 @@ const struct fdisk_field *fdisk_label_get_field_by_name(
        assert(name);
 
        for (i = 0; i < lb->nfields; i++) {
-               if (lb->fields[i].name && strcasecmp(lb->fields[i].name, name) == 0)
+               if (lb->fields[i].name && c_strcasecmp(lb->fields[i].name, name) == 0)
                        return &lb->fields[i];
        }
 
index 85669327cd4b2972e3fe8f038ee151cc7d357fd1..8e36ada74c7d64b9c596f511fad61f00ef3dc2f0 100644 (file)
@@ -2,6 +2,7 @@
 #include <ctype.h>
 
 #include "fdiskP.h"
+#include "cctype.h"
 #include "strutils.h"
 
 /**
@@ -264,7 +265,7 @@ struct fdisk_parttype *fdisk_label_get_parttype_from_string(
 
        for (i = 0; i < lb->nparttypes; i++)
                if (lb->parttypes[i].typestr
-                   && strcasecmp(lb->parttypes[i].typestr, str) == 0)
+                   && c_strcasecmp(lb->parttypes[i].typestr, str) == 0)
                        return (struct fdisk_parttype *)&lb->parttypes[i];
 
        return NULL;
index 652b14ed3c0fae449d28f1ac798e791f4d307158..6bb642f0213338dc705ec39b125e15c7b6956033 100644 (file)
@@ -1,4 +1,5 @@
 
+#include "cctype.h"
 #include "fdiskP.h"
 #include "strutils.h"
 #include "carefulputc.h"
@@ -239,7 +240,7 @@ static struct fdisk_scriptheader *script_get_header(struct fdisk_script *dp,
        list_for_each(p, &dp->headers) {
                struct fdisk_scriptheader *fi = list_entry(p, struct fdisk_scriptheader, headers);
 
-               if (strcasecmp(fi->name, name) == 0)
+               if (c_strcasecmp(fi->name, name) == 0)
                        return fi;
        }
 
@@ -1165,41 +1166,41 @@ static int parse_line_nameval(struct fdisk_script *dp, char *s)
                DBG(SCRIPT, ul_debugobj(dp, " parsing '%s'", p));
                p = (char *) skip_blank(p);
 
-               if (!strncasecmp(p, "start=", 6)) {
+               if (!c_strncasecmp(p, "start=", 6)) {
                        p += 6;
                        rc = parse_start_value(dp, pa, &p);
 
-               } else if (!strncasecmp(p, "size=", 5)) {
+               } else if (!c_strncasecmp(p, "size=", 5)) {
                        p += 5;
                        rc = parse_size_value(dp, pa, &p);
 
-               } else if (!strncasecmp(p, "bootable", 8)) {
+               } else if (!c_strncasecmp(p, "bootable", 8)) {
                        /* we use next_token() to skip possible extra space */
                        char *tk = next_token(&p);
-                       if (tk && strcasecmp(tk, "bootable") == 0)
+                       if (tk && c_strcasecmp(tk, "bootable") == 0)
                                pa->boot = 1;
                        else
                                rc = -EINVAL;
 
-               } else if (!strncasecmp(p, "attrs=", 6)) {
+               } else if (!c_strncasecmp(p, "attrs=", 6)) {
                        p += 6;
                        free(pa->attrs);
                        rc = next_string(&p, &pa->attrs);
 
-               } else if (!strncasecmp(p, "uuid=", 5)) {
+               } else if (!c_strncasecmp(p, "uuid=", 5)) {
                        p += 5;
                        free(pa->uuid);
                        rc = next_string(&p, &pa->uuid);
 
-               } else if (!strncasecmp(p, "name=", 5)) {
+               } else if (!c_strncasecmp(p, "name=", 5)) {
                        p += 5;
                        free(pa->name);
                        rc = next_string(&p, &pa->name);
                        if (!rc)
                                unhexmangle_string(pa->name);
 
-               } else if (!strncasecmp(p, "type=", 5) ||
-                          !strncasecmp(p, "Id=", 3)) {         /* backward compatibility */
+               } else if (!c_strncasecmp(p, "type=", 5) ||
+                          !c_strncasecmp(p, "Id=", 3)) {               /* backward compatibility */
                        char *type = NULL;
 
                        fdisk_unref_parttype(pa->type);