[Groonga-commit] groonga/groonga [master] fix time usec overflow.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 9月 17日 (金) 11:41:58 JST


Kouhei Sutou	2010-09-17 02:41:58 +0000 (Fri, 17 Sep 2010)

  New Revision: 42845d91d7fba69b2026dc991ff2950473b63536

  Log:
    fix time usec overflow.

  Modified files:
    lib/ctx.c
    lib/ctx.h
    lib/db.c

  Modified: lib/ctx.c (+5 -6)
===================================================================
--- lib/ctx.c    2010-09-15 02:58:53 +0000 (339a999)
+++ lib/ctx.c    2010-09-17 02:41:58 +0000 (80adc9d)
@@ -78,7 +78,7 @@ grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
   time(&t);
   _ftime(&tb);
   tv->tv_sec = (int32_t) t;
-  tv->tv_nsec = tb.millitm / 1000 * GRN_TIME_NSEC_PER_SEC;
+  tv->tv_nsec = tb.millitm * (GRN_TIME_NSEC_PER_SEC / 1000);
   return GRN_SUCCESS;
 #else /* WIN32 */
   struct timeval t;
@@ -86,7 +86,7 @@ grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
     SERR("gettimeofday");
   } else {
     tv->tv_sec = (int32_t) t.tv_sec;
-    tv->tv_nsec = t.tv_usec / GRN_TIME_USEC_PER_SEC * GRN_TIME_NSEC_PER_SEC;
+    tv->tv_nsec = GRN_TIME_USEC_TO_NSEC(t.tv_usec);
   }
   return ctx->rc;
 #endif /* WIN32 */
@@ -99,8 +99,7 @@ grn_time_now(grn_ctx *ctx, grn_obj *obj)
   grn_timeval tv;
   grn_timeval_now(ctx, &tv);
   GRN_TIME_SET(ctx, obj, GRN_TIME_PACK(tv.tv_sec,
-                                      tv.tv_nsec / GRN_TIME_NSEC_PER_SEC *
-                                      GRN_TIME_USEC_PER_SEC));
+                                       GRN_TIME_NSEC_TO_USEC(tv.tv_nsec)));
 }
 
 grn_rc
@@ -170,8 +169,8 @@ grn_str2timeval(const char *str, uint32_t str_len, grn_timeval *tv)
     uv *= 10;
     r2++;
   }
-  if (uv >= GRN_TIME_NSEC_PER_SEC) { return GRN_INVALID_ARGUMENT; }
-  tv->tv_nsec = uv;
+  if (uv >= GRN_TIME_USEC_PER_SEC) { return GRN_INVALID_ARGUMENT; }
+  tv->tv_nsec = GRN_TIME_USEC_TO_NSEC(uv);
   return GRN_SUCCESS;
 }
 

  Modified: lib/ctx.h (+3 -0)
===================================================================
--- lib/ctx.h    2010-09-15 02:58:53 +0000 (b6e5726)
+++ lib/ctx.h    2010-09-17 02:41:58 +0000 (005e069)
@@ -399,6 +399,9 @@ extern grn_timeval grn_starttime;
 #endif /* GRN_TIMEVAL_STR_FORMAT */
 #define GRN_TIME_NSEC_PER_SEC 1000000000
 #define GRN_TIME_NSEC_PER_SEC_F 1000000000.0
+#define GRN_TIME_NSEC_PER_USEC (GRN_TIME_NSEC_PER_SEC / GRN_TIME_USEC_PER_SEC)
+#define GRN_TIME_NSEC_TO_USEC(nsec) ((nsec) / GRN_TIME_NSEC_PER_USEC)
+#define GRN_TIME_USEC_TO_NSEC(usec) ((usec) * GRN_TIME_NSEC_PER_USEC)
 
 #define LAP(prefix,format,...) {\
   uint64_t et;\

  Modified: lib/db.c (+3 -3)
===================================================================
--- lib/db.c    2010-09-15 02:58:53 +0000 (f1982d0)
+++ lib/db.c    2010-09-17 02:41:58 +0000 (900cc83)
@@ -3845,9 +3845,9 @@ grn_obj_cast(grn_ctx *ctx, grn_obj *src, grn_obj *dest, int addp)
           }
           GRN_OBJ_FIN(ctx, &buf);
         }
-        GRN_TIME_SET(ctx, dest, GRN_TIME_PACK((int64_t)v.tv_sec,
-                                              v.tv_nsec / GRN_TIME_NSEC_PER_SEC *
-                                              GRN_TIME_USEC_PER_SEC));
+        GRN_TIME_SET(ctx, dest,
+                     GRN_TIME_PACK((int64_t)v.tv_sec,
+                                   GRN_TIME_NSEC_TO_USEC(v.tv_nsec)));
       }
       break;
     case GRN_DB_INT64 :




Groonga-commit メーリングリストの案内
Back to archive index