This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
a long /etc/groups entry could cause memory exhaustion.
[perl5.git] / reentr.pl
index 1f4e6ac..a16f33c 100644 (file)
--- a/reentr.pl
+++ b/reentr.pl
@@ -5,6 +5,11 @@
 # and optionally also the relevant metaconfig units (-U option).
 # 
 
+BEGIN {
+    # Get function prototypes
+    require 'regen_lib.pl';
+}
+
 use strict;
 use Getopt::Std;
 my %opts;
@@ -35,23 +40,26 @@ my %map = (
 # Example #3: S_CBI   means type func_r(const char*, char*, int)
 
 
+safer_unlink 'reentr.h';
 die "reentr.h: $!" unless open(H, ">reentr.h");
+binmode H;
 select H;
 print <<EOF;
-/*
+/* -*- buffer-read-only: t -*-
+ *
  *    reentr.h
  *
- *    Copyright (c) 1997-2002, Larry Wall
+ *    Copyright (C) 2002, 2003, 2005 by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
  *
  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
- *  This file is built by reentrl.pl from data in reentr.pl.
+ *  This file is built by reentr.pl from data in reentr.pl.
  */
 
 #ifndef REENTR_H
-#define REENTR_H 
+#define REENTR_H
 
 #ifdef USE_REENTRANT_API
  
@@ -80,6 +88,17 @@ print <<EOF;
 #   define NETDB_R_OBSOLETE
 #endif
 
+/*
+ * As of OpenBSD 3.7, reentrant functions are now working, they just are
+ * incompatible with everyone else.  To make OpenBSD happy, we have to
+ * memzero out certain structures before calling the functions.
+ */
+#if defined(__OpenBSD__)
+#    define REENTR_MEMZERO(a,b) memzero(a,b),
+#else
+#    define REENTR_MEMZERO(a,b)
+#endif 
+
 #ifdef NETDB_R_OBSOLETE
 #   undef HAS_ENDHOSTENT_R
 #   undef HAS_ENDNETENT_R
@@ -135,6 +154,7 @@ my %seenp; # the different prototype signatures for all functions
 my %seent; # the return type of this function
 my %seens; # the type of this function's "S"
 my %seend; # the type of this function's "D"
+my %seenm; # all the types
 my %seenu; # the length of the argument list of this function
 
 while (<DATA>) { # Read in the protypes.
@@ -164,6 +184,7 @@ while (<DATA>) { # Read in the protypes.
 
     # If given the -U option open up the metaconfig unit for this function.
     if ($opts{U} && open(U, ">d_${func}_r.U"))  {
+       binmode U;
        select U;
     }
 
@@ -188,7 +209,7 @@ while (<DATA>) { # Read in the protypes.
        print <<EOF;
 ?RCS: \$Id: d_${func}_r.U,v $
 ?RCS:
-?RCS: Copyright (c) 2002 Jarkko Hietaniemi
+?RCS: Copyright (c) 2002,2003 Jarkko Hietaniemi
 ?RCS:
 ?RCS: You may distribute under the terms of either the GNU General Public
 ?RCS: License or the Artistic License, as specified in the README file.
@@ -259,6 +280,7 @@ EOF
         $seent{$func} = $type;
         $seens{$func} = $m{S};
         $seend{$func} = $m{D};
+       $seenm{$func} = \%m;
     }
     if ($opts{U}) {
        print <<EOF;
@@ -363,6 +385,7 @@ EOF
 EOF
         }
     }
+    return if @F == 1;
     push @define, <<EOF;
 
 /* Any of the @F using \L$n? */
@@ -451,19 +474,22 @@ EOF
 #if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
        $seend{$func} _${func}_data;
 #else
-       $seent{$func} _${func}_struct;
+       $seent{$func} *_${func}_struct_buffer;
 #endif
 EOF
            push @init, <<EOF;
-#ifdef __GLIBC__
-       PL_reentrant_buffer->_${func}_struct.initialized = 0;
-       /* work around glibc-2.2.5 bug */
-       PL_reentrant_buffer->_${func}_struct.current_saltbits = 0;
+#if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
+       PL_reentrant_buffer->_${func}_struct_buffer = 0;
+#endif
+EOF
+           push @free, <<EOF;
+#if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
+       Safefree(PL_reentrant_buffer->_${func}_struct_buffer);
 #endif
 EOF
            pushssif $endif;
        }
-        elsif ($func =~ /^(drand48|gmtime|localtime|random)$/) {
+        elsif ($func =~ /^(drand48|gmtime|localtime|random|srandom)$/) {
            pushssif $ifdef;
            push @struct, <<EOF;
        $seent{$func} _${func}_struct;
@@ -472,6 +498,18 @@ EOF
                push @struct, <<EOF;
        double  _${func}_double;
 EOF
+           } elsif ($1 eq 'random') {
+           push @struct, <<EOF;
+#   if RANDOM_R_PROTO == REENTRANT_PROTO_I_iS
+       int     _${func}_retval;
+#   endif
+#   if RANDOM_R_PROTO == REENTRANT_PROTO_I_lS
+       long    _${func}_retval;
+#   endif
+#   if RANDOM_R_PROTO == REENTRANT_PROTO_I_St
+       int32_t _${func}_retval;
+#   endif
+EOF
            }
            pushssif $endif;
        }
@@ -493,43 +531,36 @@ EOF
        $seent{$func}*  _${genfunc}_ptr;
 #   endif
 EOF
-           if ($genfunc eq 'getspent') {
-               push @size, <<EOF;
-       PL_reentrant_buffer->_${genfunc}_size = 1024;
-EOF
-           } else {
-               push @struct, <<EOF;
+           push @struct, <<EOF;
 #   ifdef USE_${GENFUNC}_FPTR
        FILE*   _${genfunc}_fptr;
 #   endif
 EOF
-                   push @init, <<EOF;
+           push @init, <<EOF;
 #   ifdef USE_${GENFUNC}_FPTR
        PL_reentrant_buffer->_${genfunc}_fptr = NULL;
 #   endif
 EOF
-               my $sc = $genfunc eq 'getgrent' ?
+           my $sc = $genfunc eq 'grent' ?
                    '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
-               my $sz = $genfunc eq 'getgrent' ?
-                    '_grent_size' : '_pwent_size';
-               push @size, <<EOF;
+           my $sz = "_${genfunc}_size";
+           push @size, <<EOF;
 #   if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
-       PL_reentrant_buffer->_${genfunc}_size = sysconf($sc);
+       PL_reentrant_buffer->$sz = sysconf($sc);
        if (PL_reentrant_buffer->$sz == -1)
                PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
 #   else
 #       if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
-       PL_reentrant_buffer->_${genfunc}_size = SIABUFSIZ;
+       PL_reentrant_buffer->$sz = SIABUFSIZ;
 #       else
 #           ifdef __sgi
-       PL_reentrant_buffer->_${genfunc}_size = BUFSIZ;
+       PL_reentrant_buffer->$sz = BUFSIZ;
 #           else
-       PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
+       PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
 #           endif
 #       endif
 #   endif 
 EOF
-            }
            pushinitfree $genfunc;
            pushssif $endif;
        }
@@ -623,7 +654,9 @@ EOF
            my $b = $a;
            my $w = '';
            substr($b, 0, $seenu{$func}) = '';
-           if ($b =~ /R/) {
+           if ($func =~ /^random$/) {
+               $true = "PL_reentrant_buffer->_random_retval";
+           } elsif ($b =~ /R/) {
                $true = "PL_reentrant_buffer->_${genfunc}_ptr";
            } elsif ($b =~ /T/ && $func eq 'drand48') {
                $true = "PL_reentrant_buffer->_${genfunc}_double";
@@ -652,17 +685,27 @@ EOF
                             $_ eq 'D' ?
                                 "&PL_reentrant_buffer->_${genfunc}_data" :
                             $_ eq 'S' ?
-                                ($func =~ /^readdir/ ?
+                                ($func =~ /^readdir\d*$/ ?
                                  "PL_reentrant_buffer->_${genfunc}_struct" :
-                                 "&PL_reentrant_buffer->_${genfunc}_struct" ) :
+                                 $func =~ /^crypt$/ ?
+                                 "PL_reentrant_buffer->_${genfunc}_struct_buffer" :
+                                 "&PL_reentrant_buffer->_${genfunc}_struct") :
                             $_ eq 'T' && $func eq 'drand48' ?
                                 "&PL_reentrant_buffer->_${genfunc}_double" :
+                            $_ =~ /^[ilt]$/ && $func eq 'random' ?
+                                "&PL_reentrant_buffer->_random_retval" :
                                 $_
                         } split '', $b;
                $w = ", $w" if length $v;
            }
            my $call = "${func}_r($v$w)";
