[Groonga-commit] groonga/groonga at aeb0bdc [master] windows: use strcpy_s() on Windows

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Apr 18 17:04:41 JST 2015


Kouhei Sutou	2015-04-18 17:04:41 +0900 (Sat, 18 Apr 2015)

  New Revision: aeb0bdcf604fd7d4f1013881c51ab6ee0e08af46
  https://github.com/groonga/groonga/commit/aeb0bdcf604fd7d4f1013881c51ab6ee0e08af46

  Message:
    windows: use strcpy_s() on Windows

  Modified files:
    include/groonga/portability.h
    lib/ctx.c
    lib/geo.c
    lib/ii.c
    lib/mrb.c
    lib/mrb/mrb_bulk.c
    lib/plugin.c
    lib/proc.c
    src/groonga.c
    src/groonga_benchmark.c

  Modified: include/groonga/portability.h (+8 -0)
===================================================================
--- include/groonga/portability.h    2015-04-18 16:34:46 +0900 (c8dc92d)
+++ include/groonga/portability.h    2015-04-18 17:04:41 +0900 (fe3b19b)
@@ -94,6 +94,14 @@
 #endif /* WIN32 */
 
 #ifdef WIN32
+# define grn_strcpy(dest, dest_size, src)       \
+  strcpy_s((dest), (dest_size), (src))
+#else /* WIN32 */
+# define grn_strcpy(dest, dest_size, src)       \
+  strcpy((dest), (src), (src))
+#endif /* WIN32 */
+
+#ifdef WIN32
 # define grn_strncpy(dest, dest_size, src, n)   \
   strncpy_s((dest), (dest_size), (src), (n))
 #else /* WIN32 */

  Modified: lib/ctx.c (+1 -1)
===================================================================
--- lib/ctx.c    2015-04-18 16:34:46 +0900 (4ed5dae)
+++ lib/ctx.c    2015-04-18 17:04:41 +0900 (43be6a4)
@@ -606,7 +606,7 @@ grn_ctx_impl_set_current_error_message(grn_ctx *ctx)
   }
 
   grn_ctx_impl_clear_n_same_error_mssagges(ctx);
-  strcpy(ctx->impl->previous_errbuf, ctx->errbuf);
+  grn_strcpy(ctx->impl->previous_errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
 }
 
 static grn_rc

  Modified: lib/geo.c (+2 -2)
