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
Incorrect math when reading a padded number from shared memory #11634
Comments
From markgr@online.noCreated by markgr@online.noSynopsis When reading a number from shared memory that is padded with /0`s and then doing Detail If the fourth field in shmwrite is larger than the value being written, the Work arounds I have posted this on perl monks http://www.perlmonks.org/?node_id=924278 and Example code The following code does nothing sensible (not even error checking) but it does Thanks Mark #!/usr/bin/perl use IPC::SysV qw(IPC_STAT IPC_PRIVATE IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR IPC_RMID); $key=shmget(IPC_PRIVATE,100,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); my $ret=fork(); if($ret>0){ Perl Info
|
From zefram@fysh.orgMark Grant wrote:
Yes, it will. Perl maintains a scalar's numeric value separately from I recommend that you always explicitly stringify the scalar when reading -zefram |
The RT System itself - Status changed from 'new' to 'open' |
From @LeontOn Mon, Sep 5, 2011 at 10:32 PM, Mark Grant <perlbug-followup@perl.org> wrote:
It seems shmread doesn't turn off SvIOK on the variable when writing The patch attached is a quick fix, but I'm wondering if there aren't Leon |
From @Leontshmread.patchdiff --git a/doio.c b/doio.c
index cecc574..1759fea 100644
--- a/doio.c
+++ b/doio.c
@@ -2304,7 +2304,7 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
/* suppress warning when reading into undef var (tchrist 3/Mar/00) */
if (! SvOK(mstr))
sv_setpvs(mstr, "");
- SvPV_force_nolen(mstr);
+ SvPOK_only(mstr);
mbuf = SvGROW(mstr, (STRLEN)msize+1);
Copy(shm + mpos, mbuf, msize, char);
|
From @LeontOn Tue, Sep 6, 2011 at 10:40 AM, Zefram <zefram@fysh.org> wrote:
No, shmread copies the data, it does not map it into a scalar. My Leon |
From @cpansproutOn Tue Sep 06 04:44:45 2011, LeonT wrote:
Is it possible to write tests for this sort of thing? |
From @LeontOn Tue, Sep 6, 2011 at 5:31 PM, Father Chrysostomos via RT
Sure. I've updated my patch and attached a test for this issue, but I Leon |
From @Leontshmread.patchdiff --git a/doio.c b/doio.c
index cecc574..6f6d6b8 100644
--- a/doio.c
+++ b/doio.c
@@ -2304,7 +2304,8 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
/* suppress warning when reading into undef var (tchrist 3/Mar/00) */
if (! SvOK(mstr))
sv_setpvs(mstr, "");
- SvPV_force_nolen(mstr);
+ sv_upgrade(mstr, SVt_PV);
+ SvPOK_only(mstr);
mbuf = SvGROW(mstr, (STRLEN)msize+1);
Copy(shm + mpos, mbuf, msize, char);
|
From @LeontOn Tue, Sep 6, 2011 at 5:31 PM, Father Chrysostomos via RT
Fix with tests attached Leon |
From @Leont0001-Added-SysV-shared-memory-tests.patchFrom 2395d6ca00592a30ca1a71d0539471816293883f Mon Sep 17 00:00:00 2001
From: Leon Timmermans <fawaka@gmail.com>
Date: Tue, 13 Sep 2011 18:34:28 +0200
Subject: [PATCH 1/2] Added SysV shared memory tests
Tests are based on IPC-SysV's tests, though I had to remove a lot for it
not to rely on IPC::SharedMem.
---
MANIFEST | 1 +
t/io/shm.t | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)
create mode 100644 t/io/shm.t
diff --git a/MANIFEST b/MANIFEST
index 16121f8..a52d7a7 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -4810,6 +4810,7 @@ t/io/print.t See if print commands work
t/io/pvbm.t See if PVBMs break IO commands
t/io/read.t See if read works
t/io/say.t See if say works
+t/io/shm.t See if SysV shared memory works
t/io/tell.t See if file seeking works
t/io/through.t See if pipe passes data intact
t/io/utf8.t See if file seeking works
diff --git a/t/io/shm.t b/t/io/shm.t
new file mode 100644
index 0000000..a450679
--- /dev/null
+++ b/t/io/shm.t
@@ -0,0 +1,75 @@
+################################################################################
+#
+# $Revision: 6 $
+# $Author: mhx $
+# $Date: 2010/03/07 16:01:42 +0100 $
+#
+################################################################################
+#
+# Version 2.x, Copyright (C) 2007-2010, Marcus Holland-Moritz <mhx@cpan.org>.
+# Version 1.x, Copyright (C) 1999, Graham Barr <gbarr@pobox.com>.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the same terms as Perl itself.
+#
+################################################################################
+
+BEGIN {
+ if ($ENV{'PERL_CORE'}) {
+ chdir 't' if -d 't';
+ @INC = '../lib' if -d '../lib' && -d '../ext';
+ }
+
+ require Test::More; import Test::More;
+ require Config; import Config;
+
+ if ($ENV{'PERL_CORE'} && $Config{'extensions'} !~ m[\bIPC/SysV\b]) {
+ plan(skip_all => 'IPC::SysV was not built');
+ }
+}
+
+if ($Config{'d_shm'} ne 'define') {
+ plan(skip_all => '$Config{d_shm} undefined');
+}
+
+use sigtrap qw/die normal-signals error-signals/;
+use IPC::SysV qw/ IPC_PRIVATE S_IRWXU IPC_RMID /;
+
+my $key;
+END { shmctl $key, IPC_RMID, 0 if defined $key }
+
+{
+ local $SIG{SYS} = sub { plan(skip_all => "SIGSYS caught") } if exists $SIG{SYS};
+ $key = shmget IPC_PRIVATE, 8, S_IRWXU;
+}
+
+if (not defined $key) {
+ my $info = "IPC::SharedMem->new failed: $!";
+ if ($! == &IPC::SysV::ENOSPC || $! == &IPC::SysV::ENOSYS ||
+ $! == &IPC::SysV::ENOMEM || $! == &IPC::SysV::EACCES) {
+ plan(skip_all => $info);
+ }
+ else {
+ die $info;
+ }
+}
+else {
+ plan(tests => 11);
+ pass('acquired shared mem');
+}
+
+ok(shmwrite($key, pack("N", 4711), 0, 4), 'write(offs=0)');
+ok(shmwrite($key, pack("N", 210577), 4, 4), 'write(offs=4)');
+
+my $var;
+ok(shmread($key, $var, 0, 4), 'read(offs=0) returned ok');
+is($var, pack("N", 4711), 'read(offs=0) correct');
+ok(shmread($key, $var, 4, 4), 'read(offs=4) returned ok');
+is($var, pack("N", 210577), 'read(offs=4) correct');
+
+ok(shmwrite($key, "Shared", 1, 6), 'write(offs=1)');
+
+ok(shmread($key, $var, 1, 6), 'read(offs=1) returned ok');
+is($var, 'Shared', 'read(offs=1) correct');
+ok(shmwrite($key,"Memory", 0, 6), 'write(offs=0)');
+
--
1.7.4.1
|
From @Leont0002-Fix-98480-math-when-reading-shared-memory.patchFrom 38c932d1f18ff2003ccac01607311506b81e1714 Mon Sep 17 00:00:00 2001
From: Leon Timmermans <fawaka@gmail.com>
Date: Tue, 13 Sep 2011 20:13:22 +0200
Subject: [PATCH 2/2] Fix #98480 math when reading shared memory
shmread didn't unset SvIOK properly, causing a read into a SVIV to have
an incorrect numeric value. This patch fixes that and adds tests.
---
doio.c | 3 ++-
t/io/shm.t | 8 +++++++-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/doio.c b/doio.c
index cb77cf6..1edf8d2 100644
--- a/doio.c
+++ b/doio.c
@@ -2278,7 +2278,8 @@ Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
/* suppress warning when reading into undef var (tchrist 3/Mar/00) */
if (! SvOK(mstr))
sv_setpvs(mstr, "");
- SvPV_force_nolen(mstr);
+ sv_upgrade(mstr, SVt_PV);
+ SvPOK_only(mstr);
mbuf = SvGROW(mstr, (STRLEN)msize+1);
Copy(shm + mpos, mbuf, msize, char);
diff --git a/t/io/shm.t b/t/io/shm.t
index a450679..46bb2e1 100644
--- a/t/io/shm.t
+++ b/t/io/shm.t
@@ -54,7 +54,7 @@ if (not defined $key) {
}
}
else {
- plan(tests => 11);
+ plan(tests => 13);
pass('acquired shared mem');
}
@@ -73,3 +73,9 @@ ok(shmread($key, $var, 1, 6), 'read(offs=1) returned ok');
is($var, 'Shared', 'read(offs=1) correct');
ok(shmwrite($key,"Memory", 0, 6), 'write(offs=0)');
+my $number = 1;
+my $int = 2;
+shmwrite $key, $int, 0, 1;
+shmread $key, $number, 0, 1;
+is("$number", $int, qq{"\$id" eq "$int"});
+cmp_ok($number + 0, '==', $int, "\$id + 0 == $int");
--
1.7.4.1
|
From @LeontOn Tue, Sep 6, 2011 at 5:31 PM, Father Chrysostomos via RT
Fix with tests attached Leon |
From @cpansproutOn Wed Sep 14 08:12:07 2011, LeonT wrote:
It seems you’ve submitted one of those ‘stealth’ patches. For some |
From @cpansproutOn Wed Sep 14 08:11:13 2011, LeonT wrote:
*That’s* odd! Your message showed up twice in RT, once without the |
From @LeontOn Wed, Sep 14, 2011 at 6:56 PM, Father Chrysostomos via RT
It seems gmail screwed up, it appears in my mailbox twice too, once Leon |
From @cpansproutOn Wed Sep 14 08:11:13 2011, LeonT wrote:
|
@cpansprout - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#98480 (status was 'resolved')
Searchable as RT98480$
The text was updated successfully, but these errors were encountered: