From: Karel Zak <kzak@redhat.com>
Date: Tue, 1 Apr 2025 17:45:01 +0200
Subject: libfdisk: avoid strcasecmp() for ASCII-only strings

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>
(cherry picked from commit 8ec0c305e9f6ab904119201a13873faedb30da57)
---
 libfdisk/src/context.c  |  9 +++++----
 libfdisk/src/label.c    |  3 ++-
 libfdisk/src/parttype.c |  3 ++-
 libfdisk/src/script.c   | 21 +++++++++++----------
 4 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 2bf9e2e..3b2a4d2 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -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));
diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c
index 3b6a614..04afe8d 100644
--- a/libfdisk/src/label.c
+++ b/libfdisk/src/label.c
@@ -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];
 	}
 
diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c
index 8566932..8e36ada 100644
--- a/libfdisk/src/parttype.c
+++ b/libfdisk/src/parttype.c
@@ -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;
diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c
index 652b14e..6bb642f 100644
--- a/libfdisk/src/script.c
+++ b/libfdisk/src/script.c
@@ -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);
