[klibc] [PATCH] nfsmount: defeat dummypmap compiler warning
Greg Thelen
gthelen at google.com
Tue Feb 28 19:50:33 PST 2012
maximilian attems <max at stro.at> writes:
> On Wed, 11 Jan 2012, Greg Thelen wrote:
>
>> Without this patch gcc 4.6 warns:
>> KLIBCCC usr/kinit/nfsmount/dummypmap.o
>> usr/kinit/nfsmount/dummypmap.c: In function 'dummy_portmap':
>> usr/kinit/nfsmount/dummypmap.c:191:13: warning: array subscript is below array bounds [-Warray-bounds]
>>
>> Signed-off-by: Greg Thelen <gthelen at google.com>
>> ---
>> usr/kinit/nfsmount/dummypmap.c | 2 +-
>> 1 files changed, 1 insertions(+), 1 deletions(-)
>>
>> diff --git a/usr/kinit/nfsmount/dummypmap.c b/usr/kinit/nfsmount/dummypmap.c
>> index 7c5b6bc..7c4da35 100644
>> --- a/usr/kinit/nfsmount/dummypmap.c
>> +++ b/usr/kinit/nfsmount/dummypmap.c
>> @@ -150,7 +150,7 @@ static int dummy_portmap(int sock, FILE *portmap_file)
>> int pktlen, addrlen;
>> unsigned char pkt[65536]; /* Max UDP packet size */
>> /* RPC UDP packets do not include TCP fragment size */
>> - struct rpc_call *rpc = (struct rpc_call *) &pkt[-4];
>> + struct rpc_call *rpc = (struct rpc_call *)((uintptr_t)pkt - 4);
>> struct rpc_auth *cred;
>> struct rpc_auth *vrf;
>> struct portmap_args *args;
>
> hmm suspect this looks like papering, please
> hpa has the following suggestion:
> "I would actually be happier making pkt a union/structure with a (dummy)
> fragment size header
> Wasting 4 bytes on the stack is not an issue here
> I'd actually suggest we do:
> union {
> struct rpc_call rpc;
> char pkt[65536+4];
> } pkt;
> And just load the packet at &rpc.hdr.udp"
>
>
> thanks.
Is this what you are suggesting?
>From 87fae6051aa4b30643c964eb83f7d54a258f5a68 Mon Sep 17 00:00:00 2001
From: Greg Thelen <gthelen at google.com>
Date: Wed, 4 Jan 2012 17:02:09 -0800
Subject: [PATCH] [klibc] nfsmount: defeat dummypmap compiler warning
Without this patch gcc 4.6 warns:
KLIBCCC usr/kinit/nfsmount/dummypmap.o
usr/kinit/nfsmount/dummypmap.c: In function 'dummy_portmap':
usr/kinit/nfsmount/dummypmap.c:191:13: warning: array subscript is below array bounds [-Warray-bounds]
Signed-off-by: Greg Thelen <gthelen at google.com>
---
usr/kinit/nfsmount/dummypmap.c | 20 +++++++++++++-------
1 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/usr/kinit/nfsmount/dummypmap.c b/usr/kinit/nfsmount/dummypmap.c
index 7c5b6bc..2cc889a 100644
--- a/usr/kinit/nfsmount/dummypmap.c
+++ b/usr/kinit/nfsmount/dummypmap.c
@@ -148,9 +148,13 @@ static int dummy_portmap(int sock, FILE *portmap_file)
{
struct sockaddr_in sin;
int pktlen, addrlen;
- unsigned char pkt[65536]; /* Max UDP packet size */
- /* RPC UDP packets do not include TCP fragment size */
- struct rpc_call *rpc = (struct rpc_call *) &pkt[-4];
+ enum { max_packet_sz = 65536 };
+ union {
+ struct rpc_call rpc;
+ /* Max UDP packet size + unused TCP fragment size */
+ char payload[max_packet_sz + offsetof(struct rpc_header, udp)];
+ } __attribute__ ((__packed__)) pkt;
+ struct rpc_call *rpc = &pkt.rpc;
struct rpc_auth *cred;
struct rpc_auth *vrf;
struct portmap_args *args;
@@ -158,8 +162,8 @@ static int dummy_portmap(int sock, FILE *portmap_file)
for (;;) {
addrlen = sizeof sin;
- pktlen = recvfrom(sock, &pkt, sizeof pkt, 0,
- (struct sockaddr *)&sin, &addrlen);
+ pktlen = recvfrom(sock, &rpc->hdr.udp, max_packet_sz,
+ 0, (struct sockaddr *)&sin, &addrlen);
if (pktlen < 0) {
if (errno == EINTR)
@@ -189,9 +193,11 @@ static int dummy_portmap(int sock, FILE *portmap_file)
} else if (rpc->prog_vers != htonl(2)) {
rply.rpc.reply_state = htonl(PROG_MISMATCH);
} else if (!(vrf = get_auth(cred)) ||
- (char *)vrf > (char *)pkt + pktlen - 8 - sizeof(*args) ||
+ (char *)vrf > ((char *)&rpc->hdr.udp + pktlen - 8 -
+ sizeof(*args)) ||
!(args = get_auth(vrf)) ||
- (char *)args > (char *)pkt + pktlen - sizeof(*args) ||
+ (char *)args > ((char *)&rpc->hdr.udp + pktlen -
+ sizeof(*args)) ||
check_cred(cred) || check_vrf(vrf)) {
/* Can't deal with credentials data; the kernel
won't send them */
--
1.7.7.3
More information about the klibc
mailing list