[Groonga-commit] groonga/groonga at 9f08e16 [master] grn_ts: do a validity check in grn_ts_expr_const_node_open()

Zurück zum Archiv-Index

susumu.yata null+****@clear*****
Thu Nov 12 14:09:09 JST 2015


susumu.yata	2015-11-12 14:09:09 +0900 (Thu, 12 Nov 2015)

  New Revision: 9f08e16c660ad48bf734d68e6e5ca59fe5d9392a
  https://github.com/groonga/groonga/commit/9f08e16c660ad48bf734d68e6e5ca59fe5d9392a

  Message:
    grn_ts: do a validity check in grn_ts_expr_const_node_open()

  Modified files:
    lib/ts/ts_expr.c
    lib/ts/ts_expr_node.c

  Modified: lib/ts/ts_expr.c (+1 -95)
===================================================================
--- lib/ts/ts_expr.c    2015-11-12 13:38:28 +0900 (7288ed5)
+++ lib/ts/ts_expr.c    2015-11-12 14:09:09 +0900 (a87221e)
@@ -36,99 +36,6 @@
 #include "ts_expr_parser.h"
 
 /*-------------------------------------------------------------
- * Built-in data kinds.
- */
-
-/* grn_ts_bool_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_bool_is_valid(grn_ts_bool value) {
-  return GRN_TRUE;
-}
-
-/* grn_ts_int_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_int_is_valid(grn_ts_int value) {
-  return GRN_TRUE;
-}
-
-/* grn_ts_float_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_float_is_valid(grn_ts_float value) {
-  return isfinite(value);
-}
-
-/* grn_ts_time_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_time_is_valid(grn_ts_time value) {
-  return GRN_TRUE;
-}
-
-/* grn_ts_text_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_text_is_valid(grn_ts_text value) {
-  return value.ptr || !value.size;
-}
-
-/* grn_ts_geo_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_geo_is_valid(grn_ts_geo value) {
-  return ((value.latitude >= GRN_GEO_MIN_LATITUDE) &&
-          (value.latitude <= GRN_GEO_MAX_LATITUDE)) &&
-         ((value.longitude >= GRN_GEO_MIN_LONGITUDE) &&
-          (value.longitude <= GRN_GEO_MAX_LONGITUDE));
-}
-
-#define GRN_TS_VECTOR_IS_VALID(type)\
-  if (value.size) {\
-    size_t i;\
-    if (!value.ptr) {\
-      return GRN_FALSE;\
-    }\
-    for (i = 0; i < value.size; i++) {\
-      if (!grn_ts_ ## type ## _is_valid(value.ptr[i])) {\
-        return GRN_FALSE;\
-      }\
-    }\
-  }\
-  return GRN_TRUE;
-/* grn_ts_bool_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_bool_vector_is_valid(grn_ts_bool_vector value) {
-  GRN_TS_VECTOR_IS_VALID(bool)
-}
-
-/* grn_ts_int_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_int_vector_is_valid(grn_ts_int_vector value) {
-  GRN_TS_VECTOR_IS_VALID(int)
-}
-
-/* grn_ts_float_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_float_vector_is_valid(grn_ts_float_vector value) {
-  GRN_TS_VECTOR_IS_VALID(float)
-}
-
-/* grn_ts_time_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_time_vector_is_valid(grn_ts_time_vector value) {
-  GRN_TS_VECTOR_IS_VALID(time)
-}
-
-/* grn_ts_text_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_text_vector_is_valid(grn_ts_text_vector value) {
-  GRN_TS_VECTOR_IS_VALID(text)
-}
-
-/* grn_ts_geo_vector_is_valid() returns whether a value is valid or not. */
-inline static grn_ts_bool
-grn_ts_geo_vector_is_valid(grn_ts_geo_vector value) {
-  GRN_TS_VECTOR_IS_VALID(geo)
-}
-#undef GRN_TS_VECTOR_IS_VALID
-
-/*-------------------------------------------------------------
  * grn_ts_expr_bridge.
  */
 
@@ -800,8 +707,7 @@ grn_ts_expr_push_op(grn_ctx *ctx, grn_ts_expr *expr, grn_ts_op_type op_type) {
   if (!ctx) {\
     return GRN_INVALID_ARGUMENT;\
   }\
-  if (!expr || (expr->type != GRN_TS_EXPR_INCOMPLETE) ||\
-      !grn_ts_ ## kind ## _is_valid(value)) {\
+  if (!expr || (expr->type != GRN_TS_EXPR_INCOMPLETE)) {\
     GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");\
   }\
   rc = grn_ts_expr_reserve_stack(ctx, expr);\

  Modified: lib/ts/ts_expr_node.c (+34 -1)
===================================================================
--- lib/ts/ts_expr_node.c    2015-11-12 13:38:28 +0900 (e86605d)
+++ lib/ts/ts_expr_node.c    2015-11-12 14:09:09 +0900 (641981c)
@@ -1964,10 +1964,43 @@ grn_ts_expr_const_node_set_vector(grn_ctx *ctx, grn_ts_expr_const_node *node,
 }
 #undef GRN_TS_EXPR_CONST_NODE_SET_VECTOR_CASE
 
+#define GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(KIND, kind)\
+  case GRN_TS_ ## KIND: {\
+    if (!grn_ts_ ## kind ## _is_valid(*(const grn_ts_ ## kind *)value)) {\
+      GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");\
+    }\
+    return GRN_SUCCESS;\
+  }
+static grn_rc
+grn_ts_expr_const_node_check_value(grn_ctx *ctx, grn_ts_data_kind kind,
+                                   const void *value) {
+  switch (kind) {
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(BOOL, bool)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(INT, int)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(FLOAT, float)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(TIME, time)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(TEXT, text)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(GEO, geo)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(BOOL_VECTOR, bool_vector)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(INT_VECTOR, int_vector)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(FLOAT_VECTOR, float_vector)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(TIME_VECTOR, time_vector)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(TEXT_VECTOR, text_vector)
+    GRN_TS_EXPR_CONST_NODE_CHECK_VALUE(GEO_VECTOR, geo_vector)
+    default: {
+      GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");
+    }
+  }
+}
+#undef GRN_TS_EXPR_CONST_NODE_CHECK_VALUE
+
 grn_rc
 grn_ts_expr_const_node_open(grn_ctx *ctx, grn_ts_data_kind kind,
                             const void *value, grn_ts_expr_node **node) {
-  grn_rc rc;
+  grn_rc rc = grn_ts_expr_const_node_check_value(ctx, kind, value);
+  if (rc != GRN_SUCCESS) {
+    return rc;
+  }
   grn_ts_expr_const_node *new_node = GRN_MALLOCN(grn_ts_expr_const_node, 1);
   if (!new_node) {
     GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_MALLOCN failed: %zu x 1",
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index