From 62bd97303eb6d1fb0109e4a57d38c2ba6b0be7ff Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 16 Aug 2021 11:34:23 -0700 Subject: Fix a SystemError in code.replace() (#27771) While the comment said 'We don't bother resizing localspluskinds', this would cause .replace() to crash when it happened. (Also types.CodeType(), but testing that is tedious, and this tests all code paths.) --- Objects/codeobject.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Objects/codeobject.c') diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 3dc9fd787f3..ad8f13a781b 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -471,9 +471,11 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount, localsplusnames, localspluskinds); } // If any cells were args then nlocalsplus will have shrunk. - // We don't bother resizing localspluskinds. - if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0) { - goto error; + if (nlocalsplus != PyTuple_GET_SIZE(localsplusnames)) { + if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0 + || _PyBytes_Resize(&localspluskinds, nlocalsplus) < 0) { + goto error; + } } struct _PyCodeConstructor con = { -- cgit v1.2.3