From 8f82de94069a2f0385149dd4415eeff2a8d4a89a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 14 Feb 2018 07:13:34 +0530 Subject: [PATCH] dupenv apparently returns the size fo the buffer not the string --- src/calibre/utils/windows/winutil.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/windows/winutil.c b/src/calibre/utils/windows/winutil.c index 361369d984..7e3484f636 100644 --- a/src/calibre/utils/windows/winutil.c +++ b/src/calibre/utils/windows/winutil.c @@ -237,8 +237,8 @@ winutil_getenv(PyObject *self, PyObject *args) { wchar_t *buf = NULL; size_t sz = 0; PyObject *ans = NULL; - if (_wdupenv_s(&buf, &sz, q) != 0 || buf == NULL) { ans = Py_None; Py_INCREF(ans); } - else ans = PyUnicode_FromWideChar(buf, sz); + if (_wdupenv_s(&buf, &sz, q) != 0 || buf == NULL || sz == 0) { ans = Py_None; Py_INCREF(ans); } + else ans = PyUnicode_FromWideChar(buf, sz - 1); if (buf) free(buf); return ans; }