From b8f82e6febddbd534e0f08ab480367561daa0717 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Mon, 9 Oct 2006 19:29:06 +0000 Subject: [PATCH] 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. --- Doc/lib/libos.tex | 2 +- Lib/test/test_posix.py | 5 +++++ Misc/NEWS | 2 ++ Modules/posixmodule.c | 13 +++++++------ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Doc/lib/libos.tex b/Doc/lib/libos.tex index ceafaaa496be..e84402f7c7da 100644 --- a/Doc/lib/libos.tex +++ b/Doc/lib/libos.tex @@ -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. diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 24e5b1ced623..319b86005111 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 34699b8e3b31..220bbb64c5bf 100644 --- 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. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5919e44c2208..dc7f723a5e6b 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -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; -- 2.47.3