From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53406) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr96L-00044m-Vw for qemu-devel@nongnu.org; Thu, 12 Dec 2013 11:27:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vr96H-00064J-3Y for qemu-devel@nongnu.org; Thu, 12 Dec 2013 11:27:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:64115) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr96G-000645-Sr for qemu-devel@nongnu.org; Thu, 12 Dec 2013 11:26:57 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rBCFoOLH012657 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 12 Dec 2013 10:50:24 -0500 Message-ID: <52A9DB3F.4010902@redhat.com> Date: Thu, 12 Dec 2013 08:50:23 -0700 From: Eric Blake MIME-Version: 1.0 References: <1386763230-9202-1-git-send-email-kraxel@redhat.com> <52A8EF33.4020207@redhat.com> <1386851244.19301.46.camel@nilsson.home.kraxel.org> In-Reply-To: <1386851244.19301.46.camel@nilsson.home.kraxel.org> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="8VdfmurruhUIHb2IWU6Jf0aMNJFQQeTlD" Subject: Re: [Qemu-devel] [PATCH] inet_listen_opts: add error checking List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gerd Hoffmann Cc: qemu-devel@nongnu.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --8VdfmurruhUIHb2IWU6Jf0aMNJFQQeTlD Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 12/12/2013 05:27 AM, Gerd Hoffmann wrote: > Hi, >=20 >>> + if (port_offset) { >>> + int baseport; >>> + errno =3D 0; >>> + baseport =3D strtol(port, NULL, 10); >=20 >> >> WHY is strtol() such a PAINFUL interface to use correctly? >=20 > Crossed my mind too after reading the manpage, which sayed you should > clear errno to reliable detect errors as checking the return value > doesn't cut it. >=20 > Your points obviously underline that. >=20 >> And WHY >> can't qemu copy libvirt's lead of writing a SANE wrapper function, and= >> then mandating that the rest of the code base use the sane wrapper >> instead of strtol()? >> >=20 > Care to share a pointer to the code? /* Like strtol, but produce an "int" result, and check more carefully. Return 0 upon success; return -1 to indicate failure. When END_PTR is NULL, the byte after the final valid digit must be NUL= =2E Otherwise, it's like strtol and lets the caller check any suffix for validity. This function is careful to return -1 when the string S represents a number that is not representable as an "int". */ int virStrToLong_i(char const *s, char **end_ptr, int base, int *result) { long int val; char *p; int err; errno =3D 0; val =3D strtol(s, &p, base); /* exempt from syntax-check */ err =3D (errno || (!end_ptr && *p) || p =3D=3D s || (int) val !=3D va= l); if (end_ptr) *end_ptr =3D p; if (err) return -1; *result =3D val; return 0; } and other variants of virStrToLong_* for parsing into unsigned int, long, etc. Libvirt then couples that with a syntax check that gets run during 'make syntax-check' (or we could even migrate it into 'make check') that forbids all use of strtol() not on a line with the magic exemption comment. Therefore, the number of actual uses of strtol() in the source code base is limited to just these wrapper functions, and everyone else gets sane semantics. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --8VdfmurruhUIHb2IWU6Jf0aMNJFQQeTlD Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.15 (GNU/Linux) Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJSqds/AAoJEKeha0olJ0Nq6B4H/jdX3S9r1AgrutlaeTqzesom L7izEzb+sm2vUHpvXdHC9yQlRRmbliAxkKjvBz5ezFQbdagkhy07wY3uPAGKtgjB 4VGDVjnU8L54h0hg5YyEfjT9uHKKKDH/tnjmPlq9hRCt/nqji4QFVcKPazvcS0co VHlLFcunLZlv/38QC1QcmG5/wsDCmuKHrZ2YJV7eK4m2uP6RTSIr3ecJ/eCcmTAl 2hQko6ltOdnqaltfOcJVPShCdlzjT4f/DdLHYmDlnlcTcRzSL3GBmCopCZ1f2s2k xywdxZmQoTivOWvrCtb9NJUugtLdPl7MBEYbHth7FhN31sWzIsv/GXIJ2TD7nLs= =ipwu -----END PGP SIGNATURE----- --8VdfmurruhUIHb2IWU6Jf0aMNJFQQeTlD--