]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport r45505, r45573, r45576
authorMartin v. Löwis <martin@v.loewis.de>
Mon, 9 Oct 2006 19:29:06 +0000 (19:29 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Mon, 9 Oct 2006 19:29:06 +0000 (19:29 +0000)
- reset errno before calling confstr - use confstr() doc to simplify
  checks afterwards
- Correct implementation and documentation of os.confstr.  Add a simple
  test case.  I've yet to figure out how to provoke a None return I can test.
- Address issues brought up by MvL on python-checkins.
  I tested this with valgrind on amd64.

  The man pages I found for diff architectures are inconsistent on this.
  I'm not entirely sure this change is correct for all architectures
  either.

  Perhaps we should just over-allocate and not worry about it?

The change to return None instead of "" in case of unconfigured
values has not been backported.

Doc/lib/libos.tex
Lib/test/test_posix.py
Misc/NEWS
Modules/posixmodule.c

index ceafaaa496beac356bbdc2311cdf4b92fa39f198..e84402f7c7da254d2b0876adfb65733f3dc1913c 100644 (file)
@@ -1773,7 +1773,7 @@ Return string-valued system configuration values.
 string which is the name of a defined system value; these names are
 specified in a number of standards (\POSIX, \UNIX{} 95, \UNIX{} 98, and
 others).  Some platforms define additional names as well.  The names
-known to the host operating system are given in the
+known to the host operating system are given as the keys of the
 \code{confstr_names} dictionary.  For configuration variables not
 included in that mapping, passing an integer for \var{name} is also
 accepted.
index 24e5b1ced62339a2369ce9969b84dbcafb53877b..319b86005111e835813882f6b7b5a5075d6f3d66 100644 (file)
@@ -73,6 +73,11 @@ class PosixTester(unittest.TestCase):
             finally:
                 fp.close()
 
+    def test_confstr(self):
+        if hasattr(posix, 'confstr'):
+            self.assertRaises(ValueError, posix.confstr, "CS_garbage")
+            self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
+
     def test_dup2(self):
         if hasattr(posix, 'dup2'):
             fp1 = open(test_support.TESTFN)
index 34699b8e3b31d803305299de4dd7ba8494039383..220bbb64c5bfa23ec9efe85968cb9ffce2419f57 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -73,6 +73,8 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Fix buffer handling in posix.confstr. 
+
 - Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault
   when encoding non-BMP unicode characters.
 
index 5919e44c22084f7a13bd9c0ebcae8afb1364d881..dc7f723a5e6b655d0e2e45e78785d5d5b65f7827 100644 (file)
@@ -6599,12 +6599,13 @@ posix_confstr(PyObject *self, PyObject *args)
 {
     PyObject *result = NULL;
     int name;
-    char buffer[64];
+    char buffer[256];
 
     if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
-        int len = confstr(name, buffer, sizeof(buffer));
+       int len;
 
         errno = 0;
+       len = confstr(name, buffer, sizeof(buffer));
         if (len == 0) {
             if (errno != 0)
                 posix_error();
@@ -6612,13 +6613,13 @@ posix_confstr(PyObject *self, PyObject *args)
                 result = PyString_FromString("");
         }
         else {
-            if (len >= sizeof(buffer)) {
-                result = PyString_FromStringAndSize(NULL, len);
+           if ((unsigned int)len >= sizeof(buffer)) {
+                result = PyString_FromStringAndSize(NULL, len-1);
                 if (result != NULL)
-                    confstr(name, PyString_AS_STRING(result), len+1);
+                    confstr(name, PyString_AS_STRING(result), len);
             }
             else
-                result = PyString_FromString(buffer);
+                result = PyString_FromStringAndSize(buffer, len-1);
         }
     }
     return result;