- Issue #3845: In PyRun_SimpleFileExFlags avoid invalid memory access with
  short file names.
diff --git a/Misc/NEWS b/Misc/NEWS
index 1a7aaa6..db86b51 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -204,6 +204,9 @@
 
 - The re.sub(), re.subn() and re.split() functions now accept a flags parameter.
 
+- Issue #3845: In PyRun_SimpleFileExFlags avoid invalid memory access with
+  short file names.
+
 Library
 -------
 
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index b3866ce..8fc0ca1 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -898,7 +898,7 @@
 {
 	PyObject *m, *d, *v;
 	const char *ext;
-	int set_file_name = 0, ret;
+	int set_file_name = 0, ret, len;
 
 	m = PyImport_AddModule("__main__");
 	if (m == NULL)
@@ -915,7 +915,8 @@
 		set_file_name = 1;
 		Py_DECREF(f);
 	}
-	ext = filename + strlen(filename) - 4;
+	len = strlen(filename);
+	ext = filename + len - (len > 4 ? 4 : 0);
 	if (maybe_pyc_file(fp, filename, ext, closeit)) {
 		/* Try to run a pyc file. First, re-open in binary */
 		if (closeit)