From 7033dc8adc10fcb7e8d2334e28dc8074a633917c Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Sat, 10 Sep 2022 06:58:45 -0700
Subject: [PATCH] GH-96678: Fix undefined behavior in ceval.c (GH-96708)

(cherry picked from commit 50a70a083d34305a52fac4f5901bff2ead152d68)

Co-authored-by: Mark Shannon <mark@hotpy.org>
---
 .../2022-09-09-13-13-27.gh-issue-96678.vMxi9F.rst         | 1 +
 Python/ceval.c                                            | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-09-09-13-13-27.gh-issue-96678.vMxi9F.rst

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-09-13-13-27.gh-issue-96678.vMxi9F.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-09-13-13-27.gh-issue-96678.vMxi9F.rst
new file mode 100644
index 00000000000..575b52be294
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-09-13-13-27.gh-issue-96678.vMxi9F.rst	
@@ -0,0 +1 @@
+Fix case of undefined behavior in ceval.c
diff --git a/Python/ceval.c b/Python/ceval.c
index 461439b1eda..66fa2eabbdc 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -6166,7 +6166,13 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
     /* Pack other positional arguments into the *args argument */
     if (co->co_flags & CO_VARARGS) {
         PyObject *u = NULL;
-        u = _PyTuple_FromArraySteal(args + n, argcount - n);
+        if (argcount == n) {
+            u = Py_NewRef(&_Py_SINGLETON(tuple_empty));
+        }
+        else {
+            assert(args != NULL);
+            u = _PyTuple_FromArraySteal(args + n, argcount - n);
+        }
         if (u == NULL) {
             goto fail_post_positional;
         }
-- 
GitLab