From owner-freebsd-bugs@FreeBSD.ORG Mon Aug 21 10:22:11 2006 Return-Path: X-Original-To: freebsd-bugs@freebsd.org Delivered-To: freebsd-bugs@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 64DEC16A4DD for ; Mon, 21 Aug 2006 10:22:11 +0000 (UTC) (envelope-from sheldonh@clue.co.za) Received: from munchkin.clue.co.za (munchkin.clue.co.za [66.219.59.160]) by mx1.FreeBSD.org (Postfix) with ESMTP id E2FE143D45 for ; Mon, 21 Aug 2006 10:22:10 +0000 (GMT) (envelope-from sheldonh@clue.co.za) Received: from uucp by munchkin.clue.co.za with local (Exim 4.62) (envelope-from ) id 1GF6v3-0004cN-MK; Mon, 21 Aug 2006 10:22:09 +0000 Received: from axlet.clue.co.za ([10.0.0.8]) by urchin.clue.co.za with esmtpa (Exim 4.62) (envelope-from ) id 1GF6ug-0007HA-LW; Mon, 21 Aug 2006 12:21:52 +0200 From: Sheldon Hearn Organization: Clue Technologies To: "Ighighi Ighighi" Date: Mon, 21 Aug 2006 12:21:39 +0200 User-Agent: KMail/1.9.4 References: In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart1861485.Rhc0O5H4Le"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200608211221.43718@axl.clue.co.za> X-Spam-Scanner: urchin.clue.co.za X-Spam-Score: -5.0 (-----) [INT -49] X-Spam-Report: -5.0/5.0 ---- Start SpamAssassin results * -5.0 ALL_TRUSTED Passed through trusted hosts only via SMTP ---- End SpamAssassin results Cc: freebsd-bugs@freebsd.org Subject: Re: bug in truncate(1) - all FreeBSD versions X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Aug 2006 10:22:11 -0000 --nextPart1861485.Rhc0O5H4Le Content-Type: multipart/mixed; boundary="Boundary-01=_0kY6EOMN4bNsve/" Content-Transfer-Encoding: 7bit Content-Disposition: inline --Boundary-01=_0kY6EOMN4bNsve/ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Monday 21 August 2006 11:55, Ighighi Ighighi wrote: > I'm working on a portable version of truncate(1) and I noticed a lot > of "continue" statements in the main while() loop that skip the call > to close(fd) at the end. The obvious consequence is a file > descriptor leak... > > There's still another bug: parselength() does not need to return > off_t because its return value is checked in the usual binary (0 vs > -1) way. Thanks for the feedback. I believe that the attached patch addresses=20 both of the concerns raised. Ciao, Sheldon. =2D-=20 Sheldon Hearn IT Director Clue Technologies (PTY) Ltd Web: http://www.clue.co.za/ Mail: sheldonh@clue.co.za Office: +27-21-913-8840 Mobile: +27-83-564-3276 Timezone: SAST (+0200) --Boundary-01=_0kY6EOMN4bNsve/ Content-Type: text/x-diff; charset="iso-8859-6"; name="truncate.c.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="truncate.c.patch" =2D-- truncate.c.orig 2006-08-21 12:10:35.000000000 +0200 +++ truncate.c 2006-08-21 12:20:10.000000000 +0200 @@ -40,7 +40,7 @@ #include #include =20 =2Dstatic off_t parselength(char *, off_t *); +static int parselength(char *, off_t *); static void usage(void); =20 static int no_create; @@ -107,7 +107,11 @@ oflags =3D O_WRONLY | O_CREAT; omode =3D S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; =20 + fd =3D -1; while ((fname =3D *argv++) !=3D NULL) { + if (fd !=3D -1) + close(fd); + if ((fd =3D open(fname, oflags, omode)) =3D=3D -1) { if (errno !=3D ENOENT) { warn("%s", fname); @@ -138,9 +142,9 @@ error++; continue; } =2D =2D close(fd); } + if (fd !=3D -1) + close(fd); =20 return error ? EXIT_FAILURE : EXIT_SUCCESS; } @@ -149,7 +153,7 @@ * Return the numeric value of a string given in the form [+-][0-9]+[GMK] * or -1 on format error or overflow. */ =2Dstatic off_t +static int parselength(char *ls, off_t *sz) { off_t length, oflow; --Boundary-01=_0kY6EOMN4bNsve/-- --nextPart1861485.Rhc0O5H4Le Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBE6Yk3pGJX8XSgas0RAp5iAJ95l+6pi4Ws+j6HabPAeUgXxY07tQCfbKTz Mbm+NBki5ymUCzwcdDQfEr4= =5OEG -----END PGP SIGNATURE----- --nextPart1861485.Rhc0O5H4Le--