Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2634,13 +2634,14 @@ _posix_free(void *module)
static PyObject *
stat_nanosecond_timestamp(_posixstate *state, time_t sec, unsigned long nsec)
{
#if SIZEOF_LONG >= 8
#if SIZEOF_TIME_T == 4
return PyLong_FromLongLong(sec * SEC_TO_NS + nsec);
Copy link
Member

Choose a reason for hiding this comment

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

What is the type of SEC_TO_NS? Should not it or sec be cast to long long?

We will get a warning also in the very unlikely case of more than 64-bit long long. Maybe use condition SIZEOF_TIME_T + 4 <= SIZEOF_LONG_LONG?

Copy link
Member Author

Choose a reason for hiding this comment

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

What is the type of SEC_TO_NS?

It's a long long: it uses the LL suffix.

Copy link
Member Author

Choose a reason for hiding this comment

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

We will get a warning also in the very unlikely case of more than 64-bit long long.

I don't see which kind of warning would be emitted if long long is larger than 64-bit?

Copy link
Member

Choose a reason for hiding this comment

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

The same warning. If long long is 128-bit and time_t is 64-bit, then the condition (LLONG_MIN/SEC_TO_NS) <= sec && sec <= (LLONG_MAX/SEC_TO_NS - 1) will be tested, and it will be always true.

I do not know if Python is currently used on any platform with larger than 64-bit long long. This is hypothetical.

Copy link
Member

Choose a reason for hiding this comment

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

Anyway, this would be just a warning. Not wrong code or missed optimization.

Copy link
Member Author

Choose a reason for hiding this comment

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

If long long is 128-bit and time_t is 64-bit

Oh ok. Well, I expect many warnings if long long is larger than 64-bit, not just on these lines. We can revisit the code once such platform will exist :-)

#else
/* 1677-09-21 00:12:44 to 2262-04-11 23:47:15 UTC inclusive */
if ((LLONG_MIN/SEC_TO_NS) <= sec && sec <= (LLONG_MAX/SEC_TO_NS - 1)) {
return PyLong_FromLongLong(sec * SEC_TO_NS + nsec);
}
else
#endif
{
PyObject *ns_total = NULL;
PyObject *s_in_ns = NULL;
Expand All @@ -2663,6 +2664,7 @@ stat_nanosecond_timestamp(_posixstate *state, time_t sec, unsigned long nsec)
Py_XDECREF(s_in_ns);
return ns_total;
}
#endif
}

static int
Expand Down
Loading