Skip to contentSkip to conversation

adjust array index

PATCH Applied Julia Lawall 4 participants 10 messages Jul 28, 2011 – Aug 1, 2011

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

cover adjust array index

These patches fix cases where the upper bound of a loop is used as an array indes, while it appears that the loop index should be used as the array index. I have only guessed that the code is incorrect, not actually tested any of it.

#1 lore raw

PATCH 1/6 net: adjust array index

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

---
Not tested.

 net/ipv4/igmp.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/igmp.c +1 -1
diff -u -p a/net/ipv4/igmp.c b/net/ipv4/igmp.c
@@ -1718,7 +1718,7 @@ static int ip_mc_add_src(struct in_devic
 
 		pmc->sfcount[sfmode]--;
 		for (j=0; j<i; j++)
-			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
+			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
 	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
 #ifdef CONFIG_IP_MULTICAST
 		struct ip_sf_list *psf;
maintainer Applied
#2 lore raw

Re: [PATCH 1/6] net: adjust array index

From: Julia Lawall <[email protected]>
Date: Thu, 28 Jul 2011 14:46:01 +0200

Quoted 20 lines

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

Quoted 1 line

Applied.

#3 lore raw

PATCH 2/6 drivers/media/dvb/dvb-usb/usb-urb.c: adjust array index

In reply to Julia Lawall

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

---
Not tested.

 drivers/media/dvb/dvb-usb/usb-urb.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/dvb/dvb-usb/usb-urb.c +2 -2
diff -u -p a/drivers/media/dvb/dvb-usb/usb-urb.c b/drivers/media/dvb/dvb-usb/usb-urb.c
@@ -148,7 +148,7 @@ static int usb_bulk_urb_init(struct usb_
 		if (!stream->urb_list[i]) {
 			deb_mem("not enough memory for urb_alloc_urb!.\n");
 			for (j = 0; j < i; j++)
-				usb_free_urb(stream->urb_list[i]);
+				usb_free_urb(stream->urb_list[j]);
 			return -ENOMEM;
 		}
 		usb_fill_bulk_urb( stream->urb_list[i], stream->udev,
@@ -181,7 +181,7 @@ static int usb_isoc_urb_init(struct usb_
 		if (!stream->urb_list[i]) {
 			deb_mem("not enough memory for urb_alloc_urb!\n");
 			for (j = 0; j < i; j++)
-				usb_free_urb(stream->urb_list[i]);
+				usb_free_urb(stream->urb_list[j]);
 			return -ENOMEM;
 		}
 
#4 lore raw

PATCH 3/6 drivers/net/niu.c: adjust array index

In reply to Julia Lawall

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

---
Not tested.

 drivers/net/niu.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/niu.c +2 -2
diff -u -p a/drivers/net/niu.c b/drivers/net/niu.c
@@ -9201,7 +9201,7 @@ static int __devinit niu_ldg_init(struct
 
 	first_chan = 0;
 	for (i = 0; i < port; i++)
-		first_chan += parent->rxchan_per_port[port];
+		first_chan += parent->rxchan_per_port[i];
 	num_chan = parent->rxchan_per_port[port];
 
 	for (i = first_chan; i < (first_chan + num_chan); i++) {
@@ -9217,7 +9217,7 @@ static int __devinit niu_ldg_init(struct
 
 	first_chan = 0;
 	for (i = 0; i < port; i++)
-		first_chan += parent->txchan_per_port[port];
+		first_chan += parent->txchan_per_port[i];
 	num_chan = parent->txchan_per_port[port];
 	for (i = first_chan; i < (first_chan + num_chan); i++) {
 		err = niu_ldg_assign_ldn(np, parent,
maintainer Applied
#5 lore raw

Re: [PATCH 3/6] drivers/net/niu.c: adjust array index

From: Julia Lawall <[email protected]>
Date: Thu, 28 Jul 2011 14:46:03 +0200

Quoted 20 lines

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

Quoted 1 line

Applied.

#6 lore raw

PATCH 4/6 drivers/staging/ath6kl/miscdrv/ar3kps/ar3kpsparser.c: adjust array index

In reply to Julia Lawall

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

---
Not tested.

 drivers/staging/ath6kl/miscdrv/ar3kps/ar3kpsparser.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/ath6kl/miscdrv/ar3kps/ar3kpsparser.c +1 -1
diff -u -p a/drivers/staging/ath6kl/miscdrv/ar3kps/ar3kpsparser.c b/drivers/staging/ath6kl/miscdrv/ar3kps/ar3kpsparser.c
@@ -812,7 +812,7 @@ int AthCreateCommandList(struct ps_cmd_p
     for(count = 0; count < Patch_Count; count++) {
 
         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Freeing Patch Buffer %d \r\n",count));
-        kfree(RamPatch[Patch_Count].Data);
+	kfree(RamPatch[count].Data);
     }
 
     for(count = 0; count < Tag_Count; count++) {
#7 lore raw

PATCH 5/6 sound/core/pcm_compat.c: adjust array index

In reply to Julia Lawall

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

---
Not tested.

 sound/core/pcm_compat.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_compat.c +1 -1
diff -u -p a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c
@@ -342,7 +342,7 @@ static int snd_pcm_ioctl_xfern_compat(st
 			kfree(bufs);
 			return -EFAULT;
 		}
-		bufs[ch] = compat_ptr(ptr);
+		bufs[i] = compat_ptr(ptr);
 		bufptr++;
 	}
 	if (dir = SNDRV_PCM_STREAM_PLAYBACK)
Applied
#8 lore raw

Re: [PATCH 5/6] sound/core/pcm_compat.c: adjust array index

At Thu, 28 Jul 2011 14:46:05 +0200,

Quoted 21 lines · Julia Lawall

From: Julia Lawall <[email protected]>

Convert array index from the loop bound to the loop index.

A simplified version of the semantic patch that fixes this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression e1,e2,ar;
@@

for(e1 = 0; e1 < e2; e1++) { <...
ar[
- e2
+ e1
]
...> }
// </smpl>

Quoted 1 line

Thanks, applied now.

Takashi

Quoted 20 lines

---
Not tested.

 sound/core/pcm_compat.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff -u -p a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c

sound/core/pcm_compat.c +1 -1
@@ -342,7 +342,7 @@ static int snd_pcm_ioctl_xfern_compat(st
 			kfree(bufs);
 			return -EFAULT;
 		}
-		bufs[ch] = compat_ptr(ptr);
+		bufs[i] = compat_ptr(ptr);
 		bufptr++;
 	}
 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
#9 lore raw
In reply to Julia Lawall
Quoted 3 lines · Julia Lawall

These patches fix cases where the upper bound of a loop is used as an array index, while it appears that the loop index should be used as the array index.

Good one.