Commits

Martin von Löwis  committed 3835695

Backport r45505, r45573, r45576
- 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.

  • Participants
  • Parent commits 4eefce8
  • Branches 2.4

Comments (0)

Files changed (4)

File Doc/lib/libos.tex

 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.

File Lib/test/test_posix.py

             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)
 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.
 

File Modules/posixmodule.c

 {
     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();
                 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;