[Groonga-commit] groonga/groonga at a746fc9 [master] cast_loose: add the number of arguments check

Back to archive index
Kouhei Sutou null+****@clear*****
Thu Oct 11 15:27:11 JST 2018


Kouhei Sutou	2018-10-11 15:27:11 +0900 (Thu, 11 Oct 2018)

  Revision: a746fc91a4b00b9892002e532e08e3af357918f9
  https://github.com/groonga/groonga/commit/a746fc91a4b00b9892002e532e08e3af357918f9

  Message:
    cast_loose: add the number of arguments check

  Added files:
    test/command/suite/select/function/cast_loose/invalid/no_default_value.expected
    test/command/suite/select/function/cast_loose/invalid/no_default_value.test
    test/command/suite/select/function/cast_loose/invalid/no_type.expected
    test/command/suite/select/function/cast_loose/invalid/no_type.test
    test/command/suite/select/function/cast_loose/invalid/no_value.expected
    test/command/suite/select/function/cast_loose/invalid/no_value.test
  Modified files:
    lib/proc/proc_cast.c

  Modified: lib/proc/proc_cast.c (+20 -12)
===================================================================
--- lib/proc/proc_cast.c    2018-10-11 15:18:31 +0900 (6e560bb39)
+++ lib/proc/proc_cast.c    2018-10-11 15:27:11 +0900 (c13e555d6)
@@ -24,22 +24,30 @@
 static grn_obj *
 func_cast_loose(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
+  grn_obj *type;
+  grn_obj *value;
+  grn_obj *default_value;
+  grn_id type_id;
+  grn_rc rc;
   grn_obj *casted_value = NULL;
 
-  if (nargs == 3) {
-    grn_obj *type = args[0];
-    grn_obj *cast_value = args[1];
-    grn_obj *default_value = args[2];
-    grn_id type_id;
-    grn_rc rc;
+  if (nargs != 3) {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "cast_loose(): wrong number of arguments (%d for 3)",
+                     nargs);
+    return NULL;
+  }
+
+  type = args[0];
+  value = args[1];
+  default_value = args[2];
 
-    type_id = grn_obj_id(ctx, type);
-    casted_value = grn_plugin_proc_alloc(ctx, user_data, type_id, 0);
+  type_id = grn_obj_id(ctx, type);
+  casted_value = grn_plugin_proc_alloc(ctx, user_data, type_id, 0);
 
-    rc = grn_obj_cast(ctx, cast_value, casted_value, GRN_FALSE);
-    if (rc != GRN_SUCCESS) {
-      grn_obj_cast(ctx, default_value, casted_value, GRN_FALSE);
-    }
+  rc = grn_obj_cast(ctx, value, casted_value, GRN_FALSE);
+  if (rc != GRN_SUCCESS) {
+    grn_obj_cast(ctx, default_value, casted_value, GRN_FALSE);
   }
 
   return casted_value;

  Added: test/command/suite/select/function/cast_loose/invalid/no_default_value.expected (+40 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_default_value.expected    2018-10-11 15:27:11 +0900 (ba0b1017e)
@@ -0,0 +1,40 @@
+table_create Data TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Data
+[
+{"_key": "100"}
+]
+[[0,0.0,0.0],1]
+select Data --output_columns '_key, cast_loose(Int32, _key)'
+[
+  [
+    [
+      -22,
+      0.0,
+      0.0
+    ],
+    "cast_loose(): wrong number of arguments (2 for 3)"
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "cast_loose",
+          null
+        ]
+      ],
+      [
+        "100",
+        "cast_loose(): wrong number of arguments (2 for 3)"
+      ]
+    ]
+  ]
+]
+#|e| cast_loose(): wrong number of arguments (2 for 3)

  Added: test/command/suite/select/function/cast_loose/invalid/no_default_value.test (+6 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_default_value.test    2018-10-11 15:27:11 +0900 (f3b92048c)
@@ -0,0 +1,6 @@
+table_create Data TABLE_HASH_KEY ShortText
+load --table Data
+[
+{"_key": "100"}
+]
+select Data --output_columns '_key, cast_loose(Int32, _key)'

  Added: test/command/suite/select/function/cast_loose/invalid/no_type.expected (+40 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_type.expected    2018-10-11 15:27:11 +0900 (5c0cb0b6d)
@@ -0,0 +1,40 @@
+table_create Data TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Data
+[
+{"_key": "100"}
+]
+[[0,0.0,0.0],1]
+select Data --output_columns '_key, cast_loose()'
+[
+  [
+    [
+      -22,
+      0.0,
+      0.0
+    ],
+    "cast_loose(): wrong number of arguments (0 for 3)"
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "cast_loose",
+          null
+        ]
+      ],
+      [
+        "100",
+        "cast_loose(): wrong number of arguments (0 for 3)"
+      ]
+    ]
+  ]
+]
+#|e| cast_loose(): wrong number of arguments (0 for 3)

  Added: test/command/suite/select/function/cast_loose/invalid/no_type.test (+6 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_type.test    2018-10-11 15:27:11 +0900 (1f5942341)
@@ -0,0 +1,6 @@
+table_create Data TABLE_HASH_KEY ShortText
+load --table Data
+[
+{"_key": "100"}
+]
+select Data --output_columns '_key, cast_loose()'

  Added: test/command/suite/select/function/cast_loose/invalid/no_value.expected (+40 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_value.expected    2018-10-11 15:27:11 +0900 (5c7789b46)
@@ -0,0 +1,40 @@
+table_create Data TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Data
+[
+{"_key": "100"}
+]
+[[0,0.0,0.0],1]
+select Data --output_columns '_key, cast_loose(Int32)'
+[
+  [
+    [
+      -22,
+      0.0,
+      0.0
+    ],
+    "cast_loose(): wrong number of arguments (1 for 3)"
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "cast_loose",
+          null
+        ]
+      ],
+      [
+        "100",
+        "cast_loose(): wrong number of arguments (1 for 3)"
+      ]
+    ]
+  ]
+]
+#|e| cast_loose(): wrong number of arguments (1 for 3)

  Added: test/command/suite/select/function/cast_loose/invalid/no_value.test (+6 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/cast_loose/invalid/no_value.test    2018-10-11 15:27:11 +0900 (881e99d58)
@@ -0,0 +1,6 @@
+table_create Data TABLE_HASH_KEY ShortText
+load --table Data
+[
+{"_key": "100"}
+]
+select Data --output_columns '_key, cast_loose(Int32)'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20181011/d8b4758c/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index