Merge branch 'cremno-mrb_debug_strdup-and-strndup'

parents 7fa0a704 7c362ee7
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <mruby/variable.h> #include <mruby/variable.h>
#include "mrdberror.h" #include "mrdberror.h"
#include "apibreak.h" #include "apibreak.h"
#include "apistring.h"
#define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024) #define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024)
#define MRB_DEBUG_BP_FILE_OK (0x0001) #define MRB_DEBUG_BP_FILE_OK (0x0001)
...@@ -197,7 +198,7 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil ...@@ -197,7 +198,7 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil
} }
len = strlen(file) + 1; len = strlen(file) + 1;
set_file = (char*)mrb_malloc(mrb, len); set_file = mrb_debug_strdup(mrb, file);
index = dbg->bpnum; index = dbg->bpnum;
dbg->bp[index].bpno = dbg->next_bpno; dbg->bp[index].bpno = dbg->next_bpno;
...@@ -207,8 +208,6 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil ...@@ -207,8 +208,6 @@ mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *fil
dbg->bp[index].point.linepoint.lineno = lineno; dbg->bp[index].point.linepoint.lineno = lineno;
dbg->bpnum++; dbg->bpnum++;
strncpy(set_file, file, len);
dbg->bp[index].point.linepoint.file = set_file; dbg->bp[index].point.linepoint.file = set_file;
return dbg->bp[index].bpno; return dbg->bp[index].bpno;
...@@ -220,7 +219,6 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c ...@@ -220,7 +219,6 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c
int32_t index; int32_t index;
char* set_class; char* set_class;
char* set_method; char* set_method;
size_t len;
if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) { if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
return MRB_DEBUG_INVALID_ARGUMENT; return MRB_DEBUG_INVALID_ARGUMENT;
...@@ -235,18 +233,16 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c ...@@ -235,18 +233,16 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c
} }
if (class_name != NULL) { if (class_name != NULL) {
len = strlen(class_name) + 1; set_class = mrb_debug_strdup(mrb, class_name);
set_class = (char*)mrb_malloc(mrb, len);
strncpy(set_class, class_name, len);
} }
else { else {
set_class = NULL; set_class = NULL;
} }
len = strlen(method_name) + 1; set_method = mrb_debug_strdup(mrb, method_name);
set_method = (char*)mrb_malloc(mrb, len); if (set_method == NULL) {
mrb_free(mrb, set_class);
strncpy(set_method, method_name, len); }
index = dbg->bpnum; index = dbg->bpnum;
dbg->bp[index].bpno = dbg->next_bpno; dbg->bp[index].bpno = dbg->next_bpno;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "mrdb.h" #include "mrdb.h"
#include "mrdberror.h" #include "mrdberror.h"
#include "apilist.h" #include "apilist.h"
#include "apistring.h"
#include <mruby/compile.h> #include <mruby/compile.h>
#include <mruby/irep.h> #include <mruby/irep.h>
#include <mruby/debug.h> #include <mruby/debug.h>
...@@ -74,11 +75,7 @@ dirname(mrb_state *mrb, const char *path) ...@@ -74,11 +75,7 @@ dirname(mrb_state *mrb, const char *path)
p = strrchr(path, '/'); p = strrchr(path, '/');
len = p != NULL ? (size_t)(p - path) : strlen(path); len = p != NULL ? (size_t)(p - path) : strlen(path);
dir = (char*)mrb_malloc(mrb, len + 1); return mrb_debug_strndup(mrb, path, len);
strncpy(dir, path, len);
dir[len] = '\0';
return dir;
} }
static source_file* static source_file*
...@@ -97,8 +94,11 @@ source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename) ...@@ -97,8 +94,11 @@ source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename)
} }
file->lineno = 1; file->lineno = 1;
file->path = (char*)mrb_malloc(mrb, strlen(filename) + 1); file->path = mrb_debug_strdup(mrb, filename);
strcpy(file->path, filename); if (file->path == NULL) {
source_file_free(mrb, file);
return NULL;
}
return file; return file;
} }
......
/*
** apistring.c
**
*/
#include <string.h>
#include "apistring.h"
static size_t
mrb_debug_strnlen(const char *s, size_t maxlen)
{
const char *p = memchr(s, '\0', maxlen);
return p != NULL ? (size_t)(p - s) : maxlen;
}
char*
mrb_debug_strndup(mrb_state *mrb, const char *s, size_t size)
{
size_t l = mrb_debug_strnlen(s, size);
char *d = mrb_malloc_simple(mrb, l + 1);
if (d != NULL) {
memcpy(d, s, l);
d[l] = '\0';
}
return d;
}
char*
mrb_debug_strdup(mrb_state *mrb, const char *s)
{
size_t z = strlen(s) + 1;
char *d = mrb_malloc_simple(mrb, z);
return d != NULL ? memcpy(d, s, z) : NULL;
}
/*
* apistring.h
*/
#ifndef APISTRING_H_
#define APISTRING_H_
#include "mruby.h"
/* both functions return a null pointer on failure */
char *mrb_debug_strndup(mrb_state *mrb, const char *s, size_t size);
char *mrb_debug_strdup(mrb_state *mrb, const char *s);
#endif /* APISTRING_H_ */
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string.h> #include <string.h>
#include "apilist.h" #include "apilist.h"
#include "apistring.h"
#include <mruby/compile.h> #include <mruby/compile.h>
typedef struct help_msg { typedef struct help_msg {
...@@ -232,10 +233,7 @@ parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st) ...@@ -232,10 +233,7 @@ parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st)
len = strlen(*sp); len = strlen(*sp);
} }
if (len > 0) { if (len > 0 && (st->filename = mrb_debug_strndup(mrb, *sp, len)) != NULL) {
st->filename = (char*)mrb_malloc(mrb, len + 1);
strncpy(st->filename, *sp, len);
st->filename[len] = '\0';
*sp += len; *sp += len;
return TRUE; return TRUE;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment