]> granicus.if.org Git - python/commitdiff
Fix CID-1414686: PyInit_readline() handles errors (#4647)
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 30 Nov 2017 16:21:07 +0000 (17:21 +0100)
committerGitHub <noreply@github.com>
Thu, 30 Nov 2017 16:21:07 +0000 (17:21 +0100)
Handle PyModule_AddIntConstant() and PyModule_AddStringConstant()
failures. Add also constants before calling setup_readline(), since
setup_readline() registers callbacks which uses a reference to the
module, whereas the module is destroyed if adding constants fails.

Fix Coverity warning:

CID 1414686: Unchecked return value (CHECKED_RETURN)
2. check_return: Calling PyModule_AddStringConstant without checking
return value (as is done elsewhere 45 out of 55 times).

Modules/readline.c

index d0e3b913e57cce206b1fa62d50274902b340ac1c..811fca8cd92a7233512781a484292349835e24f7 100644 (file)
@@ -1352,13 +1352,27 @@ PyInit_readline(void)
     if (m == NULL)
         return NULL;
 
+    if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
+                                RL_READLINE_VERSION) < 0) {
+        goto error;
+    }
+    if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
+                                rl_readline_version) < 0) {
+        goto error;
+    }
+    if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
+                                   rl_library_version) < 0)
+    {
+        goto error;
+    }
+
     mod_state = (readlinestate *) PyModule_GetState(m);
     PyOS_ReadlineFunctionPointer = call_readline;
     setup_readline(mod_state);
 
-    PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
-    PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
-    PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION", rl_library_version);
-
     return m;
+
+error:
+    Py_DECREF(m);
+    return NULL;
 }