Does / abcd ^ $ / i have anything in Perl?

Perl is one of the things I've never had an excuse for. Unfortunately, I have a very specific (looks like an error for me) bit of Perl code, and I need to determine how it works.

This code has already been written and already created, I want to delete it.

I find it impossible to have a successful match, but this is one of those things that guess on the subject (especially mine) is not good enough. It protects a code block {}, which, unfortunately, has several conditions, so it can be entered regardless of the state of this expression, but there is a security problem if this happens (an error in itself if the state is undefined IMHO, but without proof that impact / severity below (reading will never be fixed)).

Is it possible for a successful match? Including zero byte insertion or any possible byte / binary data? I would even go with some kind of crazy environmental attack (for example, I consume all the memory on the host for 1 byte, forcing the Perl expression evaluator to exit the uncontrolled 2-byte allocation at runtime). Kudos to creativity. /abcd^$/i

+3
source share
5 answers

By the way, I thought I would point here use re 'debug'. You can use it to see how Perl compiles and matches your regular expressions:

$ perl -Mre=debugcolor -e '/abcd^$/'
Compiling REx "abcd^$"
Final program:
   1: EXACT <abcd> (3)
   3: BOL (4)
   4: EOL (5)
   5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Freeing REx: "abcd^$"

With m:

$ perl -Mre=debugcolor -e '/abcd^$/m'
Compiling REx "abcd^$"
Final program:
   1: EXACT <abcd> (3)
   3: MBOL (4)
   4: MEOL (5)
   5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Freeing REx: "abcd^$"

You can also try some example data and make sure that nothing matches:

$ perl -Mre=debugcolor -e '"not going to match" =~ /abcd^$/m'
Compiling REx "abcd^$"
Final program:
   1: EXACT <abcd> (3)
   3: MBOL (4)
   4: MEOL (5)
   5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Guessing start of match in sv for REx "abcd^$" against "not going to match"
Did not find anchored substr "abcd"$...
Match rejected by optimizer
Freeing REx: "abcd^$"

Here the match is not executed twice:

$ perl -Mre=debug -e '"abcd\nabcd\n\n" =~ /abcd^$/m'
...
anchored "abcd"$ at 0 (checking anchored) minlen 4
Guessing start of match in sv for REx "abcd^$" against "abcd%nabcd%n%n"
Found anchored substr "abcd"$ at offset 0...
Guessed: match at offset 0
Matching REx "abcd^$" against "abcd%nabcd%n%n"
   0 <> <abcd%nabcd>         |  1:EXACT <abcd>(3)
   4 <abcd> <%nabcd%n%n>     |  3:MBOL(4)
                                  failed...
   5 <abcd%n> <abcd%n%n>     |  1:EXACT <abcd>(3)
   9 <abcd%nabcd> <%n%n>     |  3:MBOL(4)
                                  failed...
Match failed
Freeing REx: "abcd^$"

, , debugcolor.

.

+4

man- perlre:

, "^" ( , ), "$" . , , /m . ( $*, perl 5.9.)

, $* , , .

, $*, /abcd ^ $/im ( "m" ) , "^" .


, , . - :

use overload;                                                                   
sub import {                                                                    
    overload::constant(qr => sub { $_ = shift; s/^abcd//; $_ });                 
} 

.


, , , , , $_, : "$ str = ~/abcd ^ $/i;".

$_ , , , $_ , , , , :)

+6

? , , . ? , ^? ?

, , , . , . .:)

, , , - . , , , . , , , . , , .

+4

/abcd^$/i /abcd^$/im, $* true ( Perl 5.9).

/abcd$^$/im.

, , 'abcd' , .

, ^ ' -, .

+1

perlre

^ $.

Literal /abcd^$/ , ^ , ^$ , .

perl :

$ cat prog
#! /usr/local/bin/perl -w

$* = 1;
$_ = "AbC\n\n";
print /abc\n^$/i  ? "Match.\n" : "No match.\n";
print /abc\s*^$/i ? "Match.\n" : "No match.\n";

$ ./prog
Use of $* is deprecated at ./prog line 3.
Match.
Match.

perl-5.6.1, 5.10.0 $*. , .

+1

Source: https://habr.com/ru/post/1712986/


All Articles