]> granicus.if.org Git - python/commitdiff
Fix chown on 64-bit linux. It needed to take a long (64-bit on 64bit linux) as
authorGregory P. Smith <greg@mad-scientist.com>
Tue, 18 Mar 2008 19:05:32 +0000 (19:05 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Tue, 18 Mar 2008 19:05:32 +0000 (19:05 +0000)
uid and gid input to accept values >=2**31 as valid while still accepting
negative numbers to pass -1 to chown for "no change".

Fixes issue1747858.

This should be backported to release25-maint.

Lib/test/test_posix.py
Modules/posixmodule.c

index 1fb3c4859d2f2ddf47f1911fcafa1fc1b728cbe0..179864ac08972f24cc0dc004a7e9d9815c1278cc 100644 (file)
@@ -9,6 +9,7 @@ except ImportError:
 
 import time
 import os
+import pwd
 import unittest
 import warnings
 warnings.filterwarnings('ignore', '.* potential security risk .*',
@@ -141,6 +142,33 @@ class PosixTester(unittest.TestCase):
         if hasattr(posix, 'stat'):
             self.assert_(posix.stat(test_support.TESTFN))
 
+    if hasattr(posix, 'chown'):
+        def test_chown(self):
+            # raise an OSError if the file does not exist
+            os.unlink(test_support.TESTFN)
+            self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
+
+            # re-create the file
+            open(test_support.TESTFN, 'w').close()
+            if os.getuid() == 0:
+                try:
+                    # Many linux distros have a nfsnobody user as MAX_UID-2
+                    # that makes a good test case for signedness issues.
+                    #   http://bugs.python.org/issue1747858
+                    # This part of the test only runs when run as root.
+                    # Only scary people run their tests as root.
+                    ent = pwd.getpwnam('nfsnobody')
+                    posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
+                except KeyError:
+                    pass
+            else:
+                # non-root cannot chown to root, raises OSError
+                self.assertRaises(OSError, posix.chown,
+                                  test_support.TESTFN, 0, 0)
+
+            # test a successful chown call
+            posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
+
     def test_chdir(self):
         if hasattr(posix, 'chdir'):
             posix.chdir(os.curdir)
index f73b73e043c55fd09f22c0f1ba638defc53f4607..6a72166f5ecab5e5266b8b5e0ab8e14df9189b62 100644 (file)
@@ -1881,9 +1881,9 @@ static PyObject *
 posix_chown(PyObject *self, PyObject *args)
 {
        char *path = NULL;
-       int uid, gid;
+       long uid, gid;
        int res;
-       if (!PyArg_ParseTuple(args, "etii:chown",
+       if (!PyArg_ParseTuple(args, "etll:chown",
                              Py_FileSystemDefaultEncoding, &path,
                              &uid, &gid))
                return NULL;