Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67edcc9

Browse files
committed
Issue #23517: Fix _PyTime_ObjectToDenominator()
* initialize numerator on overflow error ensure that numerator is smaller than * denominator.
1 parent a53ec7a commit 67edcc9

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

Python/pytime.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ _PyLong_FromTime_t(time_t t)
6060
#endif
6161
}
6262

63+
/* Round to nearest with ties going away from zero (_PyTime_ROUND_HALF_UP). */
6364
static double
6465
_PyTime_RoundHalfUp(double x)
6566
{
@@ -81,32 +82,31 @@ _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
8182

8283
floatpart = modf(d, &intpart);
8384
if (floatpart < 0) {
84-
floatpart = 1.0 + floatpart;
85+
floatpart += 1.0;
8586
intpart -= 1.0;
8687
}
8788

8889
floatpart *= denominator;
8990
if (round == _PyTime_ROUND_HALF_UP)
9091
floatpart = _PyTime_RoundHalfUp(floatpart);
91-
else if (round == _PyTime_ROUND_CEILING) {
92+
else if (round == _PyTime_ROUND_CEILING)
9293
floatpart = ceil(floatpart);
93-
if (floatpart >= denominator) {
94-
floatpart = 0.0;
95-
intpart += 1.0;
96-
}
97-
}
98-
else {
94+
else
9995
floatpart = floor(floatpart);
96+
if (floatpart >= denominator) {
97+
floatpart -= denominator;
98+
intpart += 1.0;
10099
}
100+
assert(0.0 <= floatpart && floatpart < denominator);
101101

102102
*sec = (time_t)intpart;
103+
*numerator = (long)floatpart;
104+
103105
err = intpart - (double)*sec;
104106
if (err <= -1.0 || err >= 1.0) {
105107
error_time_t_overflow();
106108
return -1;
107109
}
108-
109-
*numerator = (long)floatpart;
110110
return 0;
111111
}
112112

@@ -123,9 +123,9 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
123123
}
124124
else {
125125
*sec = _PyLong_AsTime_t(obj);
126+
*numerator = 0;
126127
if (*sec == (time_t)-1 && PyErr_Occurred())
127128
return -1;
128-
*numerator = 0;
129129
return 0;
130130
}
131131
}
@@ -167,7 +167,7 @@ _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
167167
{
168168
int res;
169169
res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
170-
assert(0 <= *nsec && *nsec < SEC_TO_NS );
170+
assert(0 <= *nsec && *nsec < SEC_TO_NS);
171171
return res;
172172
}
173173

@@ -177,7 +177,7 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
177177
{
178178
int res;
179179
res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
180-
assert(0 <= *usec && *usec < SEC_TO_US );
180+
assert(0 <= *usec && *usec < SEC_TO_US);
181181
return res;
182182
}
183183

@@ -444,12 +444,11 @@ _PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
444444
tv->tv_sec += 1;
445445
}
446446

447+
assert(0 <= usec && usec < SEC_TO_US);
448+
tv->tv_usec = usec;
449+
447450
if (res && raise)
448451
_PyTime_overflow();
449-
450-
assert(0 <= usec && usec <= 999999);
451-
452-
tv->tv_usec = usec;
453452
return res;
454453
}
455454

@@ -484,7 +483,7 @@ _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
484483
}
485484
ts->tv_nsec = nsec;
486485

487-
assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
486+
assert(0 <= ts->tv_nsec && ts->tv_nsec < SEC_TO_NS);
488487
return 0;
489488
}
490489
#endif

0 commit comments

Comments
 (0)