Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Mixing tied variables with goto label

by Limbic~Region (Chancellor)
on Nov 15, 2005 at 21:25 UTC ( #508797=perlquestion: print w/ replies, xml ) Need Help??
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
Every now and then I try to think of ideas for Obfuscated code. I seldom follow through, but it is often a great learning exercise. Consider the following code:
#!/usr/bin/perl use strict; use warnings; sub jump { my $place = 'START'; goto $place; } START: print "Hello, world\n"; jump();
As you might expect, this creates an infinite loop. I wondered what might happen if $place were a tied variable that instead of fetching a value - did a goto elsewhere. Would the original goto ever get called?
#!/usr/bin/perl use strict; use warnings; sub jump { tie my $place, 'main'; goto $place; } START: print "Hello, world\n"; jump(); print "I am jumping over this spot\n"; END: print "Goodbye, cruel world\n"; sub TIESCALAR { bless {}, $_[0] } sub STORE { print "Read only\n"; } sub FETCH { goto END; }
This dies yelling about Can't find label END at ... First, I know that Perl supports goto label out of a sub as demonstrated by the first snippet. Second, I can't find anything in goto docs that would suggest this shouldn't work. Can anyone shed some light?

Cheers - L~R

Comment on Mixing tied variables with goto label
Select or Download Code
Re: Mixing tied variables with goto label
by jeffa (Chancellor) on Nov 15, 2005 at 21:32 UTC
      jeffa,
      Try this:

      Yeah, but there is nothing special about that behavior. goto $var treats $var as the label as I showed in my first example. What I am trying to do is a little misdirection. Imagine that I could make you forget that I had a tied variable and that storing a value didn't appear to do anything abnormal:

      my $label = 'START'; goto $label; START: print "Hello, world\n"; END: print "Goodbye, cruel world\n";

      When you run the code, instead of going to START it actually goes to END. The trouble is that the label goes away inside the tied FETCH sub with no explanation I can find.

      Cheers - L~R

      Added a code example to make it clearer what is being attempted.
Re: Mixing tied variables with goto label
by dave_the_m (Parson) on Nov 16, 2005 at 00:27 UTC
    goto only searches the immediate call stack for labels, ie where perl code has called a function. Functions that are called indirectly, like signal handlers, tie handlers, sort functions etc don't get searched. I don't know whether this is documented.

    Dave.

      dave_the_m,
      I couldn't find it documented anywhere. So it is how the function is called that is causing the problem. I can still produce a fair amount of misdirection. I am not sure I have the time or energy to actually make the obfu, but I would be happy and impressed if someone did. Like most magic and misdirection, the most important part is the setup.

      You will need to hide the tie, sub TIESCALAR, and friends. This should be possible using evil eval. The FETCH needs to be special too. Instead of using an internal goto to produce the magic, it will return the current value in the STORE the first time, but the second time it will return a different label.

      my $label; # magic setup happens here $label = 'START'; print "$label\n"; # prints START goto $label; exit; START: print "Hello, world\n"; END: print "Goodbye, cruel world\n";
      When it is run, the output will be unexpected:
      START Goodbye, cruel world
      In any case, if you happen to find some documentation explaining this behavior I would appreciate it.

      Cheers - L~R

      Where do you think would be a good place to document this? perlfunc doesn't seem like the right place, so perlsyn maybe?

        It should go in perlfunc, in the section on goto

        Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://508797]
Approved by Arunbear
Front-paged by Roy Johnson
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-09-21 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls