Kouhei Sutou 2018-10-11 15:36:12 +0900 (Thu, 11 Oct 2018) Revision: 25412a8cdac237d8640c05046d99d9521a6b72c6 https://github.com/groonga/groonga/commit/25412a8cdac237d8640c05046d99d9521a6b72c6 Message: cast_loose: add the default value set check Added files: test/command/suite/select/function/cast_loose/invalid/invalid_default_value.expected test/command/suite/select/function/cast_loose/invalid/invalid_default_value.test Modified files: lib/proc/proc_cast.c Modified: lib/proc/proc_cast.c (+20 -1) =================================================================== --- lib/proc/proc_cast.c 2018-10-11 15:30:37 +0900 (d26bcb15d) +++ lib/proc/proc_cast.c 2018-10-11 15:36:12 +0900 (e71359e87) @@ -56,10 +56,29 @@ func_cast_loose(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_dat default_value = args[2]; casted_value = grn_plugin_proc_alloc(ctx, user_data, type_id, 0); + if (!casted_value) { + return NULL; + } rc = grn_obj_cast(ctx, value, casted_value, GRN_FALSE); if (rc != GRN_SUCCESS) { - grn_obj_cast(ctx, default_value, casted_value, GRN_FALSE); + rc = grn_obj_cast(ctx, default_value, casted_value, GRN_FALSE); + if (rc != GRN_SUCCESS) { + char type_name[GRN_TABLE_MAX_KEY_SIZE]; + int type_name_size; + grn_obj inspected; + type_name_size = grn_obj_name(ctx, type, type_name, sizeof(type_name)); + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, default_value); + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "cast_loose(): failed to set the default value: " + "<%.*s>: <%.*s>", + type_name_size, + type_name, + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + } } return casted_value; Added: test/command/suite/select/function/cast_loose/invalid/invalid_default_value.expected (+40 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/cast_loose/invalid/invalid_default_value.expected 2018-10-11 15:36:12 +0900 (d43709bd6) @@ -0,0 +1,40 @@ +table_create Data TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +load --table Data +[ +{"_key": "abc"} +] +[[0,0.0,0.0],1] +select Data --output_columns '_key, cast_loose(Int32, _key, "xyz")' +[ + [ + [ + -22, + 0.0, + 0.0 + ], + "cast_loose(): failed to set the default value: <Int32>: <\"xyz\">" + ], + [ + [ + [ + 1 + ], + [ + [ + "_key", + "ShortText" + ], + [ + "cast_loose", + null + ] + ], + [ + "abc", + "cast_loose(): failed to set the default value: <Int32>: <\"xyz\">" + ] + ] + ] +] +#|e| cast_loose(): failed to set the default value: <Int32>: <"xyz"> Added: test/command/suite/select/function/cast_loose/invalid/invalid_default_value.test (+6 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/cast_loose/invalid/invalid_default_value.test 2018-10-11 15:36:12 +0900 (9a7379ae2) @@ -0,0 +1,6 @@ +table_create Data TABLE_HASH_KEY ShortText +load --table Data +[ +{"_key": "abc"} +] +select Data --output_columns '_key, cast_loose(Int32, _key, "xyz")' -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20181011/14c2467e/attachment-0001.html>