Skip to content

[memory leak]bpo-16322, bpo-27426: Fix time zone names encoding issues in Windows#3740

Closed
denis-osipov wants to merge 21 commits into
python:masterfrom
denis-osipov:fix-time-3
Closed

[memory leak]bpo-16322, bpo-27426: Fix time zone names encoding issues in Windows#3740
denis-osipov wants to merge 21 commits into
python:masterfrom
denis-osipov:fix-time-3

Conversation

@denis-osipov

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

Copy link
Copy Markdown
Contributor

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

Comment thread Modules/timemodule.c Outdated
}

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread Modules/timemodule.c Outdated
TIME_ZONE_INFORMATION tzi;
DWORD tzid = GetTimeZoneInformation(&tzi);

if (tzid < 2) {

Copy link
Copy Markdown
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).

Comment thread Modules/timemodule.c
@@ -622,27 +656,27 @@ time_strftime(PyObject *self, PyObject *args)
fmt = PyBytes_AS_STRING(format);
#endif

Copy link
Copy Markdown
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)?

@denis-osipov denis-osipov Sep 26, 2017

Copy link
Copy Markdown
Contributor Author

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.

Comment thread Modules/timemodule.c
ins = tmp + 2;
}

//Replace %Z with time zone name

Copy link
Copy Markdown
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
}

Comment thread Modules/timemodule.c Outdated
get_windows_zone(zone);
len_zone = wcslen(zone);

// Count the number of %Z occurences

Copy link
Copy Markdown
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.

Comment thread Modules/timemodule.c
PyModule_AddIntConstant(m, "daylight", daylight);
/*bpo-16322, bpo-27426: For Windows use GetTimeZoneInformation() to avoid
wrong encoding of time zone names.*/
#ifdef MS_WINDOWS

Copy link
Copy Markdown
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?

@denis-osipov denis-osipov Sep 30, 2017

Copy link
Copy Markdown
Contributor Author

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.

Comment thread Modules/timemodule.c

#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
/* check that the format string contains only valid directives */

Copy link
Copy Markdown
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?

Comment thread Modules/timemodule.c Outdated
size_t l = wcslen(fmt) + (len_zone - 2) * count + 1;
tmp = result = (time_char *)PyMem_Malloc(l * sizeof(time_char));
while (count--) {
ins = wcsstr(fmt, L"%Z");

Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
Contributor Author

I didn't expect the Spanish Inquisition!

@bedevere-bot

Copy link
Copy Markdown

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
Copy Markdown
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