This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix core dump on IO::Seekable::setpos($fh, undef)
[perl5.git] / ext / IO / IO.xs
index 3cc3518..2eb16f4 100644 (file)
@@ -106,11 +106,12 @@ fsetpos(handle, pos)
        InputStream     handle
        SV *            pos
     CODE:
        InputStream     handle
        SV *            pos
     CODE:
-       if (handle)
+       char *p;
+       if (handle && (p = SvPVx(pos, na)) && na == sizeof(Fpos_t))
 #ifdef PerlIO
 #ifdef PerlIO
-           RETVAL = PerlIO_setpos(handle, (Fpos_t*)SvPVX(pos));
+           RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
 #else
 #else
-           RETVAL = fsetpos(handle, (Fpos_t*)SvPVX(pos));
+           RETVAL = fsetpos(handle, (Fpos_t*)p);
 #endif
        else {
            RETVAL = -1;
 #endif
        else {
            RETVAL = -1;
@@ -121,17 +122,29 @@ fsetpos(handle, pos)
 
 MODULE = IO    PACKAGE = IO::File      PREFIX = f
 
 
 MODULE = IO    PACKAGE = IO::File      PREFIX = f
 
-OutputStream
+SV *
 new_tmpfile(packname = "IO::File")
     char *             packname
 new_tmpfile(packname = "IO::File")
     char *             packname
+    PREINIT:
+       OutputStream fp;
+       GV *gv;
     CODE:
 #ifdef PerlIO
     CODE:
 #ifdef PerlIO
-       RETVAL = PerlIO_tmpfile();
+       fp = PerlIO_tmpfile();
 #else
 #else
-       RETVAL = tmpfile();
+       fp = tmpfile();
 #endif
 #endif
-    OUTPUT:
-       RETVAL
+       gv = (GV*)SvREFCNT_inc(newGVgen(packname));
+       hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
+       if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
+           ST(0) = sv_2mortal(newRV((SV*)gv));
+           sv_bless(ST(0), gv_stashpv(packname, TRUE));
+           SvREFCNT_dec(gv);   /* undo increment in newRV() */
+       }
+       else {
+           ST(0) = &sv_undef;
+           SvREFCNT_dec(gv);
+       }
 
 MODULE = IO    PACKAGE = IO::Handle    PREFIX = f
 
 
 MODULE = IO    PACKAGE = IO::Handle    PREFIX = f
 
@@ -203,6 +216,7 @@ int
 untaint(handle)
        SV *    handle
     CODE:
 untaint(handle)
        SV *    handle
     CODE:
+#ifdef IOf_UNTAINT
        IO * io;
        io = sv_2io(handle);
        if (io) {
        IO * io;
        io = sv_2io(handle);
        if (io) {
@@ -210,9 +224,12 @@ untaint(handle)
            RETVAL = 0;
        }
         else {
            RETVAL = 0;
        }
         else {
+#endif
            RETVAL = -1;
            errno = EINVAL;
            RETVAL = -1;
            errno = EINVAL;
+#ifdef IOf_UNTAINT
        }
        }
+#endif
     OUTPUT:
        RETVAL
 
     OUTPUT:
        RETVAL
 
@@ -252,8 +269,8 @@ setvbuf(handle, buf, type, size)
        int             type
        int             size
     CODE:
        int             type
        int             size
     CODE:
-#ifdef PERLIO_IS_STDIO
-#ifdef _IOFBF   /* Should be HAS_SETVBUF once Configure tests for that */
+/* Should check HAS_SETVBUF once Configure tests for that */
+#if defined(PERLIO_IS_STDIO) && defined(_IOFBF)
        if (handle)
            RETVAL = setvbuf(handle, buf, type, size);
        else {
        if (handle)
            RETVAL = setvbuf(handle, buf, type, size);
        else {
@@ -261,10 +278,7 @@ setvbuf(handle, buf, type, size)
            errno = EINVAL;
        }
 #else
            errno = EINVAL;
        }
 #else
-           RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
-#endif /* _IOFBF */
-#else
-           not_here("IO::Handle::setvbuf");
+       RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
 #endif
     OUTPUT:
        RETVAL
 #endif
     OUTPUT:
        RETVAL