This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add thread safety to some environment accesses
[perl5.git] / util.c
diff --git a/util.c b/util.c
index 359f3b6..33e60b4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1035,6 +1035,26 @@ Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U
     }
 }
 
+const char *
+Perl_cntrl_to_mnemonic(const U8 c)
+{
+    /* Returns the mnemonic string that represents character 'c', if one
+     * exists; NULL otherwise.  The only ones that exist for the purposes of
+     * this routine are a few control characters */
+
+    switch (c) {
+        case '\a':       return "\\a";
+        case '\b':       return "\\b";
+        case ESC_NATIVE: return "\\e";
+        case '\f':       return "\\f";
+        case '\n':       return "\\n";
+        case '\r':       return "\\r";
+        case '\t':       return "\\t";
+    }
+
+    return NULL;
+}
+
 /* copy a string to a safe spot */
 
 /*
@@ -1088,13 +1108,11 @@ need to use the shared memory functions, such as C<L</savesharedpvn>>.
 */
 
 char *
-Perl_savepvn(pTHX_ const char *pv, I32 len)
+Perl_savepvn(pTHX_ const char *pv, Size_t len)
 {
     char *newaddr;
     PERL_UNUSED_CONTEXT;
 
-    assert(len >= 0);
-
     Newx(newaddr,len+1,char);
     /* Give a meaning to NULL pointer mainly for the use in sv_magic() */
     if (pv) {
@@ -2103,6 +2121,15 @@ S_env_alloc(void *current, Size_t l1, Size_t l2, Size_t l3, Size_t size)
 
 #  if !defined(WIN32) && !defined(NETWARE)
 
+/*
+=for apidoc my_setenv
+
+A wrapper for the C library L<setenv(3)>.  Don't use the latter, as the perl
+version has desirable safeguards
+
+=cut
+*/
+
 void
 Perl_my_setenv(pTHX_ const char *nam, const char *val)
 {
@@ -2112,7 +2139,8 @@ Perl_my_setenv(pTHX_ const char *nam, const char *val)
 #    endif
 
 #    ifdef USE_ITHREADS
-  /* only parent thread can modify process environment */
+  /* only parent thread can modify process environment, so no need to use a
+   * mutex */
   if (PL_curinterp == aTHX)
 #    endif
   {
@@ -2248,7 +2276,7 @@ Perl_my_setenv(pTHX_ const char *nam, const char *val)
     envstr = S_env_alloc(NULL, nlen, vlen, 2, 1);
     my_setenv_format(envstr, nam, nlen, val, vlen);
     (void)PerlEnv_putenv(envstr);
-    Safefree(envstr);
+    safesysfree(envstr);
 }
 
 #  endif /* WIN32 || NETWARE */
@@ -2368,23 +2396,23 @@ Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
     /* If we managed to get status pipe check for exec fail */
     if (did_pipes && pid > 0) {
        int errkid;
-       unsigned n = 0;
+       unsigned read_total = 0;
 
-       while (n < sizeof(int)) {
+       while (read_total < sizeof(int)) {
             const SSize_t n1 = PerlLIO_read(pp[0],
-                             (void*)(((char*)&errkid)+n),
-                             (sizeof(int)) - n);
+                             (void*)(((char*)&errkid)+read_total),
+                             (sizeof(int)) - read_total);
            if (n1 <= 0)
                break;
-           n += n1;
+           read_total += n1;
        }
        PerlLIO_close(pp[0]);
        did_pipes = 0;
-       if (n) {                        /* Error */
+       if (read_total) {                       /* Error */
            int pid2, status;
            PerlLIO_close(p[This]);
-           if (n != sizeof(int))
-               Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n);
+           if (read_total != sizeof(int))
+               Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", read_total);
            do {
                pid2 = wait4pid(pid, &status, 0);
            } while (pid2 == -1 && errno == EINTR);
@@ -2683,6 +2711,15 @@ dup2(int oldfd, int newfd)
 #ifndef PERL_MICRO
 #ifdef HAS_SIGACTION
 
+/*
+=for apidoc rsignal
+
+A wrapper for the C library L<signal(2)>.  Don't use the latter, as the Perl
+version knows things that interact with the rest of the perl interpreter.
+
+=cut
+*/
+
 Sighandler_t
 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
 {
@@ -2695,7 +2732,7 @@ Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
        return (Sighandler_t) SIG_ERR;
 #endif
 
-    act.sa_handler = (void(*)(int))handler;
+    act.sa_handler = handler;
     sigemptyset(&act.sa_mask);
     act.sa_flags = 0;
 #ifdef SA_RESTART
@@ -2740,7 +2777,7 @@ Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
        return -1;
 #endif
 
-    act.sa_handler = (void(*)(int))handler;
+    act.sa_handler = handler;
     sigemptyset(&act.sa_mask);
     act.sa_flags = 0;
 #ifdef SA_RESTART
@@ -3342,7 +3379,7 @@ Perl_get_context(void)
     dVAR;
 #  ifdef OLD_PTHREADS_API
     pthread_addr_t t;
-    int error = pthread_getspecific(PL_thr_key, &t)
+    int error = pthread_getspecific(PL_thr_key, &t);
     if (error)
        Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error);
     return (void*)t;
@@ -3856,7 +3893,7 @@ Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, in
   ** If there is a better way to make it portable, go ahead by
   ** all means.
   */
-  if ((len > 0 && len < buflen) || (len == 0 && *fmt == '\0'))
+  if (inRANGE(len, 1, buflen - 1) || (len == 0 && *fmt == '\0'))
     return buf;
   else {
     /* Possibly buf overflowed - try again with a bigger buf */
@@ -3870,7 +3907,7 @@ Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, in
       buflen = strftime(buf, bufsize, fmt, &mytm);
       GCC_DIAG_RESTORE_STMT;
 
-      if (buflen > 0 && buflen < bufsize)
+      if (inRANGE(buflen, 1, bufsize - 1))
        break;
       /* heuristic to prevent out-of-memory errors */
       if (bufsize > 100*fmtlen) {
@@ -4894,51 +4931,40 @@ Perl_mem_log_del_sv(const SV *sv,
 #endif /* PERL_MEM_LOG */
 
 /*
-=for apidoc quadmath_format_single
+=for apidoc quadmath_format_valid
 
 C<quadmath_snprintf()> is very strict about its C<format> string and will
 fail, returning -1, if the format is invalid.  It accepts exactly
 one format spec.
 
-C<quadmath_format_single()> checks that the intended single spec looks
+C<quadmath_format_valid()> checks that the intended single spec looks
 sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>,
 and has C<Q> before it.  This is not a full "printf syntax check",
 just the basics.
 
-Returns the format if it is valid, NULL if not.
-
-C<quadmath_format_single()> can and will actually patch in the missing
-C<Q>, if necessary.  In this case it will return the modified copy of
-the format, B<which the caller will need to free.>
+Returns true if it is valid, false if not.
 
 See also L</quadmath_format_needed>.
 
 =cut
 */
 #ifdef USE_QUADMATH
-const char*
-Perl_quadmath_format_single(const char* format)
+bool
+Perl_quadmath_format_valid(const char* format)
 {
     STRLEN len;
 
-    PERL_ARGS_ASSERT_QUADMATH_FORMAT_SINGLE;
+    PERL_ARGS_ASSERT_QUADMATH_FORMAT_VALID;
 
     if (format[0] != '%' || strchr(format + 1, '%'))
-        return NULL;
+        return FALSE;
     len = strlen(format);
     /* minimum length three: %Qg */
-    if (len < 3 || strchr("efgaEFGA", format[len - 1]) == NULL)
-        return NULL;
-    if (format[len - 2] != 'Q') {
-        char* fixed;
-        Newx(fixed, len + 2, char);
-        memcpy(fixed, format, len - 1);
-        fixed[len - 1] = 'Q';
-        fixed[len    ] = format[len - 1];
-        fixed[len + 1] = 0;
-        return (const char*)fixed;
-    }
-    return format;
+    if (len < 3 || memCHRs("efgaEFGA", format[len - 1]) == NULL)
+        return FALSE;
+    if (format[len - 2] != 'Q')
+        return FALSE;
+    return TRUE;
 }
 #endif
 
@@ -4954,7 +4980,7 @@ but it should catch most common cases.
 
 If true is returned, those arguments B<should> in theory be processed
 with C<quadmath_snprintf()>, but in case there is more than one such
-format specifier (see L</quadmath_format_single>), and if there is
+format specifier (see L</quadmath_format_valid>), and if there is
 anything else beyond that one (even just a single byte), they
 B<cannot> be processed because C<quadmath_snprintf()> is very strict,
 accepting only one format spec, and nothing else.
@@ -4991,7 +5017,7 @@ Perl_quadmath_format_needed(const char* format)
       else
         while (isDIGIT(*q)) q++;
     }
-    if (strchr("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
+    if (memCHRs("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
       return TRUE;
     p = q + 1;
   }
@@ -5023,24 +5049,15 @@ Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...)
     va_start(ap, format);
 #ifdef USE_QUADMATH
     {
-        const char* qfmt = quadmath_format_single(format);
         bool quadmath_valid = FALSE;
-        if (qfmt) {
+        if (quadmath_format_valid(format)) {
             /* If the format looked promising, use it as quadmath. */
-            retval = quadmath_snprintf(buffer, len, qfmt, va_arg(ap, NV));
+            retval = quadmath_snprintf(buffer, len, format, va_arg(ap, NV));
             if (retval == -1) {
-                if (qfmt != format) {
-                    dTHX;
-                    SAVEFREEPV(qfmt);
-                }
-                Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", qfmt);
+                Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
             }
             quadmath_valid = TRUE;
-            if (qfmt != format)
-                Safefree(qfmt);
-            qfmt = NULL;
         }
-        assert(qfmt == NULL);
         /* quadmath_format_single() will return false for example for
          * "foo = %g", or simply "%g".  We could handle the %g by
          * using quadmath for the NV args.  More complex cases of
@@ -5153,7 +5170,8 @@ Perl_my_clearenv(pTHX)
 #  else /* ! (PERL_IMPLICIT_SYS || WIN32) */
 #    if defined(USE_ENVIRON_ARRAY)
 #      if defined(USE_ITHREADS)
-    /* only the parent thread can clobber the process environment */
+    /* only the parent thread can clobber the process environment, so no need
+     * to use a mutex */
     if (PL_curinterp == aTHX)
 #      endif /* USE_ITHREADS */
     {
@@ -5578,36 +5596,6 @@ Perl_my_strlcpy(char *dst, const char *src, Size_t size)
 }
 #endif
 
-/*
-=for apidoc my_strnlen
-
-The C library C<strnlen> if available, or a Perl implementation of it.
-
-C<my_strnlen()> computes the length of the string, up to C<maxlen>
-characters.  It will will never attempt to address more than C<maxlen>
-characters, making it suitable for use with strings that are not
-guaranteed to be NUL-terminated.
-
-=cut
-
-Description stolen from http://man.openbsd.org/strnlen.3,
-implementation stolen from PostgreSQL.
-*/
-#ifndef HAS_STRNLEN
-Size_t
-Perl_my_strnlen(const char *str, Size_t maxlen)
-{
-    const char *p = str;
-
-    PERL_ARGS_ASSERT_MY_STRNLEN;
-
-    while(maxlen-- && *p)
-        p++;
-
-    return p - str;
-}
-#endif
-
 #if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_MSC_VER < 1400) && (WINVER < 0x0500)
 /* VC7 or 7.1, building with pre-VC7 runtime libraries. */
 long _ftol( double ); /* Defined by VC6 C libs. */
@@ -6425,8 +6413,8 @@ Perl_get_c_backtrace(pTHX_ int depth, int skip)
     Safefree(raw_frames);
     return bt;
 #else
-    PERL_UNUSED_ARGV(depth);
-    PERL_UNUSED_ARGV(skip);
+    PERL_UNUSED_ARG(depth);
+    PERL_UNUSED_ARG(skip);
     return NULL;
 #endif
 }