Skip to contentSkip to conversation

Net: rxrpc: signed and unsigned issue, need type cast for n_elem.

PATCH In review Chen Gang 2 participants 3 messages Apr 22, 2013 – Apr 23, 2013

Subsystems (3)
lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 Net: rxrpc: signed and unsigned issue, need type cast for n_elem.

n_elem is unsigned int which never < 0. but it seems, we realy need check it whether < 0. so need a type cast for it.

find it by EXTRA_CFLAGS=-W

---
 net/rxrpc/ar-key.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rxrpc/ar-key.c +1 -1
index 7633a75..ba0b722 100644
@@ -346,7 +346,7 @@ static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
 
 	n_elem = ntohl(*xdr++);
 	toklen -= 4;
-	if (n_elem < 0 || n_elem > max_n_elem)
+	if ((int)n_elem < 0 || n_elem > max_n_elem)
 		return -EINVAL;
 	*_n_elem = n_elem;
 	if (n_elem > 0) {
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 17 lines · Chen Gang

n_elem is unsigned int which never < 0.
but it seems, we realy need check it whether < 0. so need a type cast for it.

find it by EXTRA_CFLAGS=-W

---
 net/rxrpc/ar-key.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rxrpc/ar-key.c +0 -0
index 7633a75..ba0b722 100644

net/rxrpc/ar-key.c static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td, · line 349

Quoted 5 lines
net/rxrpc/ar-key.c +1 -1
@@ -346,7 +346,7 @@ static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
 
 	n_elem = ntohl(*xdr++);
 	toklen -= 4;
-	if (n_elem < 0 || n_elem > max_n_elem)
+	if ((int)n_elem < 0 || n_elem > max_n_elem)

(int)n_elem < 0 is equivalent to n_elem >= 0x80000000, and if that is true then n_elem > max_n_elem (because max_n_elem <= 0xff).

Ben.

Quoted 3 lines
net/rxrpc/ar-key.c +0 -0
 		return -EINVAL;
 	*_n_elem = n_elem;
 	if (n_elem > 0) {
Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#2 lore raw
Quoted 19 lines · Ben Hutchings
Quoted 18 lines · Chen Gang

n_elem is unsigned int which never < 0.
but it seems, we realy need check it whether < 0. so need a type cast for it.

find it by EXTRA_CFLAGS=-W

---
 net/rxrpc/ar-key.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
net/rxrpc/ar-key.c +0 -0
index 7633a75..ba0b722 100644
@@ -346,7 +346,7 @@ static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
 
Quoted 8 lines

net/rxrpc/ar-key.c static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td, · line 349

Quoted 4 lines
net/rxrpc/ar-key.c +1 -1
 	n_elem = ntohl(*xdr++);
 	toklen -= 4;
-	if (n_elem < 0 || n_elem > max_n_elem)
+	if ((int)n_elem < 0 || n_elem > max_n_elem)

(int)n_elem < 0 is equivalent to n_elem >= 0x80000000, and if that is true then n_elem > max_n_elem (because max_n_elem <= 0xff).

ok, thanks, my subject and the comments are incorrect.

but I still prefer to improve the original code like below.
(also need change the subject and the comments)

----------------------diff begin------------------------------------------------

net/rxrpc/ar-key.c +1 -1
index 7633a75..cde682f 100644
@@ -346,7 +346,7 @@ static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
 
 	n_elem = ntohl(*xdr++);
 	toklen -= 4;
-	if (n_elem < 0 || n_elem > max_n_elem)
+	if (n_elem > max_n_elem)
 		return -EINVAL;
 	*_n_elem = n_elem;
 	if (n_elem > 0) {

----------------------diff end--------------------------------------------------

Quoted 6 lines

Ben.

Quoted 3 lines
net/rxrpc/ar-key.c +0 -0
 		return -EINVAL;
 	*_n_elem = n_elem;
 	if (n_elem > 0) {
Signature · 3 lines
Chen Gang

Asianux Corporation