Subject: Re: Dumb/silly network programming question
To: #List NetBSD current users <current-users@NetBSD.ORG>
From: Bill Studenmund <wrstuden@netbsd.org>
List: current-users
Date: 12/10/2003 15:06:25
--aM3YZ0Iwxop3KEKx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Dec 10, 2003 at 03:27:21AM +0100, Christian Biere wrote:
> Bill Studenmund wrote:
> > Then I get back the number of bytes read. If I get < 0, I got an error.
>=20
> Well, the same manpage also says:
>=20
> CAVEATS
>      Error checks should explicitly test for -1.  Code such as
>=20
>              while ((nr =3D read(fd, buf, sizeof(buf))) > 0)
>=20
>      is not maximally portable, as some platforms allow for nbytes to ran=
ge
>      between SSIZE_MAX and SIZE_MAX - 2, in which case the return value o=
f an
>      error-free read() may appear as a negative number distinct from -1.
>      Proper loops should use
>=20
>              while ((nr =3D read(fd, buf, sizeof(buf))) !=3D -1 && nr !=
=3D 0)

You are technically correct. However I will never pass a read size=20
greater than SSIZE_MAX - 1, so -1 is the only valid negative value for=20
read to return. :-)

Take care,

Bill

--aM3YZ0Iwxop3KEKx
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (NetBSD)

iD8DBQE/16bxWz+3JHUci9cRAkRaAJ4gCCI3deD39LifWKL7VmGo0Z+61QCeNSIH
ZQQQl8GWtOcZ+ls45PmbA58=
=NQr+
-----END PGP SIGNATURE-----

--aM3YZ0Iwxop3KEKx--