@@ -97,7 +97,7 @@ static int
9797_PyTime_DoubleToDenominator (double d , time_t * sec , long * numerator ,
9898 double denominator , _PyTime_round_t round )
9999{
100- double intpart , err ;
100+ double intpart ;
101101 /* volatile avoids optimization changing how numbers are rounded */
102102 volatile double floatpart ;
103103
@@ -115,14 +115,13 @@ _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
115115 }
116116 assert (0.0 <= floatpart && floatpart < denominator );
117117
118- * sec = (time_t )intpart ;
119- * numerator = (long )floatpart ;
120-
121- err = intpart - (double )* sec ;
122- if (err <= -1.0 || err >= 1.0 ) {
118+ if (!_Py_InIntegralTypeRange (time_t , intpart )) {
123119 error_time_t_overflow ();
124120 return -1 ;
125121 }
122+ * sec = (time_t )intpart ;
123+ * numerator = (long )floatpart ;
124+
126125 return 0 ;
127126}
128127
@@ -150,20 +149,19 @@ int
150149_PyTime_ObjectToTime_t (PyObject * obj , time_t * sec , _PyTime_round_t round )
151150{
152151 if (PyFloat_Check (obj )) {
153- double intpart , err ;
152+ double intpart ;
154153 /* volatile avoids optimization changing how numbers are rounded */
155154 volatile double d ;
156155
157156 d = PyFloat_AsDouble (obj );
158157 d = _PyTime_Round (d , round );
159158 (void )modf (d , & intpart );
160159
161- * sec = (time_t )intpart ;
162- err = intpart - (double )* sec ;
163- if (err <= -1.0 || err >= 1.0 ) {
160+ if (!_Py_InIntegralTypeRange (time_t , intpart )) {
164161 error_time_t_overflow ();
165162 return -1 ;
166163 }
164+ * sec = (time_t )intpart ;
167165 return 0 ;
168166 }
169167 else {
@@ -180,7 +178,9 @@ _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
180178{
181179 int res ;
182180 res = _PyTime_ObjectToDenominator (obj , sec , nsec , 1e9 , round );
183- assert (0 <= * nsec && * nsec < SEC_TO_NS );
181+ if (res == 0 ) {
182+ assert (0 <= * nsec && * nsec < SEC_TO_NS );
183+ }
184184 return res ;
185185}
186186
@@ -190,7 +190,9 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
190190{
191191 int res ;
192192 res = _PyTime_ObjectToDenominator (obj , sec , usec , 1e6 , round );
193- assert (0 <= * usec && * usec < SEC_TO_US );
193+ if (res == 0 ) {
194+ assert (0 <= * usec && * usec < SEC_TO_US );
195+ }
194196 return res ;
195197}
196198
@@ -276,7 +278,6 @@ static int
276278_PyTime_FromFloatObject (_PyTime_t * t , double value , _PyTime_round_t round ,
277279 long unit_to_ns )
278280{
279- double err ;
280281 /* volatile avoids optimization changing how numbers are rounded */
281282 volatile double d ;
282283
@@ -285,12 +286,11 @@ _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
285286 d *= (double )unit_to_ns ;
286287 d = _PyTime_Round (d , round );
287288
288- * t = (_PyTime_t )d ;
289- err = d - (double )* t ;
290- if (fabs (err ) >= 1.0 ) {
289+ if (!_Py_InIntegralTypeRange (_PyTime_t , d )) {
291290 _PyTime_overflow ();
292291 return -1 ;
293292 }
293+ * t = (_PyTime_t )d ;
294294 return 0 ;
295295}
296296
0 commit comments