Commits

Andrews Medina committed 2102788

improved the uid range check

  • Participants
  • Parent commits 8886e79
  • Branches stdlib-2.7.4-pwd-fix

Comments (0)

Files changed (2)

pypy/module/pwd/interp_pwd.py

 from pypy.interpreter.error import OperationError, operationerrfmt
 from rpython.rlib.rarithmetic import intmask
 
+import sys
+
+
+if sys.maxint == 2147483647:
+    def check_uid_range(space, num):
+        pass
+else:
+    def check_uid_range(space, num):
+        if num < -(1<<31) or num >= (1<<32):
+            msg = "getpwuid(): uid not found"
+            raise OperationError(space.w_KeyError, space.wrap(msg))
+
+
 eci = ExternalCompilationInfo(
     includes=['pwd.h']
     )
         ])
     return space.call_function(w_passwd_struct, w_tuple)
 
+
 def getpwuid(space, w_uid):
     """
     getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,
     Return the password database entry for the given numeric user ID.
     See pwd.__doc__ for more on password database entries.
     """
-    import sys
-    if space.is_true(space.or_(space.gt(w_uid, space.wrap(sys.maxint)),
-                              space.lt(w_uid, space.wrap(-sys.maxint - 1)))):
-        msg = "getpwuid(): uid not found"
-        raise OperationError(space.w_KeyError, space.wrap(msg))
-    uid = space.int_w(w_uid)
+    try:
+        uid = space.int_w(w_uid)
+    except OperationError, e:
+        if e.match(space, space.w_OverflowError):
+            msg = "getpwuid(): uid not found"
+            raise OperationError(space.w_KeyError, space.wrap(msg))
+        raise
+    check_uid_range(space, uid)
     pw = c_getpwuid(uid)
     if not pw:
         raise operationerrfmt(space.w_KeyError,

pypy/module/pwd/test/test_pwd.py

         # should be out of uid_t range
         raises(KeyError, pwd.getpwuid, 2**128)
         raises(KeyError, pwd.getpwuid, -2**128)
+        raises(KeyError, pwd.getpwuid, (1<<32))
+        raises(KeyError, pwd.getpwuid, -(1<<32))
 
     def test_getpwnam(self):
         import pwd