Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #1, Crash with non-0 offset #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions Mmap.xs
Expand Up @@ -224,12 +224,13 @@ mmap(var, len, prot, flags, fh = 0, off_string)
if (!(prot & PROT_WRITE))
SvREADONLY_on(var);

/* would sv_usepvn() be cleaner/better/different? would still try to realloc... */
SvPVX(var) = (char *) addr + slop;
SvCUR_set(var, len);
SvLEN_set(var, slop);
SvPVX(var) = (char *) addr;
SvCUR_set(var, len + slop);
if (slop > 0) {
sv_chop(var, addr + slop);
}
SvPOK_only(var);
ST(0) = sv_2mortal(newSVnv((IV) addr));
ST(0) = sv_2mortal(newSViv((IV) addr));

SV *
munmap(var)
Expand Down
8 changes: 7 additions & 1 deletion t/mmap.t
Expand Up @@ -3,7 +3,7 @@
BEGIN {
use strict;
use warnings;
use Test::More tests => 8;
use Test::More tests => 9;

use_ok('Sys::Mmap');
}
Expand Down Expand Up @@ -38,6 +38,12 @@ substr($temp_file_contents, 3, 1) = "Z";
is($foo, $temp_file_contents, 'Foo can be altered in RW mode');
munmap($foo);

sysopen(FOO, $temp_file, O_RDWR) or die "$temp_file: $!\n";
mmap($foo, 0, PROT_READ|PROT_WRITE, MAP_SHARED, FOO, 5);
close FOO;
is(substr($foo, 5, 1), '2', 'Offset handled');
munmap($foo);

sysopen(FOO, $temp_file, O_RDONLY) or die "$temp_file: $!\n";
my $bar = <FOO>;
is($bar, $temp_file_contents, 'Altered foo reflects on disk');
Expand Down
26 changes: 26 additions & 0 deletions t/on_exit.t
@@ -0,0 +1,26 @@
#! perl

BEGIN {
use strict;
use warnings;
use Test::More tests => 1;

use_ok('Sys::Mmap');
}

use FileHandle;

my $temp_file = "mmap.tmp";

my $temp_file_contents = "0" x 1000;
sysopen(FOO, $temp_file, O_WRONLY|O_CREAT|O_TRUNC) or die "$temp_file: $!\n";
print FOO $temp_file_contents;
close FOO;

# Perl was segfaulting when exiting after using a non zero offset
# The key here is that munmap was not done before exiting
my $foo;
sysopen(FOO, $temp_file, O_RDWR) or die "$temp_file: $!\n";
mmap($foo, 0, PROT_READ, MAP_SHARED, FOO, 256);

unlink($temp_file);