remove invocation of strlen() on buffer of strings; with refactoring

parent ee57789c
...@@ -47,6 +47,7 @@ struct RString { ...@@ -47,6 +47,7 @@ struct RString {
RSTRING_EMBED_LEN_MAX :\ RSTRING_EMBED_LEN_MAX :\
RSTRING(s)->as.heap.aux.capa) RSTRING(s)->as.heap.aux.capa)
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s)) #define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
mrb_int mrb_str_strlen(mrb_state*, struct RString*);
#define MRB_STR_SHARED 1 #define MRB_STR_SHARED 1
#define MRB_STR_NOFREE 2 #define MRB_STR_NOFREE 2
......
...@@ -534,25 +534,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -534,25 +534,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
case 'z': case 'z':
{ {
mrb_value ss; mrb_value ss;
struct RString *s;
char **ps; char **ps;
mrb_int len;
ps = va_arg(ap, char**); ps = va_arg(ap, char**);
if (i < argc) { if (i < argc) {
size_t size_t_len;
ss = to_str(mrb, *sp++); ss = to_str(mrb, *sp++);
s = mrb_str_ptr(ss); *ps = mrb_string_value_cstr(mrb, &ss);
size_t_len = strlen(RSTRING_PTR(ss));
mrb_assert(size_t_len <= MRB_INT_MAX);
len = (mrb_int)size_t_len;
if (len < RSTRING_LEN(ss)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
}
else if (len > RSTRING_LEN(ss)) {
mrb_str_modify(mrb, s);
}
*ps = RSTRING_PTR(ss);
i++; i++;
} }
} }
......
...@@ -54,6 +54,21 @@ typedef struct mrb_shared_string { ...@@ -54,6 +54,21 @@ typedef struct mrb_shared_string {
static mrb_value str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2); static mrb_value str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2);
static mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len); static mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
mrb_int
mrb_str_strlen(mrb_state *mrb, struct RString *s)
{
mrb_int i, max = STR_LEN(s);
char *p = STR_PTR(s);
if (!p) return 0;
for (i=0; i<max; i++) {
if (p[i] == '\0') {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
}
}
return max;
}
#define RESIZE_CAPA(s,capacity) do {\ #define RESIZE_CAPA(s,capacity) do {\
if (STR_EMBED_P(s)) {\ if (STR_EMBED_P(s)) {\
if (RSTRING_EMBED_LEN_MAX < (capacity)) {\ if (RSTRING_EMBED_LEN_MAX < (capacity)) {\
...@@ -2066,16 +2081,14 @@ char * ...@@ -2066,16 +2081,14 @@ char *
mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr) mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr)
{ {
struct RString *ps = mrb_str_ptr(*ptr); struct RString *ps = mrb_str_ptr(*ptr);
char *s = STR_PTR(ps); mrb_int len = mrb_str_strlen(mrb, ps);
mrb_int len; char *p = STR_PTR(ps);
len = STR_LEN(ps); if (!p || p[len] != '\0') {
mrb_assert(len >= 0); mrb_str_modify(mrb, ps);
mrb_assert((size_t)len <= SIZE_MAX); return STR_PTR(ps);
if (!s || (size_t)len != strlen(s)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
} }
return s; return p;
} }
mrb_value mrb_value
......
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