Blob Blame History Raw
From a62bcad4953223057c37a00d3bcdb051f2283578 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 9 Mar 2017 13:07:11 +0000
Subject: [PATCH 07/26] common/intutil.c: Avoid comparison between signed and
 unsigned.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

By using an unsigned char to hold the character we can avoid needing
to check for c < 0 and also avoid the comparison between signed and
unsigned integers.

In GCC the warning/error was:

intutil.c: In function ‘parse_uint32’:
intutil.c:249:63: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
   if (value > UINT32_MAX/10L || (value == UINT32_MAX/10L && c > UINT32_MAX%10))
                                                               ^
intutil.c: In function ‘parse_uint64’:
intutil.c:267:65: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
   if (value > UINT64_MAX/10LL || (value == UINT64_MAX/10LL && c > UINT64_MAX%10LL))
                                                                 ^

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 common/intutil.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/common/intutil.c b/common/intutil.c
index 57f8cb1..d500ba7 100644
--- a/common/intutil.c
+++ b/common/intutil.c
@@ -207,8 +207,8 @@ parse_uint8(const char *instr, uint8_t *outint)
 	uint8_t value = 0;
 
 	for (; *instr != '\0'; instr++) {
-		int8_t c = *instr - '0';
-		if (c < 0 || c > 9)
+		uint8_t c = *instr - '0';
+		if (c > 9)
 			return false;
 		if (value > UINT8_MAX/10 || (value == UINT8_MAX/10 && c > UINT8_MAX%10))
 			return false;
@@ -225,8 +225,8 @@ parse_uint16(const char *instr, uint16_t *outint)
 	uint16_t value = 0;
 
 	for (; *instr != '\0'; instr++) {
-		int8_t c = *instr - '0';
-		if (c < 0 || c > 9)
+		uint8_t c = *instr - '0';
+		if (c > 9)
 			return false;
 		if (value > UINT16_MAX/10 || (value == UINT16_MAX/10 && c > UINT16_MAX%10))
 			return false;
@@ -243,8 +243,8 @@ parse_uint32(const char *instr, uint32_t *outint)
 	uint32_t value = 0;
 
 	for (; *instr != '\0'; instr++) {
-		int8_t c = *instr - '0';
-		if (c < 0 || c > 9)
+		uint8_t c = *instr - '0';
+		if (c > 9)
 			return false;
 		if (value > UINT32_MAX/10L || (value == UINT32_MAX/10L && c > UINT32_MAX%10))
 			return false;
@@ -261,8 +261,8 @@ parse_uint64(const char *instr, uint64_t *outint)
 	uint64_t value = 0;
 
 	for (; *instr != '\0'; instr++) {
-		int8_t c = *instr - '0';
-		if (c < 0 || c > 9)
+		uint8_t c = *instr - '0';
+		if (c > 9)
 			return false;
 		if (value > UINT64_MAX/10LL || (value == UINT64_MAX/10LL && c > UINT64_MAX%10LL))
 			return false;
-- 
2.10.2