[ python-Bugs-811295 ] ntohs on Solaris can return negative values

SourceForge.net noreply at sourceforge.net
Mon Apr 12 15:30:39 EDT 2004


Bugs item #811295, was opened at 2003-09-23 13:23
Message generated for change (Comment added) made by gvanrossum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=811295&group_id=5470

Category: Extension Modules
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Nobody/Anonymous (nobody)
Summary: ntohs on Solaris can return negative values

Initial Comment:
On Solaris, socket.ntohs() can return a negative number
when the argument has bit 15 set. This is not according
to spec or intention or expectation. The bug is because
on Solaris, ntohs() is defined as a macro that returns
its argument unchanged, roughly:

  #define ntohs(x) (x)

The socket extension casts the argument to (short); it
should use (unsigned short) or something similar.

Here's a program showing the problem (run this on
Solaris, or on another big-endian machine where ntohs()
is defined away):

import socket
print socket.ntohs(-1)

This should print 65535 but prints -1.

(Credit to Andrew Davis for finding this!)

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2004-04-12 15:30

Message:
Logged In: YES 
user_id=6380

Hm...  I (now) get the problem with Python 2.2 but not with 
2.3.3.  This is on Sparc Solaris 9. (I like uname -sr better to 
know the release, as long as you know that SunOS 5.8 is 
Solaris 8 and SunOS 5.9 is Solaris 9. Sigh, marketing. :-)

The weird thing is that I don't see any differences between 
the 2.2 and 2.3 version of socket_ntohs()...

But the 2.2 was taken from /opt/swf, so I presume it was 
built by Sun's open source folks, while I built the 2.3.3 
version myself.  Will investigate further...

----------------------------------------------------------------------

Comment By: stefanparvu (sparvu)
Date: 2004-04-12 15:15

Message:
Logged In: YES 
user_id=1019660

My mistake. The problem is in there but only Solaris sparc!
On x86 this is not a problem.

stefan@/export/home/stefan>/opt/sfw/bin/python
Python 2.2.3 (#1, Sep  6 2003, 09:33:14) 
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more
information.
>>> 
>>> 
>>> import socket
>>> print socket.ntohs(-1)
-1


stefan


----------------------------------------------------------------------

Comment By: stefanparvu (sparvu)
Date: 2004-04-12 14:57

Message:
Logged In: YES 
user_id=1019660

Hi,

Just checked this on my solaris 9 box:

Python 2.3.3 (#1, Apr 12 2004, 21:32:46) 
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more
information.
>>> 
>>> 
>>> import socket
>>> print socket.ntohs(-1)
65535


It looks fine. what system are you running on when doing
this test. 
Most likely can be a bug and needs a patch. You can always
check the release number as:

cat /etc/release

Cheers
Stefan Parvu



----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-09-23 13:26

Message:
Logged In: YES 
user_id=6380

Of course, htons has the same problem.  The 'l' versions are
unaffected.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=811295&group_id=5470



More information about the Python-bugs-list mailing list