summaryrefslogtreecommitdiff
path: root/util.c
Commit message (Collapse)AuthorAgeFilesLines
* Perl_my_setenv(); handle integer wrapDavid Mitchell2018-11-051-23/+53
| | | | | | | | | | | | | | | | | | | RT #133204 Wean this function off int/I32 and onto UV/Size_t. Also, replace all malloc-ish calls with a wrapper that does overflow checks, In particular, it was doing (nlen + vlen + 2) which could wrap when the combined length of the environment variable name and value exceeded around 0x7fffffff. The wrapper check function is probably overkill, but belt and braces... NB this function has several variant parts, #ifdef'ed by platform type; I have blindly changed the parts that aren't compiled under linux. (cherry picked from commit 34716e2a6ee2af96078d62b065b7785c001194be)
* don't call Perl_fbm_instr() with negative lengthDavid Mitchell2017-08-231-0/+2
| | | | | | | | | | | | | RT #131575 re_intuit_start() could calculate a maximum end position less than the current start position. This used to get rejected by fbm_intr(), until v5.23.3-110-g147f21b, which made fbm_intr() faster and removed unnecessary checks. This commits fixes re_intuit_start(), and adds an assert to fbm_intr(). (cherry picked from commit bb152a4b442f7718fd37d32cc558be675e8ae1ae)
* Revert "get rid of USE_HASH_SEED_EXPLICIT"Yves Orton2017-04-231-1/+3
| | | | | | This reverts commit dd1b95f812312c85390f487887cdd55282fcd6ce. Accidentally pushed work pending unfreeze.
* get rid of USE_HASH_SEED_EXPLICITYves Orton2017-04-231-3/+1
| | | | | | I think this has been outright broken for a long time, and it doesnt make any sense to since mandatory randomization anyway, so simply remove it.
* add PERL_UTIL_H_ to util.h, not util.hDavid Mitchell2017-04-181-6/+0
| | | | | | | With v5.25.11-59-g7335cb8 I added an include guard. PERL_UTIL_H_, but added it to util.c rather than util.h. I am not a smart man....
* add include guard to util.hDavid Mitchell2017-04-161-0/+6
| | | | | | see RT #131110 for the bikeshedding on what name to use. Based on an original patch by Jim Schneider
* update size after RenewHugo van der Sanden2017-03-151-7/+11
| | | | | | | | | | | | | | | RT #130841 In general code, change this idiom: PL_foo_max += size; Renew(PL_foo, PL_foo_max, foo_t); to Renew(PL_foo, PL_foo_max + size, foo_t); PL_foo_max += size; so that if Renew dies, PL_foo_max won't be left hanging.
* Inline foldEQ, foldEQ_latin1, foldEQ_localeKarl Williamson2017-02-191-83/+0
| | | | These short functions are called in inner loops and regex backtracking.
* Moving variables to their innermost scope.Andy Lester2017-02-181-5/+3
| | | | | | Some vars have been tagged as const because they do not change in their new scopes. In pp_reverse in pp.c, I32 tmp is only used to hold a char, so is changed to char.
* (perl #126203) build issuesTony Cook2017-02-121-1/+3
| | | | helps to test builds with the right options...
* (perl #126203) avoid potential leaks on quadmath_snprintf() failureTony Cook2017-02-091-1/+4
| | | | | | | | | | In the unlikely case quadmath_snprintf() fails both sv_vcatpvfn_flags() and my_snprintf() could leak the temp format string returned by quadmath_format_single() if quadmath_format_single() had to rebuild the format. Getting quadmath_snprintf() to fail in this context seems impractical, but future changes may make it happen, so clean up after ourselves.
* Perl_fbm_instr(): remove dead code.David Mitchell2017-02-061-10/+1
| | | | | | | For the case where littlestr hasn't been FBM compiled (!SvVALID()), it can't be SvTAIL(), so there's no need for optional \n handling. Spotted by Coverity.
* mess_sv(): access only if PL_curcop is non-nullDavid Mitchell2017-01-231-8/+11
| | | | | | | | | | | | | RT #130621 In Perl_mess_sv(), don't try to add an "at foo line NN" to the error message if PL_curcop is null. In the ticket above, the reason that PL_curcop is null is the less than optimal way that evals free their optree: ideally the optree should be attached to the eval CV and freed when the CV is; instead a separate SAVEFREEOP() is done. But that fix is for another time; regardless, mess_sv() should have a PL_curcop != NULL guard anyway.
* util.c: Allow C backtrace to compile under threadsKarl Williamson2017-01-191-1/+1
| | | | | | | | This would not compile due to passing a thread context parameter to Safefree, which isn't expecting it. No claim is made by this commit that it works well under threads. No claim is made in the documentation to that effect either.
* add build options to disable the PERL_HASH* and PERL_PERTURB_KEYS env varsTony Cook2017-01-091-3/+9
| | | | | | These variables either control or reveal information used in perl's hash implementation that a careful user may not want controlled or exposed.
* add sv_set_undef() API functionDavid Mitchell2016-11-241-4/+3
| | | | | | | This function is equivalent to sv_setsv(sv, &PL_sv_undef), but more efficient. Also change the obvious places in the core to use the new idiom.
* Change white space to avoid C++ deprecation warningKarl Williamson2016-11-181-28/+28
| | | | | | | | | | | | | | | | | | | | | | C++11 requires space between the end of a string literal and a macro, so that a feature can unambiguously be added to the language. Starting in g++ 6.2, the compiler emits a warning when there isn't a space (presumably so that future versions can support C++11). Unfortunately there are many such instances in the perl core. This commit fixes those, including those in ext/, but individual commits will be used for the other modules, those in dist/ and cpan/. This commit also inserts space at the end of a macro before a string literal, even though that is not deprecated, and removes useless "" literals following a macro (instead of inserting a blank). The result is easier to read, making the macro stand out, and be clearer as to the intention. Code and modules included with the Perl core need to be compilable using C++. This is so that perl can be embedded in C++ programs. (Actually, only the hdr files need to be so compilable, but it would be hard to test that just the hdrs are compilable.) So we need to accommodate changes to the C++ language.
* reduce cost of SvVALID()David Mitchell2016-11-131-2/+3
| | | | | | | | Now that SvVALID() no longer just checks an SV flag, but instead checks for the existence of a certain type of magic, avoid using this more expensive macro when its not really needed. Also, and an extra flag test to SvVALID() to make it fail quicker.
* eliminate SVpbm_VALID flagDavid Mitchell2016-11-121-14/+1
| | | | | | | | This flag is set on an SV to indicate that it has PERL_MAGIC_bm (fast Boyer-Moore) magic attached. Instead just directly check whether it has such magic. This frees up the 0x40000000 bit for anything except AVs and HVs
* eliminate SVpbm_TAIL/SvTAIL_on()/SvTAIL_off()David Mitchell2016-11-121-2/+1
| | | | | | | | | | | | (but keep SvTAIL()) This flag is only set on SVs that have Boyer-Moore magic attached. Such SVs already re-purpose the unused IVX slot of that SV to store BmUSEFUL. This commit repurposes the unused NVX slot to store this boolean value instead. Now that flag bit (0x80000000) is only used with AVs, HVs, RVs and scalar SVs with IOK.
* Only test SvTAIL when SvVALIDDavid Mitchell2016-11-121-8/+9
| | | | | | Only use the SvTAIL() macro when we've already confirmed that the SV is SvVALID() - this is in preparation for removing the SVpbm_TAIL flag in the next commit
* rename S_delimcpy() to S_delimcpy_intern()David Mitchell2016-09-071-3/+3
| | | | | Its a bit confusing having both S_delimcpy() and Perl_delimcpy() functions.
* Perl_delimcpy(): handle backslash as last charDavid Mitchell2016-09-071-2/+9
| | | | | | | | | | | | | [perl #129064] heap-buffer-overflow S_scan_heredoc [perl #129176] Conditional jump depends on uninitialized values in S_scan_heredoc Perl_delimcpy() is supposed to copy a delimited string to another buffer; it handles \-<delimiter> escapes, but if the backslash is the last character in the src buffer, it could overrun the end of the buffer slightly. Also document a bit better what this function is supposed to do.
* Fix checks for tainted dir in $ENV{PATH}Father Chrysostomos2016-09-031-3/+22
| | | | | | | | | | | | | | | | | | | $ cat > foo #!/usr/bin/perl print "What?!\n" ^D $ chmod +x foo $ ./perl -Ilib -Te '$ENV{PATH}="."; exec "foo"' Insecure directory in $ENV{PATH} while running with -T switch at -e line 1. That is what I expect to see. But: $ ./perl -Ilib -Te '$ENV{PATH}="/\\:."; exec "foo"' What?! Perl is allowing the \ to escape the :, but the \ is not treated as an escape by the system, allowing a relative path in PATH to be consid- ered safe.
* Update outdated man links for strlcpy and strlcat.Theo Buehler2016-08-311-2/+2
|
* perlapi: Document returns from my_strlcat, my_strlcpyKarl Williamson2016-08-251-0/+7
|
* Perl_my_vsnprintf: avoid compiler warningDavid Mitchell2016-06-211-1/+2
| | | | | | | | | | | | | in the usequadmath branch, gcc is too clever for its own good: PERL_UNUSED_ARG(ap); gives: util.c:5299:18: warning: ‘sizeof’ on array function parameter ‘ap’ will return size of ‘__va_list_tag *’ [-Wsizeof-array-argument] Stick in a void* cast to shut it up.
* Use memmem() if available on the platform for Perl_ninstr()Karl Williamson2016-05-121-0/+8
|
* Finish mathomizing Perl_instr.Craig A. Berry2016-05-091-12/+0
| | | | | | | | | | | | | | | | | | | | | | fea1d2dd5d210564d4 turned instr into a macro. It also left the actual function in util.c while commenting out the prototype in proto.h (via the m flag in embed.fnc). A function compiled without a prototype under C++ does not get declared with extern "C" and thus gets mangled, which breaks the build with a strict linker (VMS, possibly AIX) because the expected symbol name is no longer produced. Without a strict linker, it just breaks the binary compatibility that was presumably the nominal reason for leaving the function around in the first place. So move the function into mathoms.c and put its prototype in the extern "C"-guarded section at the top of the same file. We also have to fake the PERL_ARGS_ASSERT_INSTR macro since its original declaration in proto.h is commented out but the porting test t/porting/args_assert.t will take revenge if it doesn't find the macro being used somewhere.
* Get -Accflags=-DPERL_MEM_LOG compiling againMatthew Horsfall2016-04-051-0/+6
| | | | | | | | It had rotted a bit Well, more than one probably. Move the declarations of the functions Perl_mem_log_alloc etc from handy.h into embed.fnc where whey belong, and where Malloc_t will have already been defined.
* Move the zeroing back before the dladdr() call.Jarkko Hietaniemi2016-03-271-5/+5
|
* [perl #127773: Assertion failure in perl built with Dusecbacktrace]Vladimir Timofeev2016-03-271-9/+10
|
* [perl #127764] Perl with '-Dusecbacktrace' doesn't compile on darwinJarkko Hietaniemi2016-03-221-1/+1
| | | | Fix a thinko in 22ff3130.
* rename and function-ise dtrace macrosDavid Mitchell2016-03-181-0/+78
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit: 1. Renames the various dtrace probe macros into a consistent and self-documenting pattern, e.g. ENTRY_PROBE => PERL_DTRACE_PROBE_ENTRY RETURN_PROBE => PERL_DTRACE_PROBE_RETURN Since they're supposed to be defined only under PERL_CORE, this shouldn't break anything that's not being naughty. 2. Implement the main body of these macros using a real function. They were formerly defined along the lines of if (PERL_SUB_ENTRY_ENABLED()) PERL_SUB_ENTRY(...); The PERL_SUB_ENTRY() part is a macro generated by the dtrace system, which for example on linux expands to a large bunch of assembly directives. Replace the direct macro with a function wrapper, e.g. if (PERL_SUB_ENTRY_ENABLED()) Perl_dtrace_probe_call(aTHX_ cv, TRUE); This reduces to once the number of times the macro is expanded. The new functions also take simpler args and then process the values they need using intermediate temporary vars to avoid huge macro expansions. For example ENTRY_PROBE(CvNAMED(cv) ? HEK_KEY(CvNAME_HEK(cv)) : GvENAME(CvGV(cv)), CopFILE((const COP *)CvSTART(cv)), CopLINE((const COP *)CvSTART(cv)), CopSTASHPV((const COP *)CvSTART(cv))); is now PERL_DTRACE_PROBE_ENTRY(cv); This reduces the executable size by 1K on -O2 -Dusedtrace builds, and by 45K on -DDEBUGGING -Dusedtrace builds.
* amigaos4: better popen() + pclose() implementationAndy Broad2016-03-111-4/+1
| | | | | | | popen(): handle better the case where the popened external might exit before the child process manages to start. pclose(): protect with a semaphore.
* perlapi: Document ninstr() and rninstr()Karl Williamson2016-03-101-3/+40
|
* make building without memcpy work (RT #127619)Lukas Mai2016-03-071-1/+6
|
* util.c: make my_mem*/my_b* prototypes more like the originalsLukas Mai2016-03-071-25/+19
|
* Time::HiRes moved from "cpan" to "dist" in 91ba54Tom Hukins2016-02-091-1/+1
|
* util.c: fix/simplify unused arg logic in my_vsnprintfLukas Mai2016-01-311-3/+2
|
* [perl #126240] avoid leaking memory when setting $ENV{foo} on darwinTony Cook2016-01-061-1/+1
| | | | | | | | | My change in e396210 was incomplete, that change was intended to force use of setenv()/unsetenv() on Darwin, but ended up using putenv() instead, which is a leaky mechanism. Added darwin to the list of many others that work better with setenv()/ unsetenv().
* [perl #123991] report an error if we can't parse the number after -CTony Cook2015-12-071-0/+3
|
* annotate the mutex use of atfork_lock and atfork_unlockJarkko Hietaniemi2015-11-231-0/+18
| | | | | | | | | | | | | | util.c:2729:1: warning: mutex 'PL_perlio_mutex' is still held at the end of function [-Wthread-safety-analysis] } util.c:2729:1: warning: mutex 'PL_op_mutex' is still held at the end of function [-Wthread-safety-analysis] } util.c:2739:5: warning: releasing mutex 'PL_perlio_mutex' that was not held [-Wthread-safety-analysis] MUTEX_UNLOCK(&PL_perlio_mutex); util.c:2744:5: warning: releasing mutex 'PL_op_mutex' that was not held [-Wthread-safety-analysis] OP_REFCNT_UNLOCK;
* clang thread safety annotationsJarkko Hietaniemi2015-11-231-0/+22
| | | | | | | | | | | | http://clang.llvm.org/docs/ThreadSafetyAnalysis.html Static (compile-time) annotations for declaring the multithreaded behavior of functions, variables, and capabilities (like mutexes). Available since about clang 3.5. ./Configure -des -Dusedevel -Dusethreads -Dcc=clang -Accflags='-Wthread-safety' clang -Wthread-safety then checks the validity of the annotations.
* VMS-specific statbuf in Perl_find_script.Craig A. Berry2015-10-151-0/+1
| | | | | | | | In 97466d2cbf895b I added a declaration at function scope, but in some paths that was overridden at an inner scope, leaving the function-level one declared but not used. So lets go back to the original intent of 45a23732c73c8 and have a separate declaration in each block that needs it.
* Revert "Missed one statbuf declaration in 45a23732c73."Craig A. Berry2015-10-151-1/+1
| | | | | | | This reverts commit 97466d2cbf895b35ac41b8bf7c31db955b52d48e. Adding a declaration at function scope causes unused variable warnings when there are paths that redeclare it at an inner scope.
* make /fixed-substr/ much faster.David Mitchell2015-10-131-69/+102
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | TL;DR: on platforms with a libc memchr() implementation which makes good use of underlying hardware support, patterns which include fixed substrings will now often be much faster; for example with glibc on on a recent x86_64 CPU, this: $s = "a" x 1000 . "wxyz"; $s =~ /wxyz/ for 1..30000 is now about 7 times faster. On systems with slow memchr(), e.g. 32-bit ARM Raspberry Pi, there will be a small or little speedup. Conversely, some pathological cases, such as "ab" x 1000 =~ /aa/ will be slower now; up to 3 times slower on the rPi, 1.5x slower on x86_64. In detail: The perl core includes a Boyer-Moore substring matcher, Perl_fbm_instr(), which is used to quickly find a fixed substring within a longer string, where a table of lookups is pre-computed from the substring. As well as being used in index() when the substring is a constant, its main use is in patterns. When the regex engine compiles a pattern, it typically takes note of the two longest fixed substrings within the pattern; for example in /[01]abc\w+de\d+fghij+/ the two longest are "abc" and "fghij". The engine uses Perl_fbm_instr() to scan for these two strings before running the full NFA. This often allows the string to be quickly rejected, or to find a suitable minimum starting point to run the NFA. However, Perl_fbm_instr() was written about 16 years ago and has been virtually untouched since, so it could do with some love. It currently special-cases strings of length 1 and 2, using roll-your-own loops along the lines of while (s < end) { if (*s++ = c1) ... } while strings of length 3+ use the Boyer-Moore algorithm. The big advantage of BM is that in a best-case, where none of the characters from the substring are found in this region of the string, it only has to test every N'th char, where N is length of the substring. For example when searching for wxyz in abcdefghikl..., it just reads and tests d,h,l,.. However these days some platforms have decent memchr() implementations. For example, glibc has assembly-level implementations for i386, x86_64, sparc32/64, powerpc32/64, s390-32/64, arm, m68k and ia64 by the looks of it. These can often be substantially faster than a C-level implementation. This commit makes Perl_fbm_instr() use memchr() where possible. For the length == 1 special case, it just calls memchr() directly rather than using a loop as previously. For the length == 2 special case, it continues to distinguish the cases where the two chars of the substring are the same or differ. For the former it calls memchr() after an initial direct failure, i.e. if (*s != c) { s++; s = memchr(....); ... } For the latter case it does a similar direct test first (to avoid the costly overhead of a call to memchr() when the next char is the one we seek anyway), but in addition, on each failure to find the second char following a found first char, it swaps which char it's searching for. This means that in something like "aaaaaaa..." =~ /ab/, it wont keep hopping 1 char position with memchar(s,'a'); after the first hop it will do memchr(s,'b') and skip lots of chars in one go. This helps reduce the number of pathological cases. For the length >= 3 cases (normal BM), it keeps using BM, but after each iteration where the pointer has been incremented by the skip determined by the BM algorithm, it now does an additional if (*s != c) { s++; s = memchr(....); ... } step before running the next iteration of BM.
* fbm_instr(): tweak docs and formattingDavid Mitchell2015-10-131-4/+26
| | | | | Expand the commentary at the start of this function; add more blank lines to separate chunks of code, and document what SVpbm_TAIL is for.
* Missed one statbuf declaration in 45a23732c73.Craig A. Berry2015-10-091-1/+1
| | | | | The VMS-specific corner of the ifdef jungle ended up using statbuf without declaring it, so add a declaration.
* partial PL_statbuf removalDaniel Dragan2015-10-081-14/+23
| | | | | | | | | | | | | | | | Perl_nextargv has to have access to the Stat_t that is written to inside S_openn_cleanup or else run/switches.t, io/argv.t, io/inplace.t, and io/iprefix.t will fail. Removing the uses of PL_statbuf that are using PL_statbuf due to historical reason, and not using PL_statbuf to pass data between different funcs/different callstacks. This patch makes it easier to remove PL_statbuf in the future since the number uses of it has been reduced. -in Perl_apply move SETERRNO before tot--; so the branch can be combined with other "tot--;" branches by CC optmizer -combine 2 Perl_croak(aTHX_ "Illegal suidscript"); statements in S_validate_suid to make code look simpler, drop my_perl arg for space efficiency on threads of rarely executed code