From 684f6fe79528b02353d6df991a69301d0c03a992 Mon Sep 17 00:00:00 2001 From: Abhishekpandey123-git Date: Wed, 18 Nov 2026 22:27:45 +0530 Subject: [PATCH 1/2] gh-161924: Reject iconv encoding names with empty stripped charset --- Lib/encodings/__init__.py | 9 ++++- Lib/test/test_codecs.py | 21 +++++++++++ ...026-11-18-17-00-00.gh-issue-161924.fix.rst | 3 ++ Modules/_codecsmodule.c | 36 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-18-17-00-00.gh-issue-161924.fix.rst diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 04594eab8224b3..193a07e575c7ff 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -192,7 +192,14 @@ def iconv_search_function(encoding): name = encoding[len('iconv:'):] else: name = encoding - if not name: + # glibc's iconv_open() strips trailing '/', ',', '//SUFFIX' options, + # and any non-ASCII-alphanumeric characters outside '_-.,:', treating + # a resulting empty charset name as the current locale encoding. + # Require at least one ASCII alphanumeric before the first '/' or ','. + charset, _, _ = name.partition('/') + charset, _, _ = charset.partition(',') + if not any('0' <= c <= '9' or 'A' <= c <= 'Z' or 'a' <= c <= 'z' + for c in charset): return None # Test if the encoding is supported by iconv. try: diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 4492e4d874189f..28ee0b2ab22edb 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3955,6 +3955,27 @@ def test_encode_surrogate_pair(self): self.assertEqual(pair.encode('iconv:' + latin1, 'backslashreplace'), rb'\ud83c\udf89') + def test_empty_stripped_charset(self): + # gh-161924: glibc's iconv_open() strips non-ASCII-alphanumeric + # characters and //options, falling back to the locale encoding + # if nothing remains. Ensure these raise LookupError. + invalid_names = [ + '!!!', + '@', + '()', + '\xe9', + '\ud55c\uae00', + 'iconv:\ud55c\uae00', + ',', + '/', + '//TRANSLIT', + '//abc', + 'iconv://TRANSLIT', + ] + for name in invalid_names: + with self.subTest(name=name): + self.assertRaises(LookupError, codecs.lookup, name) + class ASCIITest(unittest.TestCase): def test_encode(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-18-17-00-00.gh-issue-161924.fix.rst b/Misc/NEWS.d/next/Library/2026-11-18-17-00-00.gh-issue-161924.fix.rst new file mode 100644 index 00000000000000..490fb183389934 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-17-00-00.gh-issue-161924.fix.rst @@ -0,0 +1,3 @@ +Reject encoding names in the :mod:`codecs` iconv search function when the +charset portion before ``/`` or ``,`` contains no ASCII alphanumeric +characters, preventing fallback to the current locale encoding on glibc. diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index dd0ace58cf42fd..81e2c2906f8d58 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -658,6 +658,24 @@ _codecs_iconv_decode_impl(PyObject *module, const char *encoding, Py_buffer *data, const char *errors, int final) /*[clinic end generated code: output=6c6145a9decc2ba8 input=d15a04d7d3a3e0cd]*/ { + /* Reject encodings whose charset portion before '/' or ',' lacks any + * ASCII alphanumeric; glibc's iconv_open() normalizes those to "" and + * silently falls back to the current locale encoding. */ + int has_alnum = 0; + for (const char *p = encoding; *p != '\0' && *p != '/' && *p != ','; p++) { + unsigned char c = (unsigned char)*p; + if ((c >= '0' && c <= '9') || + (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z')) { + has_alnum = 1; + break; + } + } + if (!has_alnum) { + PyErr_Format(PyExc_LookupError, "unknown encoding: %s", encoding); + return NULL; + } + Py_ssize_t consumed = data->len; PyObject *decoded = _PyUnicode_DecodeIconv(encoding, data->buf, data->len, errors, @@ -992,6 +1010,24 @@ _codecs_iconv_encode_impl(PyObject *module, const char *encoding, PyObject *str, const char *errors) /*[clinic end generated code: output=bda0c5acd9be0f17 input=53a1e2028c9e2b43]*/ { + /* Reject encodings whose charset portion before '/' or ',' lacks any + * ASCII alphanumeric; glibc's iconv_open() normalizes those to "" and + * silently falls back to the current locale encoding. */ + int has_alnum = 0; + for (const char *p = encoding; *p != '\0' && *p != '/' && *p != ','; p++) { + unsigned char c = (unsigned char)*p; + if ((c >= '0' && c <= '9') || + (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z')) { + has_alnum = 1; + break; + } + } + if (!has_alnum) { + PyErr_Format(PyExc_LookupError, "unknown encoding: %s", encoding); + return NULL; + } + return codec_tuple(_PyUnicode_EncodeIconv(encoding, str, errors), PyUnicode_GET_LENGTH(str)); } From de83125f1e28d82546a12f11f2d99371cff3a82d Mon Sep 17 00:00:00 2001 From: Abhishekpandey123-git Date: Thu, 19 Nov 2026 12:24:58 +0530 Subject: [PATCH 2/2] gh-161924: Expect DeprecationWarning for non-ASCII encoding names in test --- Lib/test/test_codecs.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 28ee0b2ab22edb..48be6e53d666ad 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3959,22 +3959,33 @@ def test_empty_stripped_charset(self): # gh-161924: glibc's iconv_open() strips non-ASCII-alphanumeric # characters and //options, falling back to the locale encoding # if nothing remains. Ensure these raise LookupError. - invalid_names = [ + ascii_invalid_names = [ '!!!', '@', '()', - '\xe9', - '\ud55c\uae00', - 'iconv:\ud55c\uae00', ',', '/', '//TRANSLIT', '//abc', 'iconv://TRANSLIT', ] - for name in invalid_names: + for name in ascii_invalid_names: with self.subTest(name=name): self.assertRaises(LookupError, codecs.lookup, name) + self.assertRaises(LookupError, codecs.iconv_encode, name, 'a') + self.assertRaises(LookupError, codecs.iconv_decode, name, b'a') + + non_ascii_invalid_names = [ + '\xe9', + '\ud55c\uae00', + 'iconv:\ud55c\uae00', + ] + for name in non_ascii_invalid_names: + with self.subTest(name=name): + with self.assertWarns(DeprecationWarning): + self.assertRaises(LookupError, codecs.lookup, name) + self.assertRaises(LookupError, codecs.iconv_encode, name, 'a') + self.assertRaises(LookupError, codecs.iconv_decode, name, b'a') class ASCIITest(unittest.TestCase):