From da8c23524fce46dcb9db70c609b23ddf3f1e56d3 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 7 Mar 2023 22:14:41 +0900 Subject: [PATCH] mruby-time/time.c (mrb_to_time_t): use `floor` instead of `llround` Since we should generate `t=(sec+usec)` from a floating point number `sec` should be `floor(f)` especially for negative numbers, if the place for `usec` is specified. In contrast, we can `round` the below point digits if we are going to ignore them. But we now use `round` instead of `llround` which directly convert `double` to `long long`, to add boundary check for conversion. --- mrbgems/mruby-time/src/time.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 441824bfe..c48e1ab86 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -247,11 +247,15 @@ mrb_to_time_t(mrb_state *mrb, mrb_value obj, time_t *usec) } if (usec) { - t = (time_t)f; - *usec = (time_t)llround((f - t) * 1.0e+6); + double tt = floor(f); + if (!isfinite(tt)) goto out_of_range; + t = (time_t)tt; + *usec = (time_t)trunc((f - tt) * 1.0e+6); } else { - t = (time_t)llround(f); + double tt = round(f); + if (!isfinite(tt)) goto out_of_range; + t = (time_t)tt; } } break;