From ff007dd017cc8a83bf92323caa227ed751175f10 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Tue, 23 Jun 2020 10:22:57 -0700 Subject: [PATCH] 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. --- libraries/AP_Scripting/generator/src/main.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libraries/AP_Scripting/generator/src/main.c b/libraries/AP_Scripting/generator/src/main.c index b0575b1ab5..e981c1c8f7 100644 --- a/libraries/AP_Scripting/generator/src/main.c +++ b/libraries/AP_Scripting/generator/src/main.c @@ -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,12 +211,27 @@ 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; + break; } }