This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
authorJarkko Hietaniemi <jhi@iki.fi>
Sat, 3 Nov 2001 15:43:03 +0000 (15:43 +0000)
committerJarkko Hietaniemi <jhi@iki.fi>
Sat, 3 Nov 2001 15:43:03 +0000 (15:43 +0000)
[ 12830]
Fix clone_leak problem.
PerlIOStdio_dup was leaking FILE * as it was still doing fdopen()
as vestige of calling PerlLIO_dup().

[ 12829]
Tweaks attempting to locate Doug's clone_leak leak.
- add some PerlIO_debug()
- handle PerlIO_clone() with empty lists (clone before Perl_parse())
- Even if it closing stdout etc. do a fflush()

p4raw-link: @12830 on //depot/perlio: 6124d23f33100dc3c406774873984a1b51f6ab02
p4raw-link: @12829 on //depot/perlio: f4b9981fadd371fad0a1f2bdf41857e39e760c27

p4raw-id: //depot/perl@12832
p4raw-integrated: from //depot/perlio@12827 'copy in' perlio.c
(@12830..)

perlio.c

index 51962db..f102600 100644 (file)
--- a/perlio.c
+++ b/perlio.c
@@ -519,13 +519,16 @@ PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
 PerlIO_list_t *
 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
 {
-    int i;
-    PerlIO_list_t *list = PerlIO_list_alloc(aTHX);
-    for (i=0; i < proto->cur; i++) {
-       SV *arg = Nullsv;
-       if (proto->array[i].arg)
-           arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
-       PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
+    PerlIO_list_t *list = (PerlIO_list_t *) NULL;
+    if (proto) {
+       int i;
+       list = PerlIO_list_alloc(aTHX);
+       for (i=0; i < proto->cur; i++) {
+           SV *arg = Nullsv;
+           if (proto->array[i].arg)
+               arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
+           PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
+       }
     }
     return list;
 }
@@ -540,6 +543,7 @@ PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
     PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
     PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
     PerlIO_allocate(aTHX); /* root slot is never used */
+    PerlIO_debug("Clone %p from %p\n",aTHX,proto);
     while ((f = *table)) {
            int i;
            table = (PerlIO **) (f++);
@@ -558,6 +562,9 @@ PerlIO_destruct(pTHX)
 {
     PerlIO **table = &PL_perlio;
     PerlIO *f;
+#ifdef USE_ITHREADS
+    PerlIO_debug("Destruct %p\n",aTHX);
+#endif
     while ((f = *table)) {
        int i;
        table = (PerlIO **) (f++);
@@ -2017,15 +2024,6 @@ PerlIOUnix_refcnt_inc(int fd)
     }
 }
 
-void
-PerlIO_cleanup(pTHX)
-{
-    PerlIOUnix_refcnt_inc(0);
-    PerlIOUnix_refcnt_inc(1);
-    PerlIOUnix_refcnt_inc(2);
-    PerlIO_cleantable(aTHX_ &PL_perlio);
-}
-
 int
 PerlIOUnix_refcnt_dec(int fd)
 {
@@ -2043,6 +2041,24 @@ PerlIOUnix_refcnt_dec(int fd)
     return cnt;
 }
 
+void
+PerlIO_cleanup(pTHX)
+{
+    int i;
+#ifdef USE_ITHREADS
+    PerlIO_debug("Cleanup %p\n",aTHX);
+#endif
+    /* Raise STDIN..STDERR refcount so we don't close them */
+    for (i=0; i < 3; i++)
+       PerlIOUnix_refcnt_inc(i);
+    PerlIO_cleantable(aTHX_ &PL_perlio);
+    /* Restore STDIN..STDERR refcount */
+    for (i=0; i < 3; i++)
+       PerlIOUnix_refcnt_dec(i);
+}
+
+
+
 /*--------------------------------------------------------------------------------------*/
 /*
  * Bottom-most level for UNIX-like case
@@ -2466,23 +2482,10 @@ PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
     /* This assumes no layers underneath - which is what
        happens, but is not how I remember it. NI-S 2001/10/16
      */
-    int fd = PerlIO_fileno(o);
-    if (fd >= 0) {
-       char buf[8];
-       FILE *stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o, buf));
-       if (stdio) {
-           if ((f = PerlIOBase_dup(aTHX_ f, o, param))) {
-               PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
-               PerlIOUnix_refcnt_inc(fd);
-           }
-           else {
-               PerlSIO_fclose(stdio);
-           }
-       }
-       else {
-           PerlLIO_close(fd);
-           f = NULL;
-       }
+    if ((f = PerlIOBase_dup(aTHX_ f, o, param))) {
+       FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
+       PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
+       PerlIOUnix_refcnt_inc(fileno(stdio));
     }
     return f;
 }
@@ -2497,6 +2500,8 @@ PerlIOStdio_close(PerlIO *f)
 #endif
     FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
     if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
+       /* Do not close it but do flush any buffers */
+       PerlIO_flush(f);
        return 0;
     }
     return (