Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

= rather than =~ ?

by bcrowell2 (Friar)
on Jan 01, 2004 at 17:08 UTC ( [id://318130]=perlquestion: print w/replies, xml ) Need Help??

bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:

This web page gives the following example:
    You should also continue to master Perl if you want to ensure security. For example, if alarm bells don't immediately go off in your head when you see something like this:
    $input = /(\w+)/; my $keyword = $1;
    then you need to keep studying. The problem with this code is that when the match fails, $1 is left over from a previous match. This kind of code can be used as a security exploit, if the attacker can access the source code or have an idea that this is happening. It's code that ``looks right'' but definitely isn't.
But isn't the use of the = sign, rather than =~, also incorrect?

Replies are listed 'Best First'.
•Re: = rather than =~ ?
by merlyn (Sage) on Jan 01, 2004 at 17:21 UTC

      This note might confuse newbies, so I thought just add a comment.

      "That should have been =~!"

      What merly said is that, that was a typo, and not what he wanted. (As you can see, he is the owner of that web page.)

      But as a Perl statement, it is absolutely valid, and jweed has already given a full explanation.

      Goodness. And all this time I thought you had /meant/ it that way.
Re: = rather than =~ ?
by jweed (Chaplain) on Jan 01, 2004 at 17:18 UTC
    This is technically valid perl. Since no search string is specified, $_ will be searched, implicitly. Then, as perlop points out, the match in scalar context returns true when a match occurs and false when it doesn't. So $input will be true or false, based on whether $_ had one or more word characters.


    Who is Kayser Söze?
    Code is (almost) always untested.
Re: = rather than =~ ?
by Anonymous Monk on Jan 01, 2004 at 17:17 UTC
    It depends. I don't know what $input is, but that code is matching against $_. If the match fails, the match fails. Such code should always be written something like $foo = $1 if /(foo)/;
      my $foo = m/(foo)/ ? $1 : '';

      cheers

      tachyon

        If you feel the need point that one out, you should probably also explain why that is.. :)

        Makeshifts last the longest.

Re: = rather than =~ ?
by SavannahLion (Pilgrim) on Jan 02, 2004 at 07:09 UTC
    I just wanted to say thank you for pointing something like that that is so fundamentally important. After reading your example and better understanding how $1 behaved, I went back and took a careful look over some source code. There were three distinct sections where I made that error. After I examined the code logic, there was indeed one circumstance where, if the match failed, then there was a potential for $1 to be exposed. (The other two are wrapped in if statements.)

    Fortunately, I took to reading PerlMonks on a regular basis and happened to read this post before the code went, "live." There's no way I can give all of my daily allocated points to just the people in this thread, but if I could, I would.

    ----
    Thanks for your patience.
    Prove your knowledge @ HLPD
    Edit: Fixed botched HTML :) Sorry everyone has a different markup language.

Re: = rather than =~ ?
by inman (Curate) on Jan 02, 2004 at 18:04 UTC
    Another potentially confusing variation would be if list context was used since the match will return the list of matched sub-expressions. In the example below, $input is assigned a value for each iteration wheras $1 is only changed on a good match.

    #! /usr/bin/perl use strict; my $input; while (<DATA>) { ($input) = /(\w+)/; print "\$input=$input\t\$1=$1\n"; } __DATA__ Hello World

    Output:

    $input=Hello $1=Hello $input= $1=Hello $input=World $1=World

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://318130]
Approved by calin
Front-paged by PodMaster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found