-           $call = "((errno = $call))" if $r eq 'I';
+
+            # Must make OpenBSD happy
+            my $memzero = '';
+            if($p =~ /D$/ &&
+                ($genfunc eq 'protoent' || $genfunc eq 'servent')) {
+                $memzero = 'REENTR_MEMZERO(&PL_reentrant_buffer->_' . $genfunc . '_data, sizeof(PL_reentrant_buffer->_' . $genfunc . '_data))';
+            }
            push @wrap, <<EOF;
 #   if !defined($func) && ${FUNC}_R_PROTO == REENTRANT_PROTO_$p
 EOF
@@ -673,9 +716,15 @@ EOF
            } else {
                if ($func =~ /^get/) {
                    my $rv = $v ? ", $v" : "";
-                   push @wrap, <<EOF;
-#       define $func($v) ($call$test ? $true : (errno == ERANGE ? Perl_reentrant_retry("$func"$rv) : 0))
+                   if ($r eq 'I') {
+                       push @wrap, <<EOF;
+#       define $func($v) ($memzero(PL_reentrant_retint = $call)$test ? $true : ((PL_reentrant_retint == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
+EOF
+                   } else {
+                       push @wrap, <<EOF;
+#       define $func($v) ($call$test ? $true : ((errno == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
 EOF
+                    }
                } else {
                    push @wrap, <<EOF;
 #       define $func($v) ($call$test ? $true : 0)
@@ -706,33 +755,44 @@ typedef struct {
 /* The wrappers. */
 
 @wrap
+
 #endif /* USE_REENTRANT_API */
  
 #endif
 
+/* ex: set ro: */
 EOF
 
 close(H);
 
 # Prepare to write the reentr.c.
 
+safer_unlink 'reentr.c';
 die "reentr.c: $!" unless open(C, ">reentr.c");
+binmode C;
 select C;
 print <<EOF;
-/*
+/* -*- buffer-read-only: t -*-
+ *
  *    reentr.c
  *
- *    Copyright (c) 1997-2002, Larry Wall
+ *    Copyright (C) 2002, 2003, 2005 by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
  *
  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
- *  This file is built by reentrl.pl from data in reentr.pl.
+ *  This file is built by reentr.pl from data in reentr.pl.
  *
  * "Saruman," I said, standing away from him, "only one hand at a time can
  *  wield the One, and you know that well, so do not trouble to say we!"
  *
+ * This file contains a collection of automatically created wrappers
+ * (created by running reentr.pl) for reentrant (thread-safe) versions of
+ * various library calls, such as getpwent_r.  The wrapping is done so
+ * that other files like pp_sys.c calling those library functions need not
+ * care about the differences between various platforms' idiosyncrasies
+ * regarding these reentrant interfaces.  
  */
 
 #include "EXTERN.h"
@@ -772,7 +832,7 @@ Perl_reentrant_retry(const char *f, ...)
     dTHX;
     void *retptr = NULL;
 #ifdef USE_REENTRANT_API
-#  if defined(USE_HOSTENT_BUFFER) || defined(USE_GRENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PWENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SRVENT_BUFFER)
+#  if defined(USE_HOSTENT_BUFFER) || defined(USE_GRENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PWENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
     void *p0;
 #  endif
 #  if defined(USE_SERVENT_BUFFER)
@@ -784,9 +844,6 @@ Perl_reentrant_retry(const char *f, ...)
 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
     int anint;
 #  endif
-#ifdef PERL_REENTRANT_MAXSIZE
-    static const char larger[] = "Result from %s larger than %d bytes";
-#endif
     va_list ap;
 
     va_start(ap, f);
@@ -813,10 +870,11 @@ Perl_reentrant_retry(const char *f, ...)
                    retptr = gethostbyaddr(p0, asize, anint); break;
                case OP_GHBYNAME:
                    p0 = va_arg(ap, void *);
-                   retptr = gethostbyname(p0); break;
+                   retptr = gethostbyname((char *)p0); break;
                case OP_GHOSTENT:
                    retptr = gethostent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
@@ -840,13 +898,18 @@ Perl_reentrant_retry(const char *f, ...)
                switch (PL_op->op_type) {
                case OP_GGRNAM:
                    p0 = va_arg(ap, void *);
-                   retptr = getgrnam(p0); break;
+                   retptr = getgrnam((char *)p0); break;
                case OP_GGRGID:
+#if Gid_t_size < INTSIZE
+                   gid = (Gid_t)va_arg(ap, int);
+#else
                    gid = va_arg(ap, Gid_t);
+#endif
                    retptr = getgrgid(gid); break;
                case OP_GGRENT:
                    retptr = getgrent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
@@ -874,18 +937,14 @@ Perl_reentrant_retry(const char *f, ...)
                    retptr = getnetbyaddr(net, anint); break;
                case OP_GNBYNAME:
                    p0 = va_arg(ap, void *);
-                   retptr = getnetbyname(p0); break;
+                   retptr = getnetbyname((char *)p0); break;
                case OP_GNETENT:
                    retptr = getnetent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
-#ifdef PERL_REENTRANT_MAXSIZE
-           else if (ckWARN(WARN_MISC))
-               Perl_warner(aTHX_ packWARN(WARN_MISC),
-                           larger, OP_NAME(PL_op), PERL_REENTRANT_MAXSIZE);
-#endif
        }
        break;
 #endif
@@ -906,21 +965,21 @@ Perl_reentrant_retry(const char *f, ...)
                switch (PL_op->op_type) {
                case OP_GPWNAM:
                    p0 = va_arg(ap, void *);
-                   retptr = getpwnam(p0); break;
+                   retptr = getpwnam((char *)p0); break;
                case OP_GPWUID:
+#if Uid_t_size < INTSIZE
+                   uid = (Uid_t)va_arg(ap, int);
+#else
                    uid = va_arg(ap, Uid_t);
+#endif
                    retptr = getpwuid(uid); break;
                case OP_GPWENT:
                    retptr = getpwent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
-#ifdef PERL_REENTRANT_MAXSIZE
-           else if (ckWARN(WARN_MISC))
-               Perl_warner(aTHX_ packWARN(WARN_MISC),
-                           larger, OP_NAME(PL_op), PERL_REENTRANT_MAXSIZE);
-#endif
        }
        break;
 #endif
@@ -940,21 +999,17 @@ Perl_reentrant_retry(const char *f, ...)
                switch (PL_op->op_type) {
                case OP_GPBYNAME:
                    p0 = va_arg(ap, void *);
-                   retptr = getprotobyname(p0); break;
+                   retptr = getprotobyname((char *)p0); break;
                case OP_GPBYNUMBER:
                    anint = va_arg(ap, int);
                    retptr = getprotobynumber(anint); break;
                case OP_GPROTOENT:
                    retptr = getprotoent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
-#ifdef PERL_REENTRANT_MAXSIZE
-           else if (ckWARN(WARN_MISC))
-               Perl_warner(aTHX_ packWARN(WARN_MISC),
-                           larger, OP_NAME(PL_op), PERL_REENTRANT_MAXSIZE);
-#endif
        }
        break;
 #endif
@@ -975,22 +1030,18 @@ Perl_reentrant_retry(const char *f, ...)
                case OP_GSBYNAME:
                    p0 = va_arg(ap, void *);
                    p1 = va_arg(ap, void *);
-                   retptr = getservbyname(p0, p1); break;
+                   retptr = getservbyname((char *)p0, (char *)p1); break;
                case OP_GSBYPORT:
                    anint = va_arg(ap, int);
                    p0 = va_arg(ap, void *);
-                   retptr = getservbyport(anint, p0); break;
+                   retptr = getservbyport(anint, (char *)p0); break;
                case OP_GSERVENT:
                    retptr = getservent(); break;
                default:
+                   SETERRNO(ERANGE, LIB_INVARG);
                    break;
                }
            }
-#ifdef PERL_REENTRANT_MAXSIZE
-           else if (ckWARN(WARN_MISC))
-               Perl_warner(aTHX_ packWARN(WARN_MISC),
-                           larger, OP_NAME(PL_op), PERL_REENTRANT_MAXSIZE);
-#endif
        }
        break;
 #endif
