From 3aa3ccb2bb1c8804fbf43b260c93b65e831242c1 Mon Sep 17 00:00:00 2001                                                        
From: cgzones <cgzones@googlemail.com>
Date: Thu, 2 Nov 2023 21:20:40 +0100
Subject: [PATCH] lib: cast to unsigned char for character test functions
 (#338)

Passing a value not representable by unsigned char is undefined
behavior.

Reference:https://github.com/linux-audit/audit-userspace/commit/3aa3ccb2bb1c8804fbf43b260c93b65e831242c1
Conflict:lib/libaudit.c

---
 lib/libaudit.c     | 30 +++++++++++++++---------------
 lib/lookup_table.c |  2 +-
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/lib/libaudit.c b/lib/libaudit.c
index 02c263e..5843ac0 100644
--- a/lib/libaudit.c
+++ b/lib/libaudit.c
@@ -1006,7 +1006,7 @@ int audit_rule_syscallbyname_data(struct audit_rule_data *rule,
 		return -2;
 	nr = audit_name_to_syscall(scall, machine);
 	if (nr < 0) {
-		if (isdigit(scall[0]))
+		if (isdigit((unsigned char)scall[0]))
 			nr = strtol(scall, NULL, 0);
 	}
 	if (nr >= 0)
@@ -1535,11 +1535,11 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 		case AUDIT_OBJ_UID:
 			// Do positive & negative separate for 32 bit systems
 			vlen = strlen(v);
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtoul(v, NULL, 0);
 			else if (vlen >= 2 && *(v)=='-' &&
-						(isdigit((char)*(v+1))))
+						(isdigit((unsigned char)*(v+1))))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else {
@@ -1559,7 +1559,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 		case AUDIT_SGID:
 		case AUDIT_FSGID:
 		case AUDIT_OBJ_GID:
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else {
@@ -1575,11 +1575,11 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 			if (flags != AUDIT_FILTER_EXIT)
 				return -EAU_EXITONLY;
 			vlen = strlen(v);
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else if (vlen >= 2 && *(v)=='-' &&
-						(isdigit((char)*(v+1))))
+						(isdigit((unsigned char)*(v+1))))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else {
@@ -1594,7 +1594,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 					flags != AUDIT_FILTER_USER)
 				return -EAU_MSGTYPEEXCLUDEUSER;
 
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else
@@ -1665,7 +1665,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 				return -EAU_ARCHMISPLACED;
 			if (!(op == AUDIT_NOT_EQUAL || op == AUDIT_EQUAL))
 				return -EAU_OPEQNOTEQ;
-			if (isdigit((char)*(v))) {
+			if (isdigit((unsigned char)*(v))) {
 				int machine;
 
 				errno = 0;
@@ -1706,7 +1706,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 					return -EAU_STRTOOLONG;
 
 				for (i = 0; i < len; i++) {
-					switch (tolower(v[i])) {
+					switch (tolower((unsigned char)v[i])) {
 						case 'r':
 							val |= AUDIT_PERM_READ;
 							break;
@@ -1740,7 +1740,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 				return -EAU_FIELDUNAVAIL;
 			if (!(op == AUDIT_NOT_EQUAL || op == AUDIT_EQUAL))
 				return -EAU_OPEQNOTEQ;
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtoul(v, NULL, 0);
 			else
@@ -1753,11 +1753,11 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 			break;
 		case AUDIT_ARG0...AUDIT_ARG3:
 			vlen = strlen(v);
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtoul(v, NULL, 0);
 			else if (vlen >= 2 && *(v)=='-' &&
-						(isdigit((char)*(v+1))))
+						(isdigit((unsigned char)*(v+1))))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else
@@ -1773,11 +1773,11 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 				return -EAU_FIELDNOFILTER;
 			// Do positive & negative separate for 32 bit systems
 			vlen = strlen(v);
-			if (isdigit((char)*(v)))
+			if (isdigit((unsigned char)*(v)))
 				rule->values[rule->field_count] =
 					strtoul(v, NULL, 0);
 			else if (vlen >= 2 && *(v)=='-' &&
-						(isdigit((char)*(v+1))))
+						(isdigit((unsigned char)*(v+1))))
 				rule->values[rule->field_count] =
 					strtol(v, NULL, 0);
 			else if (strcmp(v, "unset") == 0)
@@ -1803,7 +1803,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,
 			if (field == AUDIT_PPID && !(flags==AUDIT_FILTER_EXIT))
 				return -EAU_EXITONLY;
 
-			if (!isdigit((char)*(v)))
+			if (!isdigit((unsigned char)*(v)))
 				return -EAU_FIELDVALNUM;
 
 			if (field == AUDIT_INODE)
diff --git a/lib/lookup_table.c b/lib/lookup_table.c
index 23678a4..d97c5fb 100644
--- a/lib/lookup_table.c
+++ b/lib/lookup_table.c
@@ -223,7 +223,7 @@ int audit_name_to_msg_type(const char *msg_type)
 		strncpy(buf, msg_type + 8, len);
 		errno = 0;
 		return strtol(buf, NULL, 10);
-	} else if (isdigit(*msg_type)) {
+	} else if (isdigit((unsigned char)*msg_type)) {
 		errno = 0;
 		return strtol(msg_type, NULL, 10);
 	}
-- 
2.33.0

