Skip to content

Commit a581036

Browse files
committed
Pass length constrained mrb string to osc-bridge
When views are created on mruby strings it's possible that the string pointer points to a longer string than is visible at the ruby level. In other words strlen(s.pointer) >= s.length. By copying a string we avoid this edge case. In the UI this created a bug where it was impossible to delete characters from a long string. In ruby you'd start with a string like "foobar"\0(len 6), delete a character off the end via creating a string view which at the ruby level was "fooba", but under the hood it was represented via "fooba"r\0(len 5). This specific behavior only occurs for non-embeddable strings which in mruby's case is about roughly 27 characters (compilation flag dependent).
1 parent bbac205 commit a581036

File tree

1 file changed

+16
-1
lines changed
  • src/mruby-widget-lib/src

1 file changed

+16
-1
lines changed

src/mruby-widget-lib/src/gem.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,20 @@ mrb_remote_param_set_value_ar(mrb_state *mrb, mrb_value self)
11341134
return self;
11351135
}
11361136

1137+
static char *
1138+
mrb_copy_str(mrb_state *mrb, mrb_value value)
1139+
{
1140+
size_t l = mrb_string_value_len(mrb, value);
1141+
const char *s = mrb_string_value_ptr(mrb, value);
1142+
char *out = malloc(l+1);
1143+
1144+
for(int i=0; i<l; ++i)
1145+
out[i] = s[i];
1146+
1147+
out[l] = 0;
1148+
return out;
1149+
}
1150+
11371151
static mrb_value
11381152
mrb_remote_param_set_value_str(mrb_state *mrb, mrb_value self)
11391153
{
@@ -1148,8 +1162,9 @@ mrb_remote_param_set_value_str(mrb_state *mrb, mrb_value self)
11481162
mrb_assert(param->br);
11491163
mrb_assert(param->uri);
11501164

1151-
const char *str = mrb_string_value_ptr(mrb, value);
1165+
const char *str = mrb_copy_str(mrb, value);
11521166
br_set_value_string(param->br, param->uri, str);
1167+
free(str);
11531168
return self;
11541169
}
11551170

0 commit comments

Comments
 (0)