Skip to content

Commit d1b3b4a

Browse files
committed
Fix CRT SECURE warning in winutil
1 parent c344c42 commit d1b3b4a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/calibre/test_build.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Test a binary calibre build to ensure that all needed binary images/libraries have loaded.
1313
'''
1414

15-
import os, ctypes, sys, unittest
15+
import os, ctypes, sys, unittest, time
1616
from calibre.constants import plugins, iswindows, islinux, isosx
1717
is_ci = os.environ.get('CI', '').lower() == 'true'
1818

@@ -135,6 +135,16 @@ def au(x, name):
135135
au(v, k)
136136
for k in os.environ.keys():
137137
au(winutil.getenv(unicode(k)), 'getenv-' + k)
138+
os.environ['XXXTEST'] = 'YYY'
139+
self.assertEqual(winutil.getenv(u'XXXTEST'), u'YYY')
140+
del os.environ['XXXTEST']
141+
self.assertIsNone(winutil.getenv(u'XXXTEST'))
142+
t = time.localtime()
143+
fmt = u'%Y%a%b%e%H%M'
144+
for fmt in (fmt, fmt.encode('ascii')):
145+
x = winutil.strftime(fmt, t)
146+
au(x, 'strftime')
147+
self.ae(unicode(time.strftime(fmt, t)), x)
138148

139149
def test_sqlite(self):
140150
import sqlite3

src/calibre/utils/windows/winutil.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,15 @@ winutil_set_max_stdio(PyObject *self, PyObject *args) {
232232

233233
static PyObject *
234234
winutil_getenv(PyObject *self, PyObject *args) {
235-
const wchar_t *q;
235+
const Py_UNICODE *q;
236236
if (!PyArg_ParseTuple(args, "u", &q)) return NULL;
237-
wchar_t *ans = _wgetenv(q);
238-
if (ans == NULL) Py_RETURN_NONE;
239-
return PyUnicode_FromWideChar(ans, wcslen(ans));
237+
wchar_t *buf = NULL;
238+
size_t sz = 0;
239+
PyObject *ans = NULL;
240+
if (_wdupenv_s(&buf, &sz, q) != 0 || buf == NULL) { ans = Py_NONE; Py_INCREF(ans); }
241+
else ans = PyUnicode_FromWideChar(buf, sz);
242+
if (buf) free(buf);
243+
return ans;
240244
}
241245

242246
static PyObject*

0 commit comments

Comments
 (0)