Skip to content

Commit bbdda21

Browse files
committed
Move assertion inside _PyTime_ObjectToTimeval()
Change also _PyTime_FromSeconds() assertion to ensure that the _PyTime_t type is used.
1 parent 53e137c commit bbdda21

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Modules/_datetimemodule.c

-1
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,6 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
41004100
if (_PyTime_ObjectToTimeval(timestamp,
41014101
&timet, &us, _PyTime_ROUND_FLOOR) == -1)
41024102
return NULL;
4103-
assert(0 <= us && us <= 999999);
41044103

41054104
return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
41064105
}

Python/pytime.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ static int
101101
_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
102102
double denominator, _PyTime_round_t round)
103103
{
104-
assert(denominator <= LONG_MAX);
104+
assert(denominator <= (double)LONG_MAX);
105+
105106
if (PyFloat_Check(obj)) {
106107
double d = PyFloat_AsDouble(obj);
107108
return _PyTime_DoubleToDenominator(d, sec, numerator,
@@ -149,14 +150,20 @@ int
149150
_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
150151
_PyTime_round_t round)
151152
{
152-
return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
153+
int res;
154+
res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
155+
assert(0 <= *nsec && *nsec < SEC_TO_NS );
156+
return res;
153157
}
154158

155159
int
156160
_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
157161
_PyTime_round_t round)
158162
{
159-
return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
163+
int res;
164+
res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
165+
assert(0 <= *usec && *usec < SEC_TO_US );
166+
return res;
160167
}
161168

162169
static void
@@ -170,12 +177,13 @@ _PyTime_t
170177
_PyTime_FromSeconds(int seconds)
171178
{
172179
_PyTime_t t;
180+
t = (_PyTime_t)seconds;
173181
/* ensure that integer overflow cannot happen, int type should have 32
174182
bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
175183
bits). */
176-
assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS)
177-
|| (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS));
178-
t = (_PyTime_t)seconds * SEC_TO_NS;
184+
assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
185+
|| (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
186+
t *= SEC_TO_NS;
179187
return t;
180188
}
181189

0 commit comments

Comments
 (0)