FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-41299: QueryPerformanceFrequency() cannot fail by vstinner · Pull Request #28552 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 7 additions & 21 deletions Python/pytime.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1050,26 +1050,14 @@ py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
LONGLONG frequency;

LARGE_INTEGER freq;
if (!QueryPerformanceFrequency(&freq)) {
if (raise) {
PyErr_SetFromWindowsErr(0);
}
return -1;
}
// Since Windows XP, the function cannot fail.
(void)QueryPerformanceFrequency(&freq);

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why query this every time? It's fixed at boot, so it isn't going to change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This function must be called exactly once in a process: py_get_win_perf_counter() uses a static variable for that.

frequency = freq.QuadPart;

/* Sanity check: should never occur in practice */
if (frequency < 1) {
if (raise) {
PyErr_SetString(PyExc_RuntimeError,
"invalid QueryPerformanceFrequency");
}
return -1;
}

/* Check that frequency can be casted to _PyTime_t.
// Since Windows XP, frequency cannot be zero.
assert(frequency >= 1);

Make also sure that (ticks * SEC_TO_NS) cannot overflow in
/* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
_PyTime_MulDiv(), with ticks < frequency.

Known QueryPerformanceFrequency() values:
Expand All @@ -1078,10 +1066,8 @@ py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
* 3,579,545 Hz (3.6 MHz): 279 ns resolution

None of these frequencies can overflow with 64-bit _PyTime_t, but
check for overflow, just in case. */
if (frequency > _PyTime_MAX
|| frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS)
{
check for integer overflow just in case. */
if (frequency > _PyTime_MAX / SEC_TO_NS) {
if (raise) {
PyErr_SetString(PyExc_OverflowError,
"QueryPerformanceFrequency is too large");
Expand Down

Back | FazBrowse Home | New Git URL