[Python-bugs-list] [ python-Bugs-568322 ] socket module htonl/ntohl bug

noreply@sourceforge.net noreply@sourceforge.net
Thu, 25 Jul 2002 09:08:00 -0700


Bugs item #568322, was opened at 2002-06-13 01:56
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=568322&group_id=5470

Category: Python Library
Group: Python 2.2.1
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: David Margrave (davidma)
Assigned to: Jeremy Hylton (jhylton)
Summary: socket module htonl/ntohl bug

Initial Comment:

Hello,

The following statement should work, but does not, on
python 1.5.2 and python 2.2:

>>> socket.htonl(pow(2L,32)-1)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
OverflowError: long int too long to convert

2^32-1 is a valid 32-bit value, equal to 32 1s in
binary, and htonl
should be able to handle it.  

By way of comparison, the following simple C program
works properly:

#include <stdio.h>
#include <sys/socket.h>

int main(void)
{
        printf ("%u\n", htonl(4294967295));
}

When run, it prints out '4294967295' as expected.


Dave


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

>Comment By: Jeremy Hylton (jhylton)
Date: 2002-07-25 16:08

Message:
Logged In: YES 
user_id=31392

Fixed in rev.  1.236 of socketmodule.c.
These functions will now accepts a long L such that 0 <= L <
2**32.


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

Comment By: Jeremy Hylton (jhylton)
Date: 2002-07-25 15:25

Message:
Logged In: YES 
user_id=31392

The problem here is that the functions are currently
implemented to only accept integers.  Note that you can pass
0xffffffff == -1 and get the result you expect.  If you're
using the struct module to unpack the raw bytes, you'll be
getting -1 anyway.  But it seems reasonable enough to extend
the functions to accept longs that are < 2**32.


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

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