mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge branch 'mrb_debug_strdup-and-strndup' of https://github.com/cremno/mruby into cremno-mrb_debug_strdup-and-strndup
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <mruby/variable.h>
|
||||
#include "mrdberror.h"
|
||||
#include "apibreak.h"
|
||||
#include "apistring.h"
|
||||
|
||||
#define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024)
|
||||
#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
|
||||
}
|
||||
|
||||
len = strlen(file) + 1;
|
||||
set_file = (char*)mrb_malloc(mrb, len);
|
||||
set_file = mrb_debug_strdup(mrb, file);
|
||||
|
||||
index = dbg->bpnum;
|
||||
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
|
||||
dbg->bp[index].point.linepoint.lineno = lineno;
|
||||
dbg->bpnum++;
|
||||
|
||||
strncpy(set_file, file, len);
|
||||
|
||||
dbg->bp[index].point.linepoint.file = set_file;
|
||||
|
||||
return dbg->bp[index].bpno;
|
||||
@@ -220,7 +219,6 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c
|
||||
int32_t index;
|
||||
char* set_class;
|
||||
char* set_method;
|
||||
size_t len;
|
||||
|
||||
if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
|
||||
return MRB_DEBUG_INVALID_ARGUMENT;
|
||||
@@ -235,18 +233,16 @@ mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *c
|
||||
}
|
||||
|
||||
if (class_name != NULL) {
|
||||
len = strlen(class_name) + 1;
|
||||
set_class = (char*)mrb_malloc(mrb, len);
|
||||
strncpy(set_class, class_name, len);
|
||||
set_class = mrb_debug_strdup(mrb, class_name);
|
||||
}
|
||||
else {
|
||||
set_class = NULL;
|
||||
}
|
||||
|
||||
len = strlen(method_name) + 1;
|
||||
set_method = (char*)mrb_malloc(mrb, len);
|
||||
|
||||
strncpy(set_method, method_name, len);
|
||||
set_method = mrb_debug_strdup(mrb, method_name);
|
||||
if (set_method == NULL) {
|
||||
mrb_free(mrb, set_class);
|
||||
}
|
||||
|
||||
index = dbg->bpnum;
|
||||
dbg->bp[index].bpno = dbg->next_bpno;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "mrdb.h"
|
||||
#include "mrdberror.h"
|
||||
#include "apilist.h"
|
||||
#include "apistring.h"
|
||||
#include <mruby/compile.h>
|
||||
#include <mruby/irep.h>
|
||||
#include <mruby/debug.h>
|
||||
@@ -74,11 +75,7 @@ dirname(mrb_state *mrb, const char *path)
|
||||
p = strrchr(path, '/');
|
||||
len = p != NULL ? (size_t)(p - path) : strlen(path);
|
||||
|
||||
dir = (char*)mrb_malloc(mrb, len + 1);
|
||||
strncpy(dir, path, len);
|
||||
dir[len] = '\0';
|
||||
|
||||
return dir;
|
||||
return mrb_debug_strndup(mrb, path, len);
|
||||
}
|
||||
|
||||
static source_file*
|
||||
@@ -97,8 +94,11 @@ source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename)
|
||||
}
|
||||
|
||||
file->lineno = 1;
|
||||
file->path = (char*)mrb_malloc(mrb, strlen(filename) + 1);
|
||||
strcpy(file->path, filename);
|
||||
file->path = mrb_debug_strdup(mrb, filename);
|
||||
if (file->path == NULL) {
|
||||
source_file_free(mrb, file);
|
||||
return NULL;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
** 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;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* 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 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "apilist.h"
|
||||
#include "apistring.h"
|
||||
#include <mruby/compile.h>
|
||||
|
||||
typedef struct help_msg {
|
||||
@@ -232,10 +233,7 @@ parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st)
|
||||
len = strlen(*sp);
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
st->filename = (char*)mrb_malloc(mrb, len + 1);
|
||||
strncpy(st->filename, *sp, len);
|
||||
st->filename[len] = '\0';
|
||||
if (len > 0 && (st->filename = mrb_debug_strndup(mrb, *sp, len)) != NULL) {
|
||||
*sp += len;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user