Received: from eggs.gnu.org ([2001:4830:134:3::10]:35990) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmLF4-0003Kg-0X for qemu-devel@nongnu.org; Tue, 20 Sep 2016 09:37:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bmLEz-0006Tq-Qk for qemu-devel@nongnu.org; Tue, 20 Sep 2016 09:37:44 -0400 References: From: Eric Blake Message-ID: Date: Tue, 20 Sep 2016 08:37:21 -0500 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="aQBagfuh9dgWtpEUhEPXABt3LfvBvwGFU" Subject: Re: [Qemu-devel] [PATCH v2] add resolutions via command-line List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: G 3 , Benjamin Herrenschmidt Cc: "list@suse.de:PowerPC list:PowerPC" , qemu-devel qemu-devel This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --aQBagfuh9dgWtpEUhEPXABt3LfvBvwGFU From: Eric Blake To: G 3 , Benjamin Herrenschmidt Cc: "list@suse.de:PowerPC list:PowerPC" , qemu-devel qemu-devel Message-ID: Subject: Re: [Qemu-devel] [PATCH v2] add resolutions via command-line References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 09/19/2016 11:28 PM, G 3 wrote: > Add the ability to add resolutions from the command-line. This patch > works by > looking for a property called 'resolutions' in the options node of > OpenBIOS. > If it is found all the resolutions are parsed and loaded. >=20 > +/* strlen() implementation */ > +static int strlen(const char *str) Is it worth renaming this to make it obvious that it is your (non-optimal) replacement, intentionally because you don't want to use the libc version? By the way, strlen() normally returns size_t; redefining a standard function to return a different type is just asking for problems. > +{ > + int count =3D 0; > + =20 > + for( ; *str !=3D '\0'; str++) { > + count++; =20 > + } > + =20 > + return count; > +} > + > + > +/* convert ascii string to number */ > +static int atoi(const char *str) > +{ > + int result =3D 0, multiplier; > + > + multiplier =3D strlen(str) - 1; > + for ( ; *str !=3D '\0'; str++) { > + result +=3D (*str - '0') * pow(10, multiplier); > + multiplier--; > + if (multiplier < 0) /* Something might be wrong if returning > here */ > + return result; > + } EWWWW. If you're going to (poorly) reimplement a standard function, at the very minimum you should NOT be doing it with a dirt-slow algorithm. atoi() already has undefined behavior on integer overflow; use that to your advantage. pow() (and to a lesser extent strlen()) should NEVER be part of atoi(). Try something more like: static int my_atoi(const char *str) { int result =3D 0; while (*str) { result =3D result * 10 + *str - '0'; str++; } } There you go. Much nicer, shorter, and drags in fewer dependencies (and I renamed it because I do NOT detect leading whitespace or '-', which is a requirement of the libc atoi()). > + > +/* An interesting way of emulating memory allocation. */ > +static char *malloc(int size) Again, if you're going to override a standard function, either change the name (to make it obvious what you are doing), or at a bare minimum match the standard signature (void *malloc(size_t)). And overriding malloc() without also overriding free(), realloc(), calloc(), and who knows what else, is likely a recipe for disaster if a later programmer sees your use of malloc and tries to add in other uses of memory management, without realizing your override is NOT the same as the libc version. So my recommendation: PLEASE rename this to something that is NOT 'malloc()', to make it OBVIOUS that you are NOT allocating heap memor= y. Meanwhile, I seriously doubt you really want to be reimplementing malloc(); you are likely headed to disaster (if you can't even override atoi() in a sane manner, I dread what will happen when your driver runs out of malloc() space). --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --aQBagfuh9dgWtpEUhEPXABt3LfvBvwGFU Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJX4TuRAAoJEKeha0olJ0Nq6l4IAKQtXuv5kQIjZbfx0uZ4gZLI PDrWxjxmn7nXMYa87efMujAuPP0OA91uuRsGBxK8STqKRRMzwYQ7l8VMQ4AID8wG dB2sItxCmmz+/SdI/PTl6K7a2ZPyzQ7Zr/cDZiBeg183Pijplh3WuxreyY2uABfs b9zHGzMOE1fBu7TaoYbkZelvHYw5WeEn7eQmmyLrs8vUiqCsaivr2xIPRnyA7Pf9 SICgAdvzXx92psWe/gK/I4+rr8y9bF2Z8uVu+8tnKVeTapRU43IaYJuAh1FHIIBC P060HwMCc1KKETpwULDGJkUXnD8WMRoj0QOV8PL8WkRJOVT0X67TNVE2E8yJk5E= =VYLw -----END PGP SIGNATURE----- --aQBagfuh9dgWtpEUhEPXABt3LfvBvwGFU--