Skip to content

Commit

Permalink
fix magic and upgraded SV handling for setsockopt()'s OPTVAL
Browse files Browse the repository at this point in the history
The code here checked SV flags before fetching magic, potentially
getting confused if magic fetched changed flags.

This also fixes handling for upgraded SVs, but I'm not sure that can
be tested sufficiently portably.
  • Loading branch information
tonycoz committed Jun 23, 2021
1 parent cf70408 commit 2b96d01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pp_sys.c
Expand Up @@ -2707,13 +2707,14 @@ PP(pp_ssockopt)
case OP_SSOCKOPT: {
const char *buf;
int aint;
SvGETMAGIC(sv);
if (SvPOKp(sv)) {
STRLEN l;
buf = SvPV_const(sv, l);
buf = SvPVbyte_nomg(sv, l);
len = l;
}
else {
aint = (int)SvIV(sv);
aint = (int)SvIV_nomg(sv);
buf = (const char *) &aint;
len = sizeof(int);
}
Expand Down
28 changes: 28 additions & 0 deletions t/io/socket.t
Expand Up @@ -281,6 +281,34 @@ SKIP: {
), "0\n", {}, "fresh socket not inherited across exec");
}

SKIP:
{
my $val;
{
package SetsockoptMagic;
sub TIESCALAR { bless {}, shift }
sub FETCH { $val }
}
# setsockopt() magic
socket(my $sock, PF_INET, SOCK_STREAM, $tcp);
$val = 0;
# set a known value
ok(setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, 1),
"set known SO_REUSEADDR");
isnt(getsockopt($sock, SOL_SOCKET, SO_REUSEADDR), pack("i", 0),
"check that worked");
tie my $m, "SetsockoptMagic";
# trigger the magic with the value 0
$val = pack("i", 0);
my $temp = $m;

$val = 1;
ok(setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, $m),
"set SO_REUSEADDR from magic");
isnt(getsockopt($sock, SOL_SOCKET, SO_REUSEADDR), pack("i", 0),
"check SO_REUSEADDR set correctly");
}

done_testing();

my @child_tests;
Expand Down

0 comments on commit 2b96d01

Please sign in to comment.