null+****@clear*****
null+****@clear*****
2011年 2月 18日 (金) 14:38:37 JST
Kouhei Sutou 2011-02-18 05:38:37 +0000 (Fri, 18 Feb 2011)
New Revision: 97fb9fb6e1ac4cd211d53f65fc09b37790310268
Log:
add resiter name length check.
Modified files:
lib/plugin.c
test/unit/core/test-plugin.c
Modified: lib/plugin.c (+11 -0)
===================================================================
--- lib/plugin.c 2011-02-18 05:36:24 +0000 (f1b55e3)
+++ lib/plugin.c 2011-02-18 05:38:37 +0000 (29b3a63)
@@ -391,6 +391,7 @@ grn_plugin_register(grn_ctx *ctx, const char *name)
const char *plugins_dir;
char dir_last_char;
char path[PATH_MAX];
+ int name_length, max_name_length;
plugins_dir = getenv("GRN_PLUGINS_DIR");
if (!plugins_dir) {
@@ -401,6 +402,16 @@ grn_plugin_register(grn_ctx *ctx, const char *name)
if (dir_last_char != PATH_SEPARATOR[0]) {
strcat(path, PATH_SEPARATOR);
}
+
+ name_length = strlen(name);
+ max_name_length = PATH_MAX - strlen(path) - 1;
+ if (name_length > max_name_length) {
+ ERR(GRN_INVALID_ARGUMENT,
+ "plugin name is too long: %d (max: %d) <%s%s>",
+ name_length, max_name_length,
+ path, name);
+ return ctx->rc;
+ }
strcat(path, name);
normalize_path_separator(path);
return grn_plugin_register_by_path(ctx, path);
Modified: test/unit/core/test-plugin.c (+49 -0)
===================================================================
--- test/unit/core/test-plugin.c 2011-02-18 05:36:24 +0000 (6794dbd)
+++ test/unit/core/test-plugin.c 2011-02-18 05:38:37 +0000 (10cd096)
@@ -24,6 +24,7 @@
#include <str.h>
void test_register_function(void);
+void test_register_with_too_long_name(void);
static gchar *tmp_directory, *plugins_dir, *plugins_dir_env;
@@ -122,3 +123,51 @@ test_register_function(void)
"--query groonga "
"--scorer '_score=str_len(_key)'"));
}
+
+void
+test_register_too_long_name(void)
+{
+ GString *long_name;
+ const gchar *full_path;
+ const gchar *error_message_without_path;
+ const gchar *error_message_without_name;
+ gint i, max_name_length;
+
+ long_name = gcut_take_new_string(NULL);
+ max_name_length = (PATH_MAX - strlen(plugins_dir) - 1) - 1;
+ for (i = 0; i < max_name_length; i++) {
+ g_string_append_c(long_name, 'x');
+ }
+ error_message_without_path =
+ "cannot open shared object file: No such file or directory: "
+ "<xxxxx.so> and <";
+ grn_test_assert_send_command_error(
+ context,
+ GRN_NO_SUCH_FILE_OR_DIRECTORY,
+ cut_take_printf("%s%.*s",
+ error_message_without_path,
+ (int)(GRN_CTX_MSGSIZE -
+ strlen(error_message_without_path) -
+ 1),
+ plugins_dir),
+ cut_take_printf("register %s", long_name->str));
+
+ g_string_append_c(long_name, 'x');
+ full_path = cut_take_string(g_build_filename(plugins_dir,
+ long_name->str,
+ NULL));
+ error_message_without_name =
+ cut_take_printf("plugin name is too long: %d (max: %d) <",
+ (int)(long_name->len),
+ max_name_length);
+ grn_test_assert_send_command_error(
+ context,
+ GRN_INVALID_ARGUMENT,
+ cut_take_printf("%s%.*s",
+ error_message_without_name,
+ (int)(GRN_CTX_MSGSIZE -
+ strlen(error_message_without_name) -
+ 1),
+ full_path),
+ cut_take_printf("register %s", long_name->str));
+}