Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Mar 2020 23:15:04 +0100
From:      Dimitry Andric <dim@FreeBSD.org>
To:        Gleb Smirnoff <glebius@FreeBSD.org>
Cc:        Slawa Olhovchenkov <slw@zxy.spb.ru>, svn-src-head <svn-src-head@freebsd.org>, svn-src-all <svn-src-all@freebsd.org>, src-committers@freebsd.org
Subject:   Re: svn commit: r358655 - head/sbin/mount_nfs
Message-ID:  <CAD43B77-A0F0-45BA-BB24-2FEFA154A436@FreeBSD.org>
In-Reply-To: <20200305210114.GX5741@FreeBSD.org>
References:  <202003042227.024MRGsW072613@repo.freebsd.org> <244e28ea-1217-cefc-354e-02ecb201637a@FreeBSD.org> <20200305163350.GU5741@FreeBSD.org> <20200305172454.GJ8028@zxy.spb.ru> <20200305173515.GK8028@zxy.spb.ru> <20200305174411.GV5741@FreeBSD.org> <1A13B93C-7D87-4210-AD3D-2B198C166CE1@FreeBSD.org> <20200305183041.GU8012@zxy.spb.ru> <20200305210114.GX5741@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_06DCACBE-2866-4643-AE54-B08F4BA952F8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

On 5 Mar 2020, at 22:01, Gleb Smirnoff <glebius@FreeBSD.org> wrote:
>=20
> On Thu, Mar 05, 2020 at 09:30:41PM +0300, Slawa Olhovchenkov wrote:
> S> > > On Thu, Mar 05, 2020 at 08:35:15PM +0300, Slawa Olhovchenkov =
wrote:
> S> > > S> > > D> sbin/mount_nfs/mount_nfs.c:549:10: error: cast from =
'char *' to 'struct
> S> > > S> > > D> if_msghdr *' increases required alignment from 1 to 4
> S> > > S> > > D> [-Werror,-Wcast-align]
> S> > > S> > > D>                         ifm =3D (struct if_msghdr =
*)buf;
> S> > > S> > > D>                               ^~~~~~~~~~~~~~~~~~~~~~~
> S> > > S> > > D> 1 error generated.
> S> > > S> > > D>
> S> > > S> > > D> In practice I don't think the buffer can ever get =
misaligned, so can you
> S> > > S> > > D> please add a NO_WCAST_ALIGN=3D to the Makefile?
> S> > > S> > >
> S> > > S> > > route(8) handles the same problem via intermediate (void =
*) cast. What is
> S> > > S> > > preferred way to solve the problem? Change compiler =
flags file wide, or
> S> > > S> > > just through (void *) cast?
> S> > > S> >
> S> > > S> > Copy to aligned buffer or got SIGBUS on some =
architectures?
> S> > > S>
> S> > > S> char buf[2048] __aligned(__alignof(struct if_msghdr));
> S> > > S>
> S> > > S> resolve this watning.
> S> > >
> S> > > Thanks, Slawa! I think this is the most elegant solution.
> S> >
> S> > Why don't just declare the buffer as:
> S> >
> S> >   struct if_msghdr buf;
> S> >
> S> > and then do:
> S> >
> S> >   nread =3D read(s, &buf, sizeof buf);
> S> >
> S> > ?  You are never reading more than one if_msghdr anyway, and then =
there
> S> > is no need to cast anything.
> S>
> S> My inspiration: route socket can return other messages (man 4 =
route)
>=20
> Yes, exactly. We don't know what size next datagram is going to be.

Oh, in that case this code seems completely wrong.  How do you know the
full datagram will be delivered with one read() call?

If it always is, then there is no need to read more than the size of
struct if_msghdr, since you are not using any data beyond it.  So in
that case, you can suffice with read(..., sizeof(if_msghdr)).

If the read() call will return partial results, you must repeatedly call
it in a loop, until you either reach EOF, or have enough data. In that
case, a buffer with the size of if_msghdr is also enough, since you
never need to read beyond that.

-Dimitry


--Apple-Mail=_06DCACBE-2866-4643-AE54-B08F4BA952F8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.2

iF0EARECAB0WIQR6tGLSzjX8bUI5T82wXqMKLiCWowUCXmF56AAKCRCwXqMKLiCW
o8u/AJ4gMCatiYthXVdvDYPlt+MuNcHd4QCfWsm9p65HeS1zqopZ0sysOH9eatc=
=4Ny0
-----END PGP SIGNATURE-----

--Apple-Mail=_06DCACBE-2866-4643-AE54-B08F4BA952F8--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAD43B77-A0F0-45BA-BB24-2FEFA154A436>