[LEDE-DEV] [PATCH] libubox: Fix -Wsign-compare warnings

Rosen Penev rosenp at gmail.com
Tue Mar 21 19:58:18 PDT 2017


-Wsign-compare is part of the -Wextra series of warnings so it's worth fixing. The issues are mainly sign conversion ones.
---
 base64.c       |  6 +++---
 blob.c         | 12 ++++++------
 blob.h         |  8 ++++----
 blobmsg.c      |  2 +-
 blobmsg_json.c |  4 ++--
 jshn.c         |  2 +-
 json_script.c  | 16 +++++++++-------
 kvlist.c       |  2 +-
 8 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/base64.c b/base64.c
index 4186ce8..f4cb5c6 100644
--- a/base64.c
+++ b/base64.c
@@ -142,7 +142,6 @@ int b64_encode(const void *_src, size_t srclength,
 	size_t datalength = 0;
 	u_char input[3] = {0};
 	u_char output[4];
-	int i;
 
 	while (2 < srclength) {
 		input[0] = *src++;
@@ -167,7 +166,7 @@ int b64_encode(const void *_src, size_t srclength,
 	if (0 != srclength) {
 		/* Get what's left. */
 		input[0] = input[1] = input[2] = '\0';
-		for (i = 0; i < srclength; i++)
+		for (size_t i = 0; i < srclength; i++)
 			input[i] = *src++;
 
 		output[0] = input[0] >> 2;
@@ -200,7 +199,8 @@ int b64_decode(const void *_src, void *dest, size_t targsize)
 {
 	const char *src = _src;
 	unsigned char *target = dest;
-	int tarindex, state, ch;
+	int state, ch;
+	size_t tarindex;
 	u_char nextbyte;
 	char *pos;
 
diff --git a/blob.c b/blob.c
index 03d5e9c..45c3204 100644
--- a/blob.c
+++ b/blob.c
@@ -186,7 +186,7 @@ blob_nest_end(struct blob_buf *buf, void *cookie)
 	buf->head = attr;
 }
 
-static const int blob_type_minlen[BLOB_ATTR_LAST] = {
+static const size_t blob_type_minlen[BLOB_ATTR_LAST] = {
 	[BLOB_ATTR_STRING] = 1,
 	[BLOB_ATTR_INT8] = sizeof(uint8_t),
 	[BLOB_ATTR_INT16] = sizeof(uint16_t),
@@ -222,18 +222,18 @@ blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_at
 {
 	struct blob_attr *pos;
 	int found = 0;
-	int rem;
+	unsigned int rem;
 
 	memset(data, 0, sizeof(struct blob_attr *) * max);
 	blob_for_each_attr(pos, attr, rem) {
-		int id = blob_id(pos);
-		int len = blob_len(pos);
+		unsigned int id = blob_id(pos);
+		unsigned int len = blob_len(pos);
 
-		if (id >= max)
+		if ((int)id >= max)
 			continue;
 
 		if (info) {
-			int type = info[id].type;
+			unsigned int type = info[id].type;
 
 			if (type < BLOB_ATTR_LAST) {
 				if (!blob_check_type(blob_data(pos), len, type))
diff --git a/blob.h b/blob.h
index a092f5d..3f069da 100644
--- a/blob.h
+++ b/blob.h
@@ -154,25 +154,25 @@ blob_get_u64(const struct blob_attr *attr)
 static inline int8_t
 blob_get_int8(const struct blob_attr *attr)
 {
-	return blob_get_u8(attr);
+	return (int8_t)blob_get_u8(attr);
 }
 
 static inline int16_t
 blob_get_int16(const struct blob_attr *attr)
 {
-	return blob_get_u16(attr);
+	return (int16_t)blob_get_u16(attr);
 }
 
 static inline int32_t
 blob_get_int32(const struct blob_attr *attr)
 {
-	return blob_get_u32(attr);
+	return (int32_t)blob_get_u32(attr);
 }
 
 static inline int64_t
 blob_get_int64(const struct blob_attr *attr)
 {
-	return blob_get_u64(attr);
+	return (int64_t)blob_get_u64(attr);
 }
 
 static inline const char *
diff --git a/blobmsg.c b/blobmsg.c
index c2bb717..1eddf23 100644
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -67,7 +67,7 @@ int blobmsg_check_array(const struct blob_attr *attr, int type)
 {
 	struct blob_attr *cur;
 	bool name;
-	int rem;
+	unsigned int rem;
 	int size = 0;
 
 	switch (blobmsg_type(attr)) {
diff --git a/blobmsg_json.c b/blobmsg_json.c
index ca9dd1a..c46890c 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -146,7 +146,7 @@ static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
 static void add_separator(struct strbuf *s)
 {
 	const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
-	int len;
+	size_t len;
 
 	if (!s->indent)
 		return;
@@ -280,7 +280,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i
 {
 	struct blob_attr *pos;
 	bool first = true;
-	int rem = len;
+	unsigned int rem = len;
 
 	blobmsg_puts(s, (array ? "[" : "{" ), 1);
 	s->indent_level++;
diff --git a/json_script.c b/json_script.c
index 463aac8..8fb082e 100644
--- a/json_script.c
+++ b/json_script.c
@@ -95,7 +95,7 @@ const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *
 				 const char *name)
 {
 	struct blob_attr *cur;
-	int rem;
+	unsigned int rem;
 
 	blobmsg_for_each_attr(cur, vars, rem) {
 		if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
@@ -164,7 +164,7 @@ static int handle_case(struct json_call *call, struct blob_attr *expr)
 {
 	struct blob_attr *tb[3], *cur;
 	const char *var;
-	int rem;
+	unsigned int rem;
 
 	json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, BLOBMSG_TYPE_TABLE);
 	if (!tb[1] || !tb[2])
@@ -233,7 +233,7 @@ static int expr_eq_regex(struct json_call *call, struct blob_attr *expr, bool re
 	struct json_script_ctx *ctx = call->ctx;
 	struct blob_attr *tb[3], *cur;
 	const char *var;
-	int rem;
+	unsigned int rem;
 
 	json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
 	if (!tb[1] || !tb[2])
@@ -277,7 +277,7 @@ static int handle_expr_has(struct json_call *call, struct blob_attr *expr)
 {
 	struct json_script_ctx *ctx = call->ctx;
 	struct blob_attr *tb[3], *cur;
-	int rem;
+	unsigned int rem;
 
 	json_get_tuple(expr, tb, 0, 0);
 	if (!tb[1])
@@ -306,7 +306,8 @@ static int handle_expr_has(struct json_call *call, struct blob_attr *expr)
 static int expr_and_or(struct json_call *call, struct blob_attr *expr, bool and)
 {
 	struct blob_attr *cur;
-	int ret, rem;
+	int ret;
+	unsigned int rem;
 	int i = 0;
 
 	blobmsg_for_each_attr(cur, expr, rem) {
@@ -513,7 +514,8 @@ static int cmd_process_strings(struct json_call *call, struct blob_attr *attr)
 	struct json_script_ctx *ctx = call->ctx;
 	struct blob_attr *cur;
 	int args = -1;
-	int rem, ret;
+	int ret;
+	unsigned int rem;
 	void *c;
 
 	blob_buf_init(&ctx->buf, 0);
@@ -570,7 +572,7 @@ static int json_process_cmd(struct json_call *call, struct blob_attr *block)
 {
 	struct json_script_ctx *ctx = call->ctx;
 	struct blob_attr *cur;
-	int rem;
+	unsigned int rem;
 	int ret;
 	int i = 0;
 
diff --git a/kvlist.c b/kvlist.c
index a7b6ea0..66fc8cd 100644
--- a/kvlist.c
+++ b/kvlist.c
@@ -75,7 +75,7 @@ bool kvlist_set(struct kvlist *kv, const char *name, const void *data)
 {
 	struct kvlist_node *node;
 	char *name_buf;
-	int len = kv->get_len(kv, data);
+	size_t len = kv->get_len(kv, data);
 
 	node = calloc_a(sizeof(struct kvlist_node) + len,
 		&name_buf, strlen(name) + 1);
-- 
2.9.3




More information about the Lede-dev mailing list