Skip to content

Conversation

denis-osipov
Copy link
Contributor

@denis-osipov denis-osipov commented Sep 25, 2017

Use Windows APIs to avoid wrong encoding of time zone names: bpo-16322 and bpo-27426.
Switch back to using wcsftime because:

https://bugs.python.org/issue16322

@@ -109,6 +109,20 @@ win_perf_counter(_Py_clock_info_t *info)
}
return PyFloat_FromDouble(diff / (double)cpu_frequency);
}

// Function to get time zone name with Windows API
static void get_windows_zone(wchar_t *out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move "static void" on a separate line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

TIME_ZONE_INFORMATION tzi;
DWORD tzid = GetTimeZoneInformation(&tzi);

if (tzid < 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to use named constants? TIME_ZONE_ID_STANDARD, TIME_ZONE_ID_DAYLIGHT, etc.

Handle error (TIME_ZONE_ID_INVALID).

@@ -622,27 +656,27 @@ time_strftime(PyObject *self, PyObject *args)
fmt = PyBytes_AS_STRING(format);
#endif

#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What abut the case defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)?

Copy link
Contributor Author

@denis-osipov denis-osipov Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serhiy-storchaka If I'm not mistaken, this case is possible only when we undefine HAVE_WCSFTIME somewhere. Do you mean we should have special code for this case?
Now I just returned this block to state before switching from wcsftime to strftime.


//Replace %Z with time zone name
if (count) {
size_t l = wcslen(fmt) + (len_zone - 2) * count + 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for integer overflow.

if (len_zone - 2 > (Py_SSIZE_MAX/sizeof(time_char) - 1 - wcslen(fmt)) / count) {
    // raise MemoryError
}


// Count the number of %Z occurences
ins = fmt;
for (count = 0; tmp = wcsstr(ins, L"%Z"); ++count) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the same variable for ins and tmp here.

wrong encoding of time zone names.*/
#ifdef MS_WINDOWS
TIME_ZONE_INFORMATION tzi;
GetTimeZoneInformation(&tzi);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle error (TIME_ZONE_ID_INVALID).

Is DaylightName initialized properly if GetTimeZoneInformation() != TIME_ZONE_ID_DAYLIGHT?

Copy link
Contributor Author

@denis-osipov denis-osipov Sep 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so. TIME_ZONE_INFORMATION structure get all members if GetTimeZoneInformation() succeeds. On my system GetTimeZoneInformation() == TIME_ZONE_ID_UNKNOWN, i.e. daylight saving time is not used, but both names initialized properly. However, DaylightName and StandardName can be empty.

/* check that the format string contains only valid directives */
for (outbuf = strchr(fmt, '%');
for (outbuf = wcschr(fmt, L'%');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loops for Windows, AIX and Solaris are too similar. Can they be merged?

while (count--) {
ins = wcsstr(fmt, L"%Z");
len_copy = ins - fmt;
if (wcsncmp(ins - 1, L"%", 1) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the format starts with a "%Z", ins == fmt and ins - 1 is outside of a buffer.

If "%Z" follows a double "%" ("%%%Z"), it is copied as is.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@denis-osipov
Copy link
Contributor Author

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@serhiy-storchaka: please review the changes made to this pull request.

@serhiy-storchaka serhiy-storchaka self-assigned this Oct 13, 2017
@denis-osipov denis-osipov changed the title bpo-16322, bpo-27426: Fix time zone names encoding issues in Windows [memory leak]bpo-16322, bpo-27426: Fix time zone names encoding issues in Windows Oct 19, 2017
@denis-osipov
Copy link
Contributor Author

Oops... Fix of memory leak needed.

./python.bat -bb -E -Wd -m test test_strftime -r -uall -j0 -R :
...
test_strftime leaked [1, 2, 1, 1] memory blocks, sum=5
...

@serhiy-storchaka serhiy-storchaka removed their assignment Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants