From ac181d21985154e148c50fa8aa2f53e2b8f5f888 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 14 Feb 2018 03:36:47 +0530 Subject: [PATCH] Fix CRT SECURE warning in winutil --- src/calibre/test_build.py | 12 +++++++++++- src/calibre/utils/windows/winutil.c | 12 ++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/calibre/test_build.py b/src/calibre/test_build.py index 897e6b51687a..83340e30bf7d 100644 --- a/src/calibre/test_build.py +++ b/src/calibre/test_build.py @@ -12,7 +12,7 @@ Test a binary calibre build to ensure that all needed binary images/libraries have loaded. ''' -import os, ctypes, sys, unittest +import os, ctypes, sys, unittest, time from calibre.constants import plugins, iswindows, islinux, isosx is_ci = os.environ.get('CI', '').lower() == 'true' @@ -135,6 +135,16 @@ def au(x, name): au(v, k) for k in os.environ.keys(): au(winutil.getenv(unicode(k)), 'getenv-' + k) + os.environ['XXXTEST'] = 'YYY' + self.assertEqual(winutil.getenv(u'XXXTEST'), u'YYY') + del os.environ['XXXTEST'] + self.assertIsNone(winutil.getenv(u'XXXTEST')) + t = time.localtime() + fmt = u'%Y%a%b%e%H%M' + for fmt in (fmt, fmt.encode('ascii')): + x = winutil.strftime(fmt, t) + au(x, 'strftime') + self.ae(unicode(time.strftime(fmt, t)), x) def test_sqlite(self): import sqlite3 diff --git a/src/calibre/utils/windows/winutil.c b/src/calibre/utils/windows/winutil.c index 04b5e04f8c6f..361369d984e2 100644 --- a/src/calibre/utils/windows/winutil.c +++ b/src/calibre/utils/windows/winutil.c @@ -232,11 +232,15 @@ winutil_set_max_stdio(PyObject *self, PyObject *args) { static PyObject * winutil_getenv(PyObject *self, PyObject *args) { - const wchar_t *q; + const Py_UNICODE *q; if (!PyArg_ParseTuple(args, "u", &q)) return NULL; - wchar_t *ans = _wgetenv(q); - if (ans == NULL) Py_RETURN_NONE; - return PyUnicode_FromWideChar(ans, wcslen(ans)); + 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 (buf) free(buf); + return ans; } static PyObject*