From 80964887a4e6813ad84241fb0499715863cdb4e9 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 19 Nov 2026 14:45:50 -0500 Subject: [PATCH] gh-162285: Consume XML-RPC character data once per element For nested elements, each closing tag joined the same text fragments and retained another copy on the value stack. Clear the fragments after joining them to avoid quadratic memory use. --- Lib/test/test_xmlrpc.py | 10 ++++++++++ Lib/xmlrpc/client.py | 4 +++- .../2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index ee0e24f6e86ae3..e8ef537b84e2cb 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -252,6 +252,16 @@ def test_loads_unsupported(self): '') self.assertRaises(ResponseError, xmlrpclib.loads, data) + def test_loads_nested_scalars(self): + template = '%s' + data = template % 'a' + values, _ = xmlrpclib.loads(data) + self.assertEqual(values, ('ab', '')) + + data = template % '12' + values, _ = xmlrpclib.loads(data) + self.assertEqual(values, (1, 2)) + def check_loads(self, s, value, **kwargs): dump = '%s' % s result, m = xmlrpclib.loads(dump, **kwargs) diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py index 84e4e4d11a7319..0d26ad03c51e73 100644 --- a/Lib/xmlrpc/client.py +++ b/Lib/xmlrpc/client.py @@ -677,7 +677,9 @@ def end(self, tag): f = self.dispatch[tag.split(':')[-1]] except KeyError: return # unknown tag ? - return f(self, "".join(self._data)) + data = "".join(self._data) + self._data.clear() + return f(self, data) # # accelerator support diff --git a/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst b/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst new file mode 100644 index 00000000000000..f839943746e10d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst @@ -0,0 +1,2 @@ +Fix quadratic memory use in :mod:`xmlrpc.client` when decoding malformed +XML-RPC packets containing nested string elements.