@@ -1004,6 +1055,7 @@ Perl_reentrant_retry(const char *f, ...)
     return retptr;
 }
 
+/* ex: set ro: */
 EOF
 
 __DATA__
@@ -1021,11 +1073,11 @@ endservent      |netdb  |               |I_D|V_D|D=struct servent_data*
 getgrent       |grp    |struct group   |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
 getgrgid T     |grp    |struct group   |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
 getgrnam C     |grp    |struct group   |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
-gethostbyaddr CWI      |netdb  |struct hostent |I_CWISBWRE|S_CWISBWIE|S_CWISBIE|S_TWISBIE|S_CIISBIE|S_CSBIE|S_TSBIE|I_CWISD|I_CIISD|I_CII|D=struct hostent_data*|T=const void*
+gethostbyaddr CWI      |netdb  |struct hostent |I_CWISBWRE|S_CWISBWIE|S_CWISBIE|S_TWISBIE|S_CIISBIE|S_CSBIE|S_TSBIE|I_CWISD|I_CIISD|I_CII|I_TsISBWRE|D=struct hostent_data*|T=const void*|s=socklen_t
 gethostbyname C        |netdb  |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
 gethostent     |netdb  |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
-getlogin       |unistd |               |I_BW|I_BI|B_BW|B_BI
-getnetbyaddr LI        |netdb  |struct netent  |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|D=struct netent_data*|T=in_addr_t|U=unsigned long
+getlogin       |unistd |char           |I_BW|I_BI|B_BW|B_BI
+getnetbyaddr LI        |netdb  |struct netent  |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|I_uISBWRE|D=struct netent_data*|T=in_addr_t|U=unsigned long|u=uint32_t
 getnetbyname C |netdb  |struct netent  |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
 getnetent      |netdb  |struct netent  |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
 getprotobyname C|netdb |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
@@ -1040,7 +1092,7 @@ getservent        |netdb  |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data
 getspnam C     |shadow |struct spwd    |I_CSBWR|S_CSBI
 gmtime T       |time   |struct tm      |S_TS|I_TS|T=const time_t*
 localtime T    |time   |struct tm      |S_TS|I_TS|T=const time_t*
-random         |stdlib |struct random_data|I_TS|T=int*
+random         |stdlib |struct random_data|I_iS|I_lS|I_St|i=int*|l=long*|t=int32_t*
 readdir T      |dirent |struct dirent  |I_TSR|I_TS|T=DIR*
 readdir64 T    |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
 setgrent       |grp    |               |I_H|V_H