===================================================================
--- lib/geo.c    2015-04-18 16:34:46 +0900 (20d89b9)
+++ lib/geo.c    2015-04-18 17:04:41 +0900 (d2eb049)
@@ -862,7 +862,7 @@ grn_geo_select_in_circle(grn_ctx *ctx, grn_obj *index,
       name_size = grn_obj_name(ctx, domain_object, name, GRN_TABLE_MAX_KEY_SIZE);
       grn_obj_unlink(ctx, domain_object);
     } else {
-      strcpy(name, "(null)");
+      grn_strcpy(name, GRN_TABLE_MAX_KEY_SIZE, "(null)");
       name_size = strlen(name);
     }
     ERR(GRN_INVALID_ARGUMENT,
@@ -1034,7 +1034,7 @@ in_rectangle_data_fill(grn_ctx *ctx, grn_obj *index,
       name_size = grn_obj_name(ctx, domain_object, name, GRN_TABLE_MAX_KEY_SIZE);
       grn_obj_unlink(ctx, domain_object);
     } else {
-      strcpy(name, "(null)");
+      grn_strcpy(name, GRN_TABLE_MAX_KEY_SIZE, "(null)");
       name_size = strlen(name);
     }
     ERR(GRN_INVALID_ARGUMENT,

  Modified: lib/ii.c (+2 -2)
===================================================================
--- lib/ii.c    2015-04-18 16:34:46 +0900 (2f4d7ec)
+++ lib/ii.c    2015-04-18 17:04:41 +0900 (50c41fb)
@@ -3483,7 +3483,7 @@ _grn_ii_create(grn_ctx *ctx, grn_ii *ii, const char *path, grn_obj *lexicon, uin
                       S_SEGMENT, MAX_PSEG, grn_io_auto, GRN_IO_EXPIRE_SEGMENT);
   if (!seg) { return NULL; }
   if (path) {
-    strcpy(path2, path);
+    grn_strcpy(path2, PATH_MAX, path);
     strcat(path2, ".c");
     chunk = grn_io_create(ctx, path2, 0, S_CHUNK, GRN_II_MAX_CHUNK, grn_io_auto,
                           GRN_IO_EXPIRE_SEGMENT);
@@ -3602,7 +3602,7 @@ grn_ii_open(grn_ctx *ctx, const char *path, grn_obj *lexicon)
     return NULL;
   }
   if (strlen(path) + 6 >= PATH_MAX) { return NULL; }
-  strcpy(path2, path);
+  grn_strcpy(path2, PATH_MAX, path);
   strcat(path2, ".c");
   seg = grn_io_open(ctx, path, grn_io_auto);
   if (!seg) { return NULL; }

  Modified: lib/mrb.c (+9 -8)
===================================================================
--- lib/mrb.c    2015-04-18 16:34:46 +0900 (3519e48)
+++ lib/mrb.c    2015-04-18 17:04:41 +0900 (24dc133)
@@ -49,7 +49,7 @@ grn_mrb_get_default_system_ruby_scripts_dir(void)
 
     base_dir = grn_win32_base_dir();
     base_dir_length = strlen(base_dir);
-    strcpy(win32_ruby_scripts_dir_buffer, base_dir);
+    grn_strcpy(win32_ruby_scripts_dir_buffer, PATH_MAX, base_dir);
     strcat(win32_ruby_scripts_dir_buffer, "/");
     strcat(win32_ruby_scripts_dir_buffer, relative_path);
     win32_ruby_scripts_dir = win32_ruby_scripts_dir_buffer;
@@ -95,7 +95,8 @@ grn_mrb_is_absolute_path(const char *path)
 }
 
 static grn_bool
-grn_mrb_expand_script_path(grn_ctx *ctx, const char *path, char *expanded_path)
+grn_mrb_expand_script_path(grn_ctx *ctx, const char *path,
+                           char *expanded_path, size_t expanded_path_size)
 {
   const char *ruby_scripts_dir;
   char dir_last_char;
@@ -104,11 +105,11 @@ grn_mrb_expand_script_path(grn_ctx *ctx, const char *path, char *expanded_path)
   if (grn_mrb_is_absolute_path(path)) {
     expanded_path[0] = '\0';
   } else if (path[0] == '.' && path[1] == '/') {
-    strcpy(expanded_path, ctx->impl->mrb.base_directory);
+    grn_strcpy(expanded_path, expanded_path_size, ctx->impl->mrb.base_directory);
     strcat(expanded_path, "/");
   } else {
     ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
-    strcpy(expanded_path, ruby_scripts_dir);
+    grn_strcpy(expanded_path, expanded_path_size, ruby_scripts_dir);
 
     dir_last_char = ruby_scripts_dir[strlen(expanded_path) - 1];
     if (dir_last_char != '/') {
@@ -145,7 +146,7 @@ grn_mrb_load(grn_ctx *ctx, const char *path)
     return mrb_nil_value();
   }
 
-  if (!grn_mrb_expand_script_path(ctx, path, expanded_path)) {
+  if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) {
     return mrb_nil_value();
   }
 
@@ -166,8 +167,8 @@ grn_mrb_load(grn_ctx *ctx, const char *path)
     char current_base_directory[PATH_MAX];
     char *last_directory;
 
-    strcpy(current_base_directory, data->base_directory);
-    strcpy(data->base_directory, expanded_path);
+    grn_strcpy(current_base_directory, PATH_MAX, data->base_directory);
+    grn_strcpy(data->base_directory, PATH_MAX, expanded_path);
     last_directory = strrchr(data->base_directory, '/');
     if (last_directory) {
       last_directory[0] = '\0';
@@ -187,7 +188,7 @@ grn_mrb_load(grn_ctx *ctx, const char *path)
     }
     mrb_parser_free(parser);
 
-    strcpy(data->base_directory, current_base_directory);
+    grn_strcpy(data->base_directory, PATH_MAX, current_base_directory);
   }
 
   return result;

  Modified: lib/mrb/mrb_bulk.c (+1 -1)
===================================================================
--- lib/mrb/mrb_bulk.c    2015-04-18 16:34:46 +0900 (75682a0)
+++ lib/mrb/mrb_bulk.c    2015-04-18 17:04:41 +0900 (8c97b97)
@@ -148,7 +148,7 @@ grn_mrb_value_from_bulk(mrb_state *mrb, grn_obj *bulk)
                                         domain_name, GRN_TABLE_MAX_KEY_SIZE);
         grn_obj_unlink(ctx, domain);
       } else {
-        strcpy(domain_name, "unknown");
+        grn_strcpy(domain_name, GRN_TABLE_MAX_KEY_SIZE, "unknown");
         domain_name_size = strlen(domain_name);
       }
       snprintf(message, MESSAGE_SIZE,

  Modified: lib/plugin.c (+4 -4)
===================================================================
--- lib/plugin.c    2015-04-18 16:34:46 +0900 (cc8c852)
+++ lib/plugin.c    2015-04-18 17:04:41 +0900 (0476d6d)
@@ -494,7 +494,7 @@ grn_plugin_get_default_system_plugins_dir(void)
 
     base_dir = grn_win32_base_dir();
     base_dir_length = strlen(base_dir);
-    strcpy(win32_plugins_dir_buffer, base_dir);
+    grn_strcpy(win32_plugins_dir_buffer, PATH_MAX, base_dir);
     strcat(win32_plugins_dir_buffer, "/");
     strcat(win32_plugins_dir_buffer, relative_path);
     win32_plugins_dir = win32_plugins_dir_buffer;
@@ -562,7 +562,7 @@ grn_plugin_find_path_mrb(grn_ctx *ctx, const char *path, size_t path_len)
     return NULL;
   }
 
