From 9d560f42a42c89223cb6f0c66d650c97b44fa3c2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 31 May 2018 14:48:00 +0200 Subject: [PATCH] bpo-33706: Fix pymain_parse_cmdline_impl() Fix a crash in Python initialization when parsing the command line options. Fix memcpy() size parameter: previously, we read one wchar_t after the end of _PyOS_optarg. Moreover, don't copy the trailingg NUL character: we write it manually anyway. Thanks Christoph Gohlke for the bug report and the fix! --- .../Core and Builtins/2018-05-31-14-50-04.bpo-33706.ztlH04.rst | 2 ++ Modules/main.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-05-31-14-50-04.bpo-33706.ztlH04.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-05-31-14-50-04.bpo-33706.ztlH04.rst b/Misc/NEWS.d/next/Core and Builtins/2018-05-31-14-50-04.bpo-33706.ztlH04.rst new file mode 100644 index 00000000000000..d3b8477b21977e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-05-31-14-50-04.bpo-33706.ztlH04.rst @@ -0,0 +1,2 @@ +Fix a crash in Python initialization when parsing the command line options. +Thanks Christoph Gohlke for the bug report and the fix! diff --git a/Modules/main.c b/Modules/main.c index 9cbb3f1ce00394..286ad418fe13a5 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -761,7 +761,7 @@ pymain_parse_cmdline_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline) pymain->err = _Py_INIT_NO_MEMORY(); return -1; } - memcpy(command, _PyOS_optarg, len * sizeof(wchar_t)); + memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t)); command[len - 2] = '\n'; command[len - 1] = 0; pymain->command = command;