This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Segv during call to builtin_execfile in application embedding Python interpreter.
Type: crash Stage: resolved
Components: Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: snoeberger, vstinner
Priority: normal Keywords: patch

Created on 2014-05-23 19:00 by snoeberger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
execfile_invoke.c snoeberger, 2014-05-23 19:00 Example C code with reproduction steps.
execfile.patch snoeberger, 2014-05-26 19:39 review
Messages (3)
msg218988 - (view) Author: Robert Snoeberger (snoeberger) Date: 2014-05-23 19:00
While embedding the Python 2.7 interpreter in an application, I have encountered a crash when the built-in function 'execfile' is invoked with one argument.

A file is attached, execfile_invoke.c, that reproduces the crash. The reproduction steps on my machine are the following:

% gcc -o execfile_invoke execfile_invoke.c -I/usr/include/python2.7 -L/usr/lib -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic
% ./execfile_invoke
Call execfile with one argument...
Segmentation fault

I am using the following version of Python.

Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2

The crash appears to occur because a call to PyEval_GetGlobals returns a NULL PyObject*,

globals = PyEval_GetGlobals();

and PyDict_GetItemString is called before a NULL check is performed.

In the Python 3 function builtin_exec, globals and locals are checked for NULL. If either is NULL, an exception with message "globals and locals cannot be NULL" is set.

if (!globals || !locals) {
            "globals and locals cannot be NULL");
   return NULL;
msg219172 - (view) Author: Robert Snoeberger (snoeberger) Date: 2014-05-26 19:39
I created a patch to add a check for NULL globals or locals. The file execfile.patch is attached. A system error is set with the message "globals and locals cannot be NULL" if either is NULL.

An open question I have is how should I create tests for this patch? I verified that the crash doesn't occur when I run the attached execfile_invoke.c program.
msg342548 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-15 03:07
Python 2.7 is close to it's end of life, Python 3 doesn't have execfile(), and this issue has no activity since 2014. I close the issue.
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65762
2019-05-15 03:07:18vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg342548

resolution: out of date
stage: resolved
2014-05-26 19:39:05snoebergersetfiles: + execfile.patch
keywords: + patch
messages: + msg219172
2014-05-23 19:00:35snoebergercreate