<?xml version="1.0" encoding="windows-1252"?>
<node id="705531" title="SWIG and tainting" created="2008-08-20 11:17:47" updated="2008-08-20 07:17:47">
<type id="120">
perlmeditation</type>
<author id="399589">
w-ber</author>
<data>
<field name="doctext">
&lt;p&gt;Dear monks,&lt;/p&gt;

&lt;p&gt;
I have been battling with annoying issues when developing a CGI script that uses C++ libraries through a &lt;a href="http://www.swig.org"&gt;SWIG&lt;/a&gt; layer. I am using the command-line options &lt;code&gt;-Tw&lt;/code&gt; as well as &lt;code&gt;use strict&lt;/code&gt; -- in other words, strictures and taint mode.
&lt;/p&gt;

&lt;p&gt;
The C++ library in question defines overloaded methods for many classes (naturally). SWIG generates a dynamic dispatch version that simulates overloading in Perl. However, frequently execution of the script would die with &lt;code&gt;No matching function for overloaded 'foo' at something.pm line X&lt;/code&gt;, where 'foo' is the name of the overloaded method. This error occurs when SWIG fails to find a match for the function signature, i.e. the given parameter list. Usually this is caused by a typecasting error, such as trying to supply a floating point number instead of an integer to the function. For example, to explicitly pass an integer, one can use &lt;code&gt;0 + $var&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
In this case, I could not fathom why things would not work. There were problems even after casting integers to integers and strings to strings (using various methods for the latter, such as &lt;code&gt;''.$var&lt;/code&gt;, &lt;code&gt;"$var"&lt;/code&gt;, and &lt;code&gt;sprintf("%s", $var)&lt;/code&gt;). Then I finally decided to take a closer look with [cpan://Devel::Peek]. What do I find? The variable in question is magic, because it's tainted.
&lt;/p&gt;

&lt;p&gt;
The reason for this meditation is that I could not find any information about this on the Internet, nor here at PerlMonks. Searching for SWIG documentation for the C++ and Perl combination -- which I thought would be more popular -- yielded no relevant results. Searching with the error message provided links to various mailing lists and nodes that described similar, but different, issues, and contained solutions I had already tried. [cpan://Data::Dumper] and [cpan://YAML] happily print out the string in question, and give no indication it would be tainted. And [cpan://Devel::Peek] output can be opaque unless you know something about the VM.
&lt;/p&gt;

&lt;p&gt;
This is clearly an issue with SWIG and missing support -- or rather error reporting -- for tainted data, but it certainly was frustrating.
&lt;/p&gt;

&lt;p&gt;
(What was the solution? Untaint with e.g. &lt;code&gt;($var) = $var =~ m{(.*)}&lt;/code&gt;, or rather with a more specific regex. Read more in [perlsec].)
&lt;/p&gt;


&lt;div class="pmsig"&gt;
&lt;div class="pmsig-399589"&gt;
&lt;p&gt;
-- &lt;br&gt;
print "Just Another [href://http://prometheus.frii.com/~gnat/yapc/2000-stages/slide25.html|Perl Adept]\n";
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</field>
</data>
</node>
