Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Is it safe to use external strings for regexes? (infinite loops)

by AnomalousMonk (Bishop)
on Oct 11, 2021 at 18:01 UTC ( #11137431=note: print w/replies, xml ) Need Help??


in reply to Re^2: Is it safe to use external strings for regexes? (infinite loops)
in thread Is it safe to use external strings for regexes?

The section to which you linked goes on to say

Thus Perl allows such constructs, by forcefully breaking the infinite loop.


Give a man a fish:  <%-{-{-{-<

  • Comment on Re^3: Is it safe to use external strings for regexes? (infinite loops)
  • Download Code

Replies are listed 'Best First'.
Re^4: Is it safe to use external strings for regexes? (infinite loops)
by AnomalousMonk (Bishop) on Oct 11, 2021 at 21:58 UTC

    Update: Oops... I meant this post as a reply to this post, not to myself, but that's ok, no need to re-parent. :)


    As you have, I think, suggested elsewhere and as the documentation itself cheerfully admits, a rewrite of this section would be welcome.

    That the section begins with a discussion of the evils of zero-width match infinite loops accompanied by a bunch of Perl code examples of such matches that don't actually "work" (in the sense that they don't produce infinite loops) is not helpful. The discussion finally gets around to saying that the Perl RE does not, in fact, allow such loops, but by then one may have been led far down the garden path and abandoned in the dark forest.


    Give a man a fish:  <%-{-{-{-<

      While I like that Perl doesn't allow this to block the engine, I'm not too sure about the solution.

      Ignoring regex grammar to silently continue might be worse than throwing an explicit warning.

        Regex-Problem: Empty match detected, trying continuation...

      Alike the "deep recursion" warning if Perl dives 100 times deep into the same sub.

      But I don't think I know the RE algebra good enough to tell.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      update

      s/1000/100/

        hippo messaged me that

        > FYI, the default recursion limit is "only" 100.

        I knew, but when in tested in the debugger I only saw

        1000 levels deep in subroutine calls!

        It seems that the debugger has a hard breakpoint at 1000 and doesn't show the normal "deep recursion" warning.

        Tested with 5.32 on Win.

        FWIW:

        use strict; use warnings; our $limit =1050; our $l=0; sub tst { $l++; #warn "level: $l" unless $l%50; die "LIMIT $limit reached" if $l>$limit; tst(); } tst();
        in debugger
        D:\tmp\pm>perl -d tst_recursion.pl Loading DB routines from perl5db.pl version 1.57 Editor support available. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. main::(tst_recursion.pl:4): our $limit =1050; DB<1> c main::tst(tst_recursion.pl:9): $l++; 1000 levels deep in subroutine calls! DB<1>

        normal run

        D:\tmp\pm>perl tst_recursion.pl Deep recursion on subroutine "main::tst" at tst_recursion.pl line 12. LIMIT 1050 reached at tst_recursion.pl line 11. D:\tmp\pm>

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^4: Is it safe to use external strings for regexes? (infinite loops)
by LanX (Sage) on Oct 11, 2021 at 21:27 UTC
    > Thus Perl allows such constructs, by forcefully breaking the infinite loop.

    Thanks, that wasn't clear to me.

    BUT I should have taken more care about the

    > WARNING: Difficult material (and prose) ahead. This section needs a rewrite.

    FWIW, the forced break can be seen with re 'debug'

    D:\tmp>perl -Mre=debug -e"'foo' =~ m{ ( o? )* }x;" Compiling REx " ( o? )* " Final program: 1: CURLYX[0]{0,INFTY} (12) 3: OPEN1 (5) 5: CURLY{0,1} (9) 7: EXACT <o> (0) 9: CLOSE1 (11) 11: WHILEM[1/1] (0) 12: NOTHING (13) 13: END (0) minlen 0 Matching REx " ( o? )* " against "foo" 0 <> <foo> | 0| 1:CURLYX[0]{0,INFTY}(12) 0 <> <foo> | 1| 11:WHILEM[1/1](0) | 1| WHILEM: matched 0 out of 0..65535 0 <> <foo> | 2| 3:OPEN1(5) 0 <> <foo> | 2| 5:CURLY{0,1}(9) | 2| EXACT <o> can match 0 times out +of 1... 0 <> <foo> | 3| 9:CLOSE1(11) 0 <> <foo> | 3| 11:WHILEM[1/1](0) | 3| WHILEM: matched 1 out of 0..655 +35 | 3| WHILEM: empty match detected, t +rying continuation... <---- HERE + 0 <> <foo> | 4| 12:NOTHING(13) 0 <> <foo> | 4| 13:END(0) Match successful! Freeing REx: " ( o? )* " D:\tmp>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-01-18 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (54 votes). Check out past polls.

    Notices?