-  strcpy(mrb_path, path);
+  grn_strcpy(mrb_path, PATH_MAX, path);
   strcat(mrb_path, mrb_suffix);
   return grn_plugin_find_path_raw(ctx, mrb_path);
 }
@@ -590,7 +590,7 @@ grn_plugin_find_path_so(grn_ctx *ctx, const char *path, size_t path_len)
     return NULL;
   }
 
-  strcpy(so_path, path);
+  grn_strcpy(so_path, PATH_MAX, path);
   strcat(so_path, so_suffix);
   return grn_plugin_find_path_raw(ctx, so_path);
 }
@@ -645,7 +645,7 @@ grn_plugin_find_path(grn_ctx *ctx, const char *name)
     path[0] = '\0';
   } else {
     plugins_dir = grn_plugin_get_system_plugins_dir();
-    strcpy(path, plugins_dir);
+    grn_strcpy(path, PATH_MAX, plugins_dir);
 
     dir_last_char = plugins_dir[strlen(path) - 1];
     if (dir_last_char != '/') {

  Modified: lib/proc.c (+1 -1)
===================================================================
--- lib/proc.c    2015-04-18 16:34:46 +0900 (7d43d66)
+++ lib/proc.c    2015-04-18 17:04:41 +0900 (5b4f428)
@@ -2610,7 +2610,7 @@ proc_delete(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
                    GRN_EXPR_SYNTAX_SCRIPT);
     if (ctx->rc) {
       char original_error_message[GRN_CTX_MSGSIZE];
-      strcpy(original_error_message, ctx->errbuf);
+      grn_strcpy(original_error_message, GRN_CTX_MSGSIZE, ctx->errbuf);
       rc = ctx->rc;
       ERR(rc,
           "[table][record][delete] failed to parse filter: "

  Modified: src/groonga.c (+8 -8)
===================================================================
--- src/groonga.c    2015-04-18 16:34:46 +0900 (b94a76b)
+++ src/groonga.c    2015-04-18 17:04:41 +0900 (667e699)
@@ -142,7 +142,7 @@ line_editor_init(int argc __attribute__((unused)), char *argv[])
   setlocale(LC_ALL, "");
 
   if (strlen(HOME_PATH) + strlen(HISTORY_PATH) < PATH_MAX) {
-    strcpy(line_editor_history_path, HOME_PATH);
+    grn_strcpy(line_editor_history_path, PATH_MAX, HOME_PATH);
     strcat(line_editor_history_path, HISTORY_PATH);
   } else {
     line_editor_history_path[0] = '\0';
@@ -2228,7 +2228,7 @@ config_file_register(const char *path, const grn_str_getopt_opt *opts,
   char *args[4];
 
   name_buf[0] = name_buf[1] = '-';
-  strcpy(name_buf + 2, name);
+  grn_strcpy(name_buf + 2, CONFIG_FILE_MAX_NAME_LENGTH + 1, name);
 
   if (value) {
     const size_t entry_size = sizeof(config_file_entry) + value_length + 1;
@@ -2238,7 +2238,7 @@ config_file_register(const char *path, const grn_str_getopt_opt *opts,
               (unsigned int)entry_size);
       return CONFIG_FILE_MALLOC_ERROR;
     }
-    strcpy((char *)(entry + 1), value);
+    grn_strcpy((char *)(entry + 1), value_length + 1, value);
     entry->next = config_file_entry_head;
     if (!config_file_entry_head) {
       if (atexit(config_file_clear)) {
@@ -2438,7 +2438,7 @@ init_default_settings(void)
     if (document_root_length >= PATH_MAX) {
       fprintf(stderr, "can't use default root: too long path\n");
     } else {
-      strcpy(win32_default_document_root, grn_win32_base_dir());
+      grn_strcpy(win32_default_document_root, PATH_MAX, grn_win32_base_dir());
       strcat(win32_default_document_root, "/");
       strcat(win32_default_document_root, GRN_DEFAULT_RELATIVE_DOCUMENT_ROOT);
       default_document_root = win32_default_document_root;
@@ -2998,9 +2998,9 @@ main(int argc, char **argv)
               bind_address_arg, (unsigned int)bind_address_length, HOST_NAME_MAX);
       return EXIT_FAILURE;
     }
-    strcpy(bind_address, bind_address_arg);
+    grn_strcpy(bind_address, HOST_NAME_MAX + 1, bind_address_arg);
   } else {
-    strcpy(bind_address, default_bind_address);
+    grn_strcpy(bind_address, HOST_NAME_MAX + 1, default_bind_address);
   }
 
   if (hostname_arg) {
@@ -3011,9 +3011,9 @@ main(int argc, char **argv)
               hostname_arg, (unsigned int)hostname_length, HOST_NAME_MAX);
       return EXIT_FAILURE;
     }
-    strcpy(hostname, hostname_arg);
+    grn_strcpy(hostname, HOST_NAME_MAX + 1, hostname_arg);
   } else {
-    strcpy(hostname, default_hostname);
+    grn_strcpy(hostname, HOST_NAME_MAX + 1, default_hostname);
   }
 
   if (document_root_arg) {

  Modified: src/groonga_benchmark.c (+24 -21)
===================================================================
--- src/groonga_benchmark.c    2015-04-18 16:34:46 +0900 (e7e426e)
+++ src/groonga_benchmark.c    2015-04-18 17:04:41 +0900 (732e212)
@@ -1267,7 +1267,7 @@ get_sysinfo(const char *path, char *result, int olen)
     sprintf(tmpbuf, "date\t%s\n", grntest_date);
     strcat(result, tmpbuf);
   } else {
-    strcpy(result, "{");
+    grn_strcpy(result, olen, "{");
     sprintf(tmpbuf, "\"script\": \"%s.scr\",\n", grntest_scriptname);
     strcat(result, tmpbuf);
     sprintf(tmpbuf, "  \"user\": \"%s\",\n", grntest_username);
@@ -1380,7 +1380,8 @@ get_sysinfo(const char *path, char *result, int olen)
   int minfo = 0;
   int unevictable = 0;
   int mlocked = 0;
-  char cpustring[256];
+#define CPU_STRING_SIZE 256
+  char cpu_string[CPU_STRING_SIZE];
   struct utsname ubuf;
   struct statvfs vfsbuf;
 
@@ -1393,7 +1394,7 @@ get_sysinfo(const char *path, char *result, int olen)
     sprintf(tmpbuf, "date\t%s\n", grntest_date);
     strcat(result, tmpbuf);
   } else {
-    strcpy(result, "{");
+    grn_strcpy(result, olen, "{");
     sprintf(tmpbuf, "\"script\": \"%s.scr\",\n", grntest_scriptname);
     strcat(result, tmpbuf);
     sprintf(tmpbuf, "  \"user\": \"%s\",\n", grntest_username);
@@ -1410,16 +1411,18 @@ get_sysinfo(const char *path, char *result, int olen)
   while (fgets(tmpbuf, 256, fp) != NULL) {
     tmpbuf[strlen(tmpbuf)-1] = '\0';
     if (!strncmp(tmpbuf, "model name\t: ", 13)) {
-      strcpy(cpustring, &tmpbuf[13]);
+      grn_strcpy(cpu_string, CPU_STRING_SIZE, &tmpbuf[13]);
     }
   }
   fclose(fp);
+#undef CPU_STRING_SIZE
+
   cpunum = sysconf(_SC_NPROCESSORS_CONF);
 
   if (grntest_outtype == OUT_TSV) {
-    sprintf(tmpbuf, "%s\n", cpustring);
+    sprintf(tmpbuf, "%s\n", cpu_string);
   } else {
-    sprintf(tmpbuf, "  \"CPU\": \"%s\",\n", cpustring);
+    sprintf(tmpbuf, "  \"CPU\": \"%s\",\n", cpu_string);
   }
   strcat(result, tmpbuf);
 
@@ -1551,7 +1554,7 @@ start_server(const char *dbpath, int r)
     exit(1);
   }
 
-  strcpy(tmpbuf, groonga_path);
+  grn_strcpy(tmpbuf, BUF_LEN, groonga_path);
   strcat(tmpbuf, " -s --protocol ");
   strcat(tmpbuf, groonga_protocol);
   strcat(tmpbuf, " -p ");
@@ -1767,7 +1770,7 @@ parse_line(char *buf, int start, int end, int num)
         return 15;
       }
     }
-    strcpy(grntest_job[num].logfile, tmpbuf);
+    grn_strcpy(grntest_job[num].logfile, BUF_LEN, tmpbuf);
     return 0;
   } else {
     grntest_job[num].concurrency = grntest_atoi(tmpbuf, tmpbuf + j, NULL);
@@ -2427,10 +2430,10 @@ ftp_sub(const char *user, const char *passwd, const char *host,
 
 #ifdef WIN32
   _splitpath(filename, NULL, NULL, fname, ext);
-  strcpy(base, fname);
+  grn_strcpy(base, BUF_LEN, fname);
   strcat(base, ext);
 #else
-  strcpy(buf, filename);
+  grn_strcpy(buf, BUF_LEN, filename);
   base = basename(buf);
 #endif /* WIN32 */
 
@@ -2471,7 +2474,7 @@ ftp_sub(const char *user, const char *passwd, const char *host,
   }
   if (!strncmp(buf, "213", 3)) {
     retval[BUF_LEN-2] = '\0';
-    strcpy(retval, get_ftp_date(buf));
+    grn_strcpy(retval, BUF_LEN - 2, get_ftp_date(buf));
     if (retval[BUF_LEN-2] != '\0' ) {
       fprintf(stderr, "buffer over run in ftp\n");
       exit(1);
@@ -2534,7 +2537,7 @@ static int
 get_username(char *name, int maxlen)
 {
   char *env=NULL;
-  strcpy(name, "nobody");
+  grn_strcpy(name, maxlen, "nobody");
 #ifdef WIN32
   env = getenv("USERNAME");
 #else
@@ -2545,7 +2548,7 @@ get_username(char *name, int maxlen)
     exit(1);
   }
   if (env) {
-    strcpy(name, env);
+    grn_strcpy(name, maxlen, env);
   }
   return 0;
 }
@@ -2577,7 +2580,7 @@ get_date(char *date, time_t *sec)
 }
 
 static int
-get_scriptname(const char *path, char *name, const char *suffix)
+get_scriptname(const char *path, char *name, size_t name_len, const char *suffix)
 {
   int slen = strlen(suffix);
   int len = strlen(path);
@@ -2591,7 +2594,7 @@ get_scriptname(const char *path, char *name, const char *suffix)
     exit(1);
   }
 
-  strcpy(name, path);
+  grn_strcpy(name, name_len, path);
   if (strncmp(&name[len-slen], suffix, slen)) {
     name[0] = '\0';
     return 0;
@@ -2874,7 +2877,7 @@ check_script(grn_ctx *ctx, const char *script_file_path)
   while (grn_text_fgets(ctx, &line, script_file) == GRN_SUCCESS) {
     GRN_TEXT_VALUE(&line)[GRN_TEXT_LEN(&line) - 1] = '\0';
     get_token(GRN_TEXT_VALUE(&line), token, BUF_LEN, &next);
-    strcpy(prev, token);
+    grn_strcpy(prev, BUF_LEN, token);
 
     while (next) {
       get_token(next, token, BUF_LEN, &next);
@@ -2882,10 +2885,10 @@ check_script(grn_ctx *ctx, const char *script_file_path)
         grntest_serverport = grn_atoi(token, token + strlen(token), NULL);
       }
       if (!strncmp(prev, "SET_HOST", 8)) {
-        strcpy(grntest_serverhost, token);
+        grn_strcpy(grntest_serverhost, BUF_LEN, token);
         grntest_remote_mode = 1;
       }
-      strcpy(prev, token);
+      grn_strcpy(prev, BUF_LEN, token);
     }
   }
   grn_obj_unlink(ctx, &line);
@@ -3045,10 +3048,10 @@ main(int argc, char **argv)
     usage();
   }
 
-  strcpy(grntest_serverhost, DEFAULT_DEST);
+  grn_strcpy(grntest_serverhost, BUF_LEN, DEFAULT_DEST);
   if (hoststr) {
     grntest_remote_mode = 1;
-    strcpy(grntest_serverhost, hoststr);
+    grn_strcpy(grntest_serverhost, BUF_LEN, hoststr);
   }
   grntest_serverport = DEFAULT_PORT;
   if (portstr) {
@@ -3086,7 +3089,7 @@ main(int argc, char **argv)
     }
   }
 
-  get_scriptname(scrname, grntest_scriptname, ".scr");
+  get_scriptname(scrname, grntest_scriptname, BUF_LEN, ".scr");
   get_username(grntest_username, 256);
 
   GRN_TIME_INIT(&grntest_starttime, 0);
-------------- next part --------------
HTML����������������������������...
Download 



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