From 6a2c0c6e2a2751ab663166410415151a24bfcd73 Mon Sep 17 00:00:00 2001 From: ken-mu Date: Tue, 23 Jan 2018 12:24:49 +0000 Subject: mruby-time: support time_t is uint --- mrbgems/mruby-time/src/time.c | 17 ++++++++++++++++- mrbgems/mruby-time/test/time.rb | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 98198083b..062851918 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -67,6 +67,11 @@ double round(double x) { /* define following macro to use probably faster timegm() on the platform */ /* #define USE_SYSTEM_TIMEGM */ +/* time_t */ +/* If your platform supports time_t as uint (e.g. uint32_t, uint64_t), */ +/* uncomment following macro. */ +/* #define MRB_TIME_T_UINT */ + /** end of Time class configuration */ #ifndef NO_GETTIMEOFDAY @@ -240,13 +245,21 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) mrb_check_num_exact(mrb, (mrb_float)sec); mrb_check_num_exact(mrb, (mrb_float)usec); - +#ifndef MRB_TIME_T_UINT if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) { goto out_of_range; } if (sizeof(time_t) == 8 && (sec > (double)INT64_MAX || (double)INT64_MIN > sec)) { goto out_of_range; } +#else + if (sizeof(time_t) == 4 && (sec > (double)UINT32_MAX || (double)0 > sec)) { + goto out_of_range; + } + if (sizeof(time_t) == 8 && (sec > (double)UINT64_MAX || (double)0 > sec)) { + goto out_of_range; + } +#endif tsec = (time_t)sec; if ((sec > 0 && tsec < 0) || (sec < 0 && (double)tsec > sec)) { out_of_range: @@ -371,9 +384,11 @@ time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, else { nowsecs = mktime(&nowtime); } +#ifndef MRB_TIME_T_UINT if (nowsecs == (time_t)-1) { mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time."); } +#endif return time_alloc(mrb, (double)nowsecs, (double)ausec, timezone); } diff --git a/mrbgems/mruby-time/test/time.rb b/mrbgems/mruby-time/test/time.rb index 4e7b94a7c..314a7560e 100644 --- a/mrbgems/mruby-time/test/time.rb +++ b/mrbgems/mruby-time/test/time.rb @@ -233,4 +233,4 @@ end assert('Time.gm with Dec 31 23:59:59 1969 raise ArgumentError') do assert_raise(ArgumentError) {Time.gm(1969, 12, 31, 23, 59, 59)} -end \ No newline at end of file +end -- cgit v1.2.3 From a1b71b64b37131064b7dbbb04151640a1b16cf5c Mon Sep 17 00:00:00 2001 From: ken-mu Date: Mon, 29 Jan 2018 09:43:43 +0000 Subject: mruby-time: remove ifdef for mktime error handling --- mrbgems/mruby-time/src/time.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 062851918..cfd51ac63 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -384,11 +384,9 @@ time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, else { nowsecs = mktime(&nowtime); } -#ifndef MRB_TIME_T_UINT if (nowsecs == (time_t)-1) { mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time."); } -#endif return time_alloc(mrb, (double)nowsecs, (double)ausec, timezone); } -- cgit v1.2.3