Date: Mon, 21 Aug 2006 12:21:39 +0200 From: Sheldon Hearn <sheldonh@clue.co.za> To: "Ighighi Ighighi" <ighighi@gmail.com> Cc: freebsd-bugs@freebsd.org Subject: Re: bug in truncate(1) - all FreeBSD versions Message-ID: <200608211221.43718@axl.clue.co.za> In-Reply-To: <de5dfb5a0608210255s34ce786fo97b502caa37b6f37@mail.gmail.com> References: <de5dfb5a0608210255s34ce786fo97b502caa37b6f37@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--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 <stdlib.h> #include <unistd.h> =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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200608211221.43718>