Check for valid indices in substring and string-copy.

This commit is contained in:
Doug Currie 2015-12-27 23:47:29 -05:00 committed by Yuichi Nishiwaki
parent 4a258f8131
commit 51ba443636
1 changed files with 446 additions and 428 deletions

View File

@ -519,17 +519,35 @@ static pic_value
pic_str_string_copy(pic_state *pic)
{
pic_str *str;
int n, start, end;
int n, start, end, len;
n = pic_get_args(pic, "s|ii", &str, &start, &end);
len = pic_str_len(str);
switch (n) {
case 1:
start = 0;
case 2:
end = pic_str_len(str);
end = len;
}
#if 0
#if 0
if (start < 0) start = 0; /* should an error be reported? */
if (end > len) end = len; /* should an error be reported? */
#else
if ((start < 0) || (end < 0) || (start > len) || (end > len))
pic_errorf(pic, "string-copy: invalid index");
#endif
if (end < start) /* surely this is an error!? */
pic_errorf(pic, "string-copy: start index > end index");
#else
/* simplest version to catch all cases as errors */
if ((start < 0) || (end > len) || (end < start))
pic_errorf(pic, "string-copy: invalid index");
#endif
return pic_obj_value(pic_str_sub(pic, str, start, end));
}