AP_Scripting: Allow generator to cope with windows line endings

Also fixes allowing an extra new line on the depends entries to be
emitted, as well as a possible stack overflow in the generator if
you inserted a malicious comment.
This commit is contained in:
Michael du Breuil 2020-06-23 10:22:57 -07:00 committed by WickedShell
parent dfbb357aaf
commit ff007dd017

View File

@ -202,7 +202,7 @@ char * next_token(void) {
trace(TRACE_TOKENS, "Token %d:%d %s", state.line_num, state.token_num, state.token);
if ((state.token!= NULL) && (strcmp(state.token, keyword_comment) == 0)) {
trace(TRACE_TOKENS, "Detected comment %d", state.line_num);
while (next_token()) {} // burn all the symbols
state.token = NULL; // burn the line
}
return state.token;
}
@ -211,9 +211,24 @@ char * start_line(void) {
while (fgets(state.line, sizeof(state.line)/sizeof(state.line[0]), description) != NULL) {//state.line = readline(NULL))) {
state.line_num++;
const size_t length = strlen(state.line);
if (length > 1 && state.line[length - 2] == '\r') {
trace(TRACE_TOKENS, "Discarding carriage return");
if (length == 2) { // empty line of just carriage return, loop again
continue;
}
state.line[length - 2] = '\0';
} else if (length > 0 && state.line[length - 1] == '\n') {
trace(TRACE_TOKENS, "Discarding newline");
if (length == 1) { // empty line of just carriage return, loop again
continue;
}
state.line[length - 1] = '\0';
}
state.token = strtok(state.line, token_delimiters);
state.token_num = 1;
trace(TRACE_TOKENS, "Token %d:%d %s", state.line_num, state.token_num, state.token);
trace(TRACE_TOKENS, "Start of line token %d:%d %s", state.line_num, state.token_num, state.token);
if (state.token != NULL) {
break;