首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl: Math64::Int64应该在一个“安全”的隔间中工作吗?

Perl: Math64::Int64应该在一个“安全”的隔间中工作吗?
EN

Stack Overflow用户
提问于 2012-07-07 06:01:21
回答 1查看 249关注 0票数 6

(我的第一篇文章堆栈溢出,希望我使用了正确的格式)

我需要评估在“安全”间隔中操作64位整数的代码。由于我同时提供了要评估的脚本和代码,所以我可以进行正常的评估,但我希望通过使用“Safe”来使意外错误更加健壮。

我应该期望Math::Int64在一个“安全”的隔间中正常工作吗?我得到了不同位数的Perl版本,整数位宽和OSes的冲突结果。(见下文)。

在调试器中,所有这些都会以相同的方式失败。

我确信我在某个地方做了一些不正确的假设,但我不知道在哪里。

感谢您所能提供的任何帮助。

代码示例(dummy.pl)

代码语言:javascript
复制
use strict;
use warnings;
use Exporter;
use Safe;
use Math::Int64 qw(int64 hex_to_int64);
#use Math::Int64 ':native_if_available';

my $safe = new Safe 'Root';
$safe->permit( qw(:browse) );
$safe->share( qw/int64 hex_to_int64 my_sprintf/ );

my $X = 0xFF & (hex_to_int64("0xAA12345678") >> 4);
print my_sprintf("X ",$X);

my $Z = evalme('$X = 0xFF & (hex_to_int64("0xAA12345678") >> 4)', $safe);
print my_sprintf("Z ",$Z);

print evalme('my_sprintf("Xe", $X)',$safe);

exit;


sub my_sprintf {
    my ($string, $value) = @_;
    return sprintf( "Value of %s: %X,\tType: %s\n",
                    $string, $value, ref($value) || "scalar"
           );
}

sub evalme {
    my($expr, $safe) = @_;
    my $retval = $safe->reval($expr);
    die "Error: $@\n" if ($@);
    return $retval;
}

执行示例

代码语言:javascript
复制
% perl -w dummy.pl

=== "Win7 x64 SP1, Strawberry Perl 5.12.3" ===
Value of X : 67,        Type: Math::Int64
Can't locate package Exporter for @Math::Int64::ISA at (eval 5) line 1.
...
Can't locate package Exporter for @Math::Int64::ISA at (eval 5) line 1.
Value of Z : 67,        Type: Math::Int64
Value of Xe: 67,        Type: Math::Int64

=== "Win XP SP3 32-bit Strawberry Perl 5.16.0" ===
Value of X : 67,        Type: Math::Int64
Value of Z : 8E,        Type: scalar
Value of Xe: 8E,        Type: scalar


(No ':native-if-available')
=== "Ubuntu 10.04 x86_64 Server, Perl 5.10.1" ===
Value of X : 67,        Type: Math::Int64
Value of Z : 4D,        Type: scalar
Value of Xe: 4D,        Type: scalar

(':native-if-available')
=== "Ubuntu 10.04 x86_64 Server, Perl 5.10.1" ===
Value of X : 67,        Type: scalar
Value of Z : 71,        Type: scalar
Value of Xe: 71,        Type: scalar



% perl -wd dummy.pl

All versions return effectively:

Value of X : 67,        Type: Math::Int64
Error: Undefined subroutine &Math::Int64::hex_to_int64 called at c:/.../perl5db.

 at dummy.pl line 35
        main::evalme('$X = 0xFF & (hex_to_int64("0xAA12345678") >> 4)',
                     'Safe=HASH(xxxxxxxx)'

版本

代码语言:javascript
复制
"Win7 x64 SP1, Strawberry Perl 5.12.3":
  Platform:
    osname=MSWin32, osvers=6.1, archname=MSWin32-x64-multi-thread
    uname='Win32 strawberryperl 5.12.3.0 #1 Sun May 15 09:43:50 2011 x64'
    use64bitint=define, use64bitall=undef, uselongdouble=undef
  Compiler:
    intsize=4, longsize=4, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='long long',
  Characteristics of this binary (from libperl):
     Compile-time options: ... USE_64_BIT_INT

"Win XP 32-bit Strawberry Perl 5.16.0":
  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname='Win32 strawberry-perl 5.16.0.1 #1 Mon May 21 22:07:30 2012 i386'
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
  Compiler:
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long long',
  Characteristics of this binary (from libperl):
     Compile-time options: ...

"Ubuntu 10.04 x86_64 Server, Perl 5.10.1":
  Linux 2.6.32-31-server,
  Platform:
    use64bitint=define, use64bitall=define, uselongdouble=undef
  Compiler:
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
  Characteristics of this binary (from libperl):
     Compile-time options: ... USE_64_BIT_ALL USE_64_BIT_INT
EN

回答 1

Stack Overflow用户

发布于 2012-07-09 17:15:59

这个问题似乎是由于过载导致的,不能在安全的车厢内工作。

重载是one of the ways以规避安全,因此它可能是故意禁用的。如果你真的想知道,你可以在perl5-porters邮件列表上询问。

我不认为这可以从Math::Int64 (顺便说一句,我是它的作者)中解决。

另一方面,您可以尝试编译具有本机64位支持的Perl。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11370234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档