From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51938) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1er23a-0005EV-TR for qemu-devel@nongnu.org; Wed, 28 Feb 2018 08:46:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1er23V-0007G4-8U for qemu-devel@nongnu.org; Wed, 28 Feb 2018 08:46:06 -0500 References: <20180228131315.30194-1-mreitz@redhat.com> <20180228131315.30194-2-mreitz@redhat.com> <20180228133434.GL17774@redhat.com> From: Max Reitz Message-ID: <1fc55643-7ca6-26a5-7e2e-7d6b09a43a13@redhat.com> Date: Wed, 28 Feb 2018 14:45:49 +0100 MIME-Version: 1.0 In-Reply-To: <20180228133434.GL17774@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="DCiNym8kLjBbNQF4lznlq1adE3JsqCxPQ" Subject: Re: [Qemu-devel] [PATCH 1/2] block/file-posix: Fix fully preallocated truncate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "=?UTF-8?Q?Daniel_P._Berrang=c3=a9?=" Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Kevin Wolf , qemu-stable@nongnu.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --DCiNym8kLjBbNQF4lznlq1adE3JsqCxPQ From: Max Reitz To: =?UTF-8?Q?Daniel_P._Berrang=c3=a9?= Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Kevin Wolf , qemu-stable@nongnu.org Message-ID: <1fc55643-7ca6-26a5-7e2e-7d6b09a43a13@redhat.com> Subject: Re: [PATCH 1/2] block/file-posix: Fix fully preallocated truncate References: <20180228131315.30194-1-mreitz@redhat.com> <20180228131315.30194-2-mreitz@redhat.com> <20180228133434.GL17774@redhat.com> In-Reply-To: <20180228133434.GL17774@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 2018-02-28 14:34, Daniel P. Berrang=C3=A9 wrote: > On Wed, Feb 28, 2018 at 02:13:14PM +0100, Max Reitz wrote: >> Storing the lseek() result in an int results in it overflowing when th= e >> file is at least 2 GB big. Then, we have a 50 % chance of the result >> being "negative" and thus thinking an error occurred when actually >> everything went just fine. >> >> So we should use the correct type for storing the result: off_t. >> >> Reported-by: Daniel P. Berrange >> Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D1549231 >> Cc: qemu-stable@nongnu.org >> Signed-off-by: Max Reitz >> --- >> block/file-posix.c | 5 +++-- >> 1 file changed, 3 insertions(+), 2 deletions(-) >> >> diff --git a/block/file-posix.c b/block/file-posix.c >> index f1591c3849..90c25864a0 100644 >> --- a/block/file-posix.c >> +++ b/block/file-posix.c >> @@ -1697,6 +1697,7 @@ static int raw_regular_truncate(int fd, int64_t = offset, PreallocMode prealloc, >> case PREALLOC_MODE_FULL: >> { >> int64_t num =3D 0, left =3D offset - current_length; >> + off_t seek_result; >> =20 >> /* >> * Knowing the final size from the beginning could allow the = file >> @@ -1711,8 +1712,8 @@ static int raw_regular_truncate(int fd, int64_t = offset, PreallocMode prealloc, >> =20 >> buf =3D g_malloc0(65536); >> =20 >> - result =3D lseek(fd, current_length, SEEK_SET); >> - if (result < 0) { >> + seek_result =3D lseek(fd, current_length, SEEK_SET); >> + if (seek_result < 0) { >=20 > off_t is an unsigned type, so this comparison to "< 0" is bogus - only = the > exact value (off_t)-1 indicates an error. So this needs to be >=20 > if (seek_result =3D=3D (off_t)-1) { > ... > } Hmmm... On my system, it appears to be a long int[1]. And find_allocation() does an off_t < 0 comparison already. And "man 0p sys_types.h" says "blkcnt_t and off_t shall be signed integer types." Max [1] #define do_stringify(x) #x #define stringify(x) do_stringify(x) int main(void) { printf("%s\n", stringify(__OFF_T_TYPE)); } Output: long int --DCiNym8kLjBbNQF4lznlq1adE3JsqCxPQ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQFGBAEBCAAwFiEEkb62CjDbPohX0Rgp9AfbAGHVz0AFAlqWso0SHG1yZWl0ekBy ZWRoYXQuY29tAAoJEPQH2wBh1c9AONAH/jquAp2QH3Vnt+mUwZmlOMUVUjuxZThB faHgE64bZA9vIpbOBh7caE7FwOytjvNj8uK/BN+uoXNj2dUmFPPSfbB9Khh4lzR3 mt1t1xkPEPtBG01i7mcveCBGohoem7n5BjFmGZKRPG0hc/+tLEaEX0bCKmbA61RB myvjNBZCSD7nd+lDt2Y2sCA2uSbYiM1YKWsZvaysueSM8ueeAw+3ToLZgD2dLfYf 4mzPdA7OxnwSFYi49Jrm+zxKFaqqqN4VySx/8CAYJcesQVvTQnwrPF6k+vMooZYe 4qJPaX+MUGbiojJqzHLxStVttKaTJ9dUCKnaLN2BiMZsljbNMCFZeyc= =jQDb -----END PGP SIGNATURE----- --DCiNym8kLjBbNQF4lznlq1adE3JsqCxPQ--