}
}
+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 */
/*
*/
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) {
# 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)
{
# 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
{
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 */
/* 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);
#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)
{
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
return -1;
#endif
- act.sa_handler = (void(*)(int))handler;
+ act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
#ifdef SA_RESTART
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;
** 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 */
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) {
#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
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.
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;
}
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
# 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 */
{
}
#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. */
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
}