support comments in user-input; also add checks for buffer overflow

parent 4e357a49
...@@ -100,9 +100,9 @@ is_code_block_open(struct mrb_parser_state *parser) ...@@ -100,9 +100,9 @@ is_code_block_open(struct mrb_parser_state *parser)
/* all states which need more code */ /* all states which need more code */
case EXPR_BEG: case EXPR_BEG:
/* an expression was just started, */ /* beginning of a statement, */
/* we can't end it like this */ /* that means previous line ended */
code_block_open = TRUE; ;;code_block_open = FALSE;
break; break;
case EXPR_DOT: case EXPR_DOT:
/* a message dot was the last token, */ /* a message dot was the last token, */
...@@ -319,6 +319,10 @@ main(int argc, char **argv) ...@@ -319,6 +319,10 @@ main(int argc, char **argv)
char_index = 0; char_index = 0;
while ((last_char = getchar()) != '\n') { while ((last_char = getchar()) != '\n') {
if (last_char == EOF) break; if (last_char == EOF) break;
if (char_index > sizeof(last_code_line)-2) {
fputs("input string too long\n", stderr);
continue;
}
last_code_line[char_index++] = last_char; last_code_line[char_index++] = last_char;
} }
if (last_char == EOF) { if (last_char == EOF) {
...@@ -326,6 +330,7 @@ main(int argc, char **argv) ...@@ -326,6 +330,7 @@ main(int argc, char **argv)
break; break;
} }
last_code_line[char_index++] = '\n';
last_code_line[char_index] = '\0'; last_code_line[char_index] = '\0';
#else #else
char* line = MIRB_READLINE(code_block_open ? "* " : "> "); char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
...@@ -333,13 +338,21 @@ main(int argc, char **argv) ...@@ -333,13 +338,21 @@ main(int argc, char **argv)
printf("\n"); printf("\n");
break; break;
} }
strncpy(last_code_line, line, sizeof(last_code_line)-1); if (strlen(line) > sizeof(last_code_line)-2) {
fputs("input string too long\n", stderr);
continue;
}
strcpy(last_code_line, line);
strcat(last_code_line, "\n");
MIRB_ADD_HISTORY(line); MIRB_ADD_HISTORY(line);
free(line); free(line);
#endif #endif
if (code_block_open) { if (code_block_open) {
strcat(ruby_code, "\n"); if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
fputs("concatenated input string too long\n", stderr);
continue;
}
strcat(ruby_code, last_code_line); strcat(ruby_code, last_code_line);
} }
else { else {
......
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