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

unpack('N', pack('N', -1)) not idempotent #802

Closed
p5pRT opened this issue Nov 2, 1999 · 4 comments
Closed

unpack('N', pack('N', -1)) not idempotent #802

p5pRT opened this issue Nov 2, 1999 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 2, 1999

Migrated from rt.perl.org#1729 (status was 'resolved')

Searchable as RT1729$

@p5pRT
Copy link
Author

p5pRT commented Nov 2, 1999

From ben@mucus.advanced.org

# Sample program demonstrating that unpack('N', pack('N', -1)) is
# not idempotent; on my machine output is​:
# unpack('N', pack('N', -1)) is 4294967295 which is positive
#
$startVal = -1;
$endVal = unpack('N', pack('N', $startVal));

# Uncommeting this fixes the problem
# $endVal = sprintf("%d", $endVal);

# Uncommenting this does not fix the problem
# $endVal = int($endVal);

print "unpack('N', pack('N', $startVal)) is $endVal which is ";
if ($endVal < 0) {
  print "negative\n";
} else {
  print "positive\n";
}

Perl Info


Site configuration information for perl 5.00404:

Configured by torin at Wed Feb  3 00:50:04 PST 1999.

Summary of my perl5 (5.0 patchlevel 4 subversion 4) configuration:
  Platform:
    osname=linux, osvers=2.0.36, archname=i386-linux
    uname='linux perv 2.0.36 #2 wed nov 18 03:00:48 pst 1998 i686 unknown '
    hint=recommended, useposix=true, d_sigaction=define
    bincompat3=n useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=2.7.2.3
    cppflags='-Dbool=char -DHAS_BOOL -D_REENTRANT'
    ccflags ='-Dbool=char -DHAS_BOOL -D_REENTRANT'
    stdchar='char', d_stdstdio=define, usevfork=false
    voidflags=15, castflags=0, d_casti32=define, d_castneg=define
    intsize=4, alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldbm -ldb -ldl -lm -lc -lposix -lcrypt
    libc=, so=so
    useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
	


@INC for perl 5.00404:
	/home/ben/lib/libwww-perl-5.09/
	/usr/lib/perl5/i386-linux/5.004
	/usr/lib/perl5
	/usr/local/lib/site_perl/i386-linux
	/usr/local/lib/site_perl
	.


Environment for perl 5.00404:
    HOME=/home/ben
    LANG (unset)
    LD_LIBRARY_PATH=/usr/local/rvplayer5.0
    LOGDIR (unset)
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/usr/bin:/usr/games:/usr/local/jdk117_v1a/bin:/home/ben/Office51/bin:/home/ben/bin
    PERLLIB=/home/ben/lib/libwww-perl-5.09/
    PERL_BADLANG (unset)
    SHELL=/usr/bin/tcsh

@p5pRT
Copy link
Author

p5pRT commented Nov 2, 1999

From [Unknown Contact. See original ticket]

Ben Teitelbaum <ben@​mucus.advanced.org> wrote

# Sample program demonstrating that unpack('N', pack('N', -1)) is
# not idempotent; on my machine output is​:
# unpack('N', pack('N', -1)) is 4294967295 which is positive

The pack type 'N' (and also 'n', 'v' and 'V') is unsigned. The
documentation is a little coy about this.

Patch (for perl5.005_62) attached.

Mike Guy

Inline Patch
--- ./pod/perlfunc.pod.orig	Tue Nov  2 21:32:06 1999
+++ ./pod/perlfunc.pod	Tue Nov  2 21:33:20 1999
@@ -2769,10 +2769,10 @@
 	   what a local C compiler calls 'long'.  If you want
 	   native-length longs, use the '!' suffix.)
 
-    n	A short in "network" (big-endian) order.
-    N	A long in "network" (big-endian) order.
-    v	A short in "VAX" (little-endian) order.
-    V	A long in "VAX" (little-endian) order.
+    n	An unsigned short in "network" (big-endian) order.
+    N	An unsigned long in "network" (big-endian) order.
+    v	An unsigned short in "VAX" (little-endian) order.
+    V	An unsigned long in "VAX" (little-endian) order.
 	  (These 'shorts' and 'longs' are _exactly_ 16 bits and
 	   _exactly_ 32 bits, respectively.)
 
@@ -2934,7 +2934,7 @@
  	0x12 0x34 0x56 0x78	# little-endian
  	0x78 0x56 0x34 0x12	# big-endian
  
-Basically, the Intel, Alpha, and VAX CPUs and little-endian, while
+Basically, the Intel, Alpha, and VAX CPUs are little-endian, while
 everybody else, for example Motorola m68k/88k, PPC, Sparc, HP PA,
 Power, and Cray are big-endian.  MIPS can be either: Digital used it
 in little-endian mode; SGI uses it in big-endian mode.

End of patch

@p5pRT
Copy link
Author

p5pRT commented Nov 2, 1999

From [Unknown Contact. See original ticket]

On Tue, Nov 02, 1999 at 09​:36​:00PM +0000, mjtg@​cus.cam.ac.uk wrote​:

Ben Teitelbaum <ben@​mucus.advanced.org> wrote
# Sample program demonstrating that unpack('N', pack('N', -1)) is
# not idempotent; on my machine output is​:
# unpack('N', pack('N', -1)) is 4294967295 which is positive

The pack type 'N' (and also 'n', 'v' and 'V') is unsigned. The
documentation is a little coy about this.

Oh thanks! I too got bitten my that recently.

--
"Never ascribe to malice that which can be explained by stupidity."
  via, but not speaking for Deutsche Bank

@p5pRT
Copy link
Author

p5pRT commented Nov 2, 1999

From @jhi

Joshua N Pritikin writes​:

On Tue, Nov 02, 1999 at 09​:36​:00PM +0000, mjtg@​cus.cam.ac.uk wrote​:

Ben Teitelbaum <ben@​mucus.advanced.org> wrote
# Sample program demonstrating that unpack('N', pack('N', -1)) is
# not idempotent; on my machine output is​:
# unpack('N', pack('N', -1)) is 4294967295 which is positive

The pack type 'N' (and also 'n', 'v' and 'V') is unsigned. The
documentation is a little coy about this.

Oh thanks! I too got bitten my that recently.

This patch is also already in the 63-to-be.

--
$jhi++; # http​://www.iki.fi/jhi/
  # There is this special biologist word we use for 'stable'.
  # It is 'dead'. -- Jack Cohen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant