diff mbox series

wpa_supplicant: Fix arithmetic on void pointer

Message ID 20191008201154.10650-1-jesus.manzano@galgus.net
State Superseded
Headers show
Series wpa_supplicant: Fix arithmetic on void pointer | expand

Commit Message

Jesus Fernandez Manzano Oct. 8, 2019, 8:11 p.m. UTC
When using void pointers in calculations, the behaviour is undefined.
Arithmetic operations on 'void *' is a GNU C extension,
which defines the 'sizeof(void)' to be 1.

This change improves portability of the code.

Signed-off-by: Jesus Fernandez Manzano <jesus.manzano@galgus.net>
---
 wpa_supplicant/wpa_priv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Johannes Berg Oct. 8, 2019, 8:20 p.m. UTC | #1
On Tue, 2019-10-08 at 22:11 +0200, Jesus Fernandez Manzano wrote:
> When using void pointers in calculations, the behaviour is undefined.
> Arithmetic operations on 'void *' is a GNU C extension,
> which defines the 'sizeof(void)' to be 1.

I'm curious, do you have a compiler that tells you about this or
something? Or how else did you find it?

johannes
Markus Theil Oct. 8, 2019, 11:17 p.m. UTC | #2
Adding -Wpointer-arith to CFLAGS does the trick for gcc and clang.

Markus

On 10/8/19 10:20 PM, Johannes Berg wrote:
> On Tue, 2019-10-08 at 22:11 +0200, Jesus Fernandez Manzano wrote:
>> When using void pointers in calculations, the behaviour is undefined.
>> Arithmetic operations on 'void *' is a GNU C extension,
>> which defines the 'sizeof(void)' to be 1.
> I'm curious, do you have a compiler that tells you about this or
> something? Or how else did you find it?
>
> johannes
>
>
> _______________________________________________
> Hostap mailing list
> Hostap@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/hostap
Jesus Fernandez Manzano Oct. 9, 2019, 7:50 a.m. UTC | #3
As Markus said, using GCC with -Wpointer-arith tells you the issue.

Jesus.
Jesus Fernandez Manzano Oct. 9, 2019, 7:52 a.m. UTC | #4
I'm sending a v2 version to fix a problem with signedness.

Jesus.
diff mbox series

Patch

diff --git a/wpa_supplicant/wpa_priv.c b/wpa_supplicant/wpa_priv.c
index b3ad45eca..f59788d38 100644
--- a/wpa_supplicant/wpa_priv.c
+++ b/wpa_supplicant/wpa_priv.c
@@ -598,7 +598,7 @@  static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
 	}
 
 	dst_addr = buf;
-	os_memcpy(&proto, buf + ETH_ALEN, 2);
+	os_memcpy(&proto, (char *) buf + ETH_ALEN, 2);
 
 	if (!wpa_priv_allowed_l2_proto(proto)) {
 		wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
@@ -607,7 +607,7 @@  static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
 	}
 
 	res = l2_packet_send(iface->l2[idx], dst_addr, proto,
-			     buf + ETH_ALEN + 2, len - ETH_ALEN - 2);
+			     (char *) buf + ETH_ALEN + 2, len - ETH_ALEN - 2);
 	wpa_printf(MSG_DEBUG, "L2 send[idx=%d]: res=%d", idx, res);
 }