[pacman-dev] [PATCH 2/3] pacsort, introduce define for escape_char error code

Rikard Falkeborn rikard.falkeborn at gmail.com
Thu Dec 31 13:19:30 UTC 2015


The signedness of char is implementation defined. On systems where
char is unsigned, comparing a variable of type char with -1 is never
true, due to integer promotion rules. To avoid this, introduce a
define for invalid field separators where -1 is cast to char. This will
ensure that the return value check works for both unsigned and signed char.

Fixes one warning [-Wtype-limits] for comparissons with -1 when compiling
with -funsigned-char.

Signed-off-by: Rikard Falkeborn <rikard.falkeborn at gmail.com>
---
 src/util/pacsort.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/util/pacsort.c b/src/util/pacsort.c
index e7dc63e..dcf1ade 100644
--- a/src/util/pacsort.c
+++ b/src/util/pacsort.c
@@ -28,6 +28,7 @@
 #include "util-common.h"
 
 #define DELIM ' '
+#define INVALD_ESCAPE_CHAR ((char)-1)
 
 #ifndef MIN
 #define MIN(a, b)      \
@@ -385,13 +386,13 @@ static int vercmp(const void *p1, const void *p2)
 static char escape_char(const char *string)
 {
 	if(!string) {
-		return -1;
+		return INVALD_ESCAPE_CHAR;
 	}
 
 	const size_t len = strlen(string);
 
 	if(len > 2) {
-		return -1;
+		return INVALD_ESCAPE_CHAR;
 	}
 
 	if(len == 1) {
@@ -399,7 +400,7 @@ static char escape_char(const char *string)
 	}
 
 	if(*string != '\\') {
-		return -1;
+		return INVALD_ESCAPE_CHAR;
 	}
 
 	switch(string[1]) {
@@ -412,7 +413,7 @@ static char escape_char(const char *string)
 		case '0':
 			return '\0';
 		default:
-			return -1;
+			return INVALD_ESCAPE_CHAR;
 	}
 }
 
@@ -463,7 +464,7 @@ static int parse_options(int argc, char **argv)
 				break;
 			case 't':
 				opts.delim = escape_char(optarg);
-				if(opts.delim == -1) {
+				if(opts.delim == INVALD_ESCAPE_CHAR) {
 					fprintf(stderr, "error: invalid field separator -- `%s'\n", optarg);
 					return 1;
 				}
-- 
2.6.4


More information about the pacman-dev mailing list