curl-library
Problem if the supported data for CURLOPT_URL goes out of scope (perl bindings)
Date: Thu, 23 Nov 2006 10:31:30 +0100
Hello list curl-library_at_cool.haxx.se,
I posted this on the curl-users some days ago but Daniel pushed me to
this list so I repeat my posting.
I've a problem with the perl binding and I would like to hear other
opinions about this issue. (I'm testing with WWW::Curl::Easy 3.01 and
perl v5.8.8 built for i686-linux.)
It seems that the perl binding got a problem if the supported URL for
the option CURLOPT_URL gets out of scope.
Here's a small example (complete example below):
my $curl = WWW::Curl::Easy->new();
{
my $ref = { dombot => 'http://www.google.de' };
$curl->setopt(CURLOPT_URL, $ref->{dombot});
}
# $ref goes out of scope
$curl->perform() && die $curl->errbuf();
The error message looks like if it contains some garbage of the
memory: "Couldn't resolve host 'À'".
The script below will give you a complete sample script to execute. Just
change the 'BUG' constant at the top of the program.
The expected behavior was that the program runs fine because perl
programs works that way and variables can't go out of scope. From my
point of view this is programmer unfriendly because I have think during
the writing of the code: Mmmmh... is this a perl library or a C
library. Can my string goes out of scope?
How other C libraries deal with this?
Any comments about this?
#!/usr/bin/perl -w
use strict;
use warnings;
use constant BUG => 1;
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new();
my $body = "";
$curl->setopt(CURLOPT_WRITEFUNCTION, sub {
my $chunk = shift;
$body .= $chunk;
return length($chunk);
});
# This scope is needed. Without it the program
# runs fine.
{
my $ref = { dombot => 'http://www.google.de' };
if (BUG)
{
$curl->setopt(CURLOPT_URL, $ref->{dombot});
}
else
{
my $dummy = $ref->{dombot};
$curl->setopt(CURLOPT_URL, $dummy);
}
}
$curl->perform() && die $curl->errbuf();
printf "OK, %d bytes fetched\n", length($body);
-- So long... FuzzReceived on 2006-11-23