How to taint a variable (for testing)

K

kj

For testing purposes I would like to be able to intentionally taint
a variable. What's the easiest way to do this?

TIA!

~K

P.S. I tried mucking around with the function SvTAINT and Inline::C,
but my test script crashes when I run it under -T. Code below;
completely untested.

#!/usr/bin/perl

BEGIN {
@INC = map { /(.*)/; $1 } @INC;
}

# the following Inline configuration sidesteps an "insecure
# dependency" fatal error when run under -T
use Inline (Config =>
DIRECTORY => '/home/jones/tmp/_Inline');

use Inline C;

use strict;
use warnings FATAL => 'all';

my $v = 42;
print +(is_tainted($v) ? 1 : 0), "\n";
taint($v);
print +(is_tainted($v) ? 1 : 0), "\n";

sub is_tainted {
# adapted from code in Programming Perl 3d ed, p. 561
my $arg = shift;
my $empty = do {
no warnings 'uninitialized';
substr( $arg, 0, 0 );
};
local $@;
eval { eval "# $empty" };
return length( $@ ) != 0;
}

__END__
__C__

void taint(SV *sv) {
SvTAINT(sv);
TAINT;
return;
}
 
K

kj

And, for reference, $^X is always tainted.
There are also various modules on CPAN with Taint in their names, some
of which will let you turn taint mode on/off and taint/untaint variables
directly.

Ben, sreservoir, thanks for your replies!

~K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top