[PATCH sed] ck_fclose should unlink *before* calling do_ck_fclose.

NeilBrown <[email protected]>
Newsgroups gmane.comp.gnu.utils.bugs
Message-ID <[email protected]>
If do_ck_fclose gets an error from fclose() it will call
panic() which will try to close everything on the list.
As the file which fclose was called on is still on the list,
it will be fclose()ed again, which is undefined behaviour and
can result in a process crash.

So unlink from the list *before* calling do_ck_fclose().

If the error happens at fflush time instead of fclose time,
the fp will not end up being closed.  This is not a serious
problem, but it could be fixed by calling ck_fflush(cur->fp)
before unlinking, and do_ck_fclose(cur->fp) afterwards.


diff --git a/sed/utils.c b/sed/utils.c
index 0a5351105e32..0e451bc24c92 100644
--- a/sed/utils.c
+++ b/sed/utils.c
@@ -297,8 +297,8 @@ ck_fclose(stream)
     {
       if (!stream || stream == cur->fp)
 	{
-	  do_ck_fclose (cur->fp);
 	  prev->link = cur->link;
+	  do_ck_fclose (cur->fp);
 	  free(cur->name);
 	  free(cur);
 	}
signature.asc (application/pgp-signature, 828 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIVAwUBU40IYDnsnt1WYoG5AQJ/chAAinGc69q1GDO0tCTbyzHNPY3yAlDAWSoQ
qR2lmMUKUIwqvUxuuih2vYq/DwmCEHarnOVGAyHE6iNghXhLx4IBx8yfco94+WJ+
dX2yrf6IQWwZTm5cmE0Hyxny4jHTNg99NwjkD/3+1TDKfKapXUgK5OUK0WzJkxDk
IpuekZlMWyx3KxLqdlm3hX92Mshk6A7yjT9APPUADLGqy2nTbscCTlFzQ898mjBW
5C36ZE/hYaJabZeua39czKFM7quZS7q+I/4ELJnuN+amoCEu70fVcWUQXjLpvwMa
Gyoxn5PZCZZzxeejFjkKO5CZ8p8Q8SR4fFkh9No7tUFS7gTBJWrQOT8cc+vHHGnc
QJNHc9OK+r5ZokMqC+Lz6KcB59480YEcIOqZJ3Bw6t5W1QHt6sTQaCuVAexAFyU3
cvL9VaMLQUWrLrs/AXjWfjQKA5QfVYOKTGRZh9kQ50I6NK+sL7FEPtcBHV841r4m
lLiABNjhjUXhJDRj5TV7+akZwhDm1z4I4QhTgiOPK3IVMwkOugvZdqcsGEgnQCYy
28SlyChoPfHrSlsoPuvueE8mrH4Y6pkgCLqGl/rUNFgRnmIQmVVkD/5GLUlSSUxM
3ojkb0DrXQw0+c+YyZAbL5c1/+3pWUVl2bNRo+1Ece3b41uguItfJ+EH9GWjHCL2
tXYwDkoHrSw=
=V3AN
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.