diff options
| author | Cremno <[email protected]> | 2013-08-07 19:58:06 +0200 |
|---|---|---|
| committer | Cremno <[email protected]> | 2013-08-07 19:58:06 +0200 |
| commit | ba027d7806e00be6b34b2539f42b9f31e218ab91 (patch) | |
| tree | 2c0e40a8ba70007b4200a056d40ff67b1bdc83dc | |
| parent | 1f5c91d68dfca9a7000f2b194efceed9b96ffdf6 (diff) | |
| download | mruby-ba027d7806e00be6b34b2539f42b9f31e218ab91.tar.gz mruby-ba027d7806e00be6b34b2539f42b9f31e218ab91.zip | |
don't use str{cpy,cat} in mruby and mrbc
The length of each string is known. It should be used.
| -rw-r--r-- | mrbgems/mruby-bin-mruby/tools/mruby/mruby.c | 18 | ||||
| -rw-r--r-- | tools/mrbc/mrbc.c | 12 |
2 files changed, 21 insertions, 9 deletions
diff --git a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c index 97b55687f..baeb95993 100644 --- a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +++ b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c @@ -91,16 +91,24 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) item = argv[0]; append_cmdline: if (!args->cmdline) { + size_t buflen; char *buf; - buf = (char *)mrb_malloc(mrb, strlen(item)+1); - strcpy(buf, item); + buflen = strlen(item) + 1; + buf = (char *)mrb_malloc(mrb, buflen); + memcpy(buf, item, buflen); args->cmdline = buf; } else { - args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); - strcat(args->cmdline, "\n"); - strcat(args->cmdline, item); + size_t cmdlinelen; + size_t itemlen; + + cmdlinelen = strlen(args->cmdline); + itemlen = strlen(item); + args->cmdline = + (char *)mrb_realloc(mrb, args->cmdline, cmdlinelen + itemlen + 2); + args->cmdline[cmdlinelen] = '\n'; + memcpy(args->cmdline + cmdlinelen + 1, item, itemlen + 1); } } else { diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 79fcd2bdf..735b22b1d 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -51,15 +51,19 @@ usage(const char *name) static char * get_outfilename(mrb_state *mrb, char *infile, char *ext) { + size_t infilelen; + size_t extlen; char *outfile; char *p; - outfile = (char*)mrb_malloc(mrb, strlen(infile) + strlen(ext) + 1); - strcpy(outfile, infile); + infilelen = strlen(infile); + extlen = strlen(ext); + outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1); + memcpy(outfile, infile, infilelen + 1); if (*ext) { if ((p = strrchr(outfile, '.')) == NULL) - p = &outfile[strlen(outfile)]; - strcpy(p, ext); + p = outfile + infilelen; + memcpy(p, ext, extlen + 1); } return outfile; |
