Skip to content

Commit

Permalink
fix heuristics for setsockopt's OPTVAL argument
Browse files Browse the repository at this point in the history
pp_ssockopt used to treat its last argument (OPTVAL) as a packed string
whenever it has string slot set (SvPOKp), but this would be confused
if the argument is an integer but had cached stringified value.
Now it will treat OPTVAL as a packed string only when it does not
contain any valid public numeric value.

Will fix GH #18642.
  • Loading branch information
t-a-k committed May 1, 2021
1 parent 98812e1 commit 52229f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pp_sys.c
Expand Up @@ -2707,7 +2707,7 @@ PP(pp_ssockopt)
case OP_SSOCKOPT: {
const char *buf;
int aint;
if (SvPOKp(sv)) {
if (SvPOKp(sv) && !SvNIOK(sv)) {
STRLEN l;
buf = SvPV_const(sv, l);
len = l;
Expand Down
26 changes: 26 additions & 0 deletions t/io/socket.t
Expand Up @@ -281,6 +281,32 @@ SKIP: {
), "0\n", {}, "fresh socket not inherited across exec");
}

# GH #18642 - test whether setsockopt works with a numeric OPTVAL which also
# has a cached stringified value
SKIP: {
defined(my $IPPROTO_IP = eval { Socket::IPPROTO_IP() })
or skip 'no IPPROTO_IP', 4;
defined(my $IP_TTL = eval { Socket::IP_TTL() })
or skip 'no IP_TTL', 4;

my $sock;
socket($sock, PF_INET, SOCK_STREAM, $tcp) or BAIL_OUT "socket: $!";

my $ttl = 7;
my $integer_only_ttl = 0 + $ttl;
ok(setsockopt($sock, $IPPROTO_IP, $IP_TTL, $integer_only_ttl),
'setsockopt with an integer-only OPTVAL');
my $set_ttl = getsockopt($sock, $IPPROTO_IP, $IP_TTL);
is(unpack('i', $set_ttl // ''), $ttl, 'TTL set to desired value');

my $also_string_ttl = $ttl;
my $string = "$also_string_ttl";
ok(setsockopt($sock, $IPPROTO_IP, $IP_TTL, $also_string_ttl),
'setsockopt with an integer OPTVAL with stringified value');
$set_ttl = getsockopt($sock, $IPPROTO_IP, $IP_TTL);
is(unpack('i', $set_ttl // ''), $ttl, 'TTL set to desired value');
}

done_testing();

my @child_tests;
Expand Down

0 comments on commit 52229f6

Please sign in to comment.