Skip to contentSkip to conversation

Negative index in drivers/usb/host/isp116x-hcd.c

PATCH New Eric Sesterhenn 1 participant 1 message May 31, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Negative index in drivers/usb/host/isp116x-hcd.c

hi,

This fixes coverity Bug #390.

With the following code

	ret = ep->branch = balance(isp116x, ep->period, ep->load);
	if (ret < 0)
		goto fail;

the problem is that ret and balance are of the type int, and ep->branch is u16. so the int balance() returns gets reduced to u16 and then converted to an int again, which removes the sign. Maybe the following little c program can explain it better:

----snip----

int foo() {
	return -5;
}
int main(int argc, char **argv) {
	int a;
	unsigned short b;

	a = b = foo();
	if (a < 0)
		puts("case 1 works\n");

	b = a = foo();
	if (a < 0 )
		puts("case 2 works\n");
}

----snip----

only the case 2 output is visible.

drivers/usb/host/isp116x-hcd.c renamed from drivers/usb/host/isp116x-hcd.c.orig +1 -1
@@ -781,7 +781,7 @@ static int isp116x_urb_enqueue(struct us
 		if (ep->branch < PERIODIC_SIZE)
 			break;
 
-		ret = ep->branch = balance(isp116x, ep->period, ep->load);
+		ep->branch = ret = balance(isp116x, ep->period, ep->load);
 		if (ret < 0)
 			goto fail;
 		ret = 0;