Author: guido.van.rossum
Date: Mon Jan 15 01:07:32 2007
New Revision: 53443

Modified:
   python/branches/p3yk/   (props changed)
   python/branches/p3yk/Lib/test/test_socket.py
   python/branches/p3yk/Misc/ACKS
   python/branches/p3yk/Modules/socketmodule.c
Log:
Merged revisions 53434 via svnmerge from 
svn+ssh://[EMAIL PROTECTED]/python/trunk

........
  r53434 | guido.van.rossum | 2007-01-14 09:03:32 -0800 (Sun, 14 Jan 2007) | 3 
lines
  
  Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
  return negative numbers, per the underlying C implementation.
........


Modified: python/branches/p3yk/Lib/test/test_socket.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_socket.py        (original)
+++ python/branches/p3yk/Lib/test/test_socket.py        Mon Jan 15 01:07:32 2007
@@ -310,6 +310,20 @@
             self.assertEqual(swapped & mask, mask)
             self.assertRaises(OverflowError, func, 1L<<34)
 
+    def testNtoHErrors(self):
+        good_values = [ 1, 2, 3, 1L, 2L, 3L ]
+        bad_values = [ -1, -2, -3, -1L, -2L, -3L ]
+        for k in good_values:
+            socket.ntohl(k)
+            socket.ntohs(k)
+            socket.htonl(k)
+            socket.htons(k)
+        for k in bad_values:
+            self.assertRaises(OverflowError, socket.ntohl, k)
+            self.assertRaises(OverflowError, socket.ntohs, k)
+            self.assertRaises(OverflowError, socket.htonl, k)
+            self.assertRaises(OverflowError, socket.htons, k)
+
     def testGetServBy(self):
         eq = self.assertEqual
         # Find one service that exists, then check all the related interfaces.

Modified: python/branches/p3yk/Misc/ACKS
==============================================================================
--- python/branches/p3yk/Misc/ACKS      (original)
+++ python/branches/p3yk/Misc/ACKS      Mon Jan 15 01:07:32 2007
@@ -521,6 +521,7 @@
 Nicholas Riley
 Jean-Claude Rimbault
 Anthony Roach
+Mark Roberts
 Andy Robinson
 Jim Robinson
 Kevin Rodgers

Modified: python/branches/p3yk/Modules/socketmodule.c
==============================================================================
--- python/branches/p3yk/Modules/socketmodule.c (original)
+++ python/branches/p3yk/Modules/socketmodule.c Mon Jan 15 01:07:32 2007
@@ -3468,7 +3468,12 @@
        if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
                return NULL;
        }
-       x2 = (int)ntohs((short)x1);
+       if (x1 < 0) {
+               PyErr_SetString(PyExc_OverflowError,
+                       "can't convert negative number to unsigned long");
+               return NULL;
+       }
+       x2 = (unsigned int)ntohs((unsigned short)x1);
        return PyInt_FromLong(x2);
 }
 
@@ -3505,7 +3510,7 @@
                                    arg->ob_type->tp_name);
        if (x == (unsigned long) -1 && PyErr_Occurred())
                return NULL;
-       return PyInt_FromLong(ntohl(x));
+       return PyLong_FromUnsignedLong(ntohl(x));
 }
 
 PyDoc_STRVAR(ntohl_doc,
@@ -3522,7 +3527,12 @@
        if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
                return NULL;
        }
-       x2 = (int)htons((short)x1);
+       if (x1 < 0) {
+               PyErr_SetString(PyExc_OverflowError,
+                       "can't convert negative number to unsigned long");
+               return NULL;
+       }
+       x2 = (unsigned int)htons((unsigned short)x1);
        return PyInt_FromLong(x2);
 }
 
@@ -3557,7 +3567,7 @@
                return PyErr_Format(PyExc_TypeError,
                                    "expected int/long, %s found",
                                    arg->ob_type->tp_name);
-       return PyInt_FromLong(htonl(x));
+       return PyLong_FromUnsignedLong(htonl((unsigned long)x));
 }
 
 PyDoc_STRVAR(htonl_doc,
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to