From 19b93f76dd00f0811d9843ec2d8bb26a3361b595 Mon Sep 17 00:00:00 2001 From: 0x8008135 Date: Sun, 26 Jun 2016 23:18:09 +0200 Subject: [PATCH 01/15] Update tokenline.c Fix for issue #9 --- tokenline.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tokenline.c b/tokenline.c index f215f6f..390b1ac 100644 --- a/tokenline.c +++ b/tokenline.c @@ -134,7 +134,12 @@ static void history_up(t_tokenline *tl) if (entry == -1) return; line_clear(tl); - set_line(tl, tl->hist_buf + entry); + if (tl->hist_begin != 0 && TL_MAX_HISTORY_SIZE == strlen(tl->hist_buf + entry) + entry) { + set_line(tl, tl->hist_buf + entry); + set_line(tl, tl->hist_buf); + } else { + set_line(tl, tl->hist_buf + entry); + } tl->hist_step = entry; } From c011c5a1e77ba6a84ac460e0cf36546ed0d8d3bc Mon Sep 17 00:00:00 2001 From: bvernoux Date: Mon, 22 Aug 2016 20:54:14 +0200 Subject: [PATCH 02/15] Fixed issue on history with up key (thanks to @0x8008135) --- tokenline.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tokenline.c b/tokenline.c index f215f6f..8c50c04 100644 --- a/tokenline.c +++ b/tokenline.c @@ -134,7 +134,12 @@ static void history_up(t_tokenline *tl) if (entry == -1) return; line_clear(tl); - set_line(tl, tl->hist_buf + entry); + if (tl->hist_begin != 0 && TL_MAX_HISTORY_SIZE == strlen(tl->hist_buf + entry) + entry) { + set_line(tl, tl->hist_buf + entry); + set_line(tl, tl->hist_buf); + } else { + set_line(tl, tl->hist_buf + entry); + } tl->hist_step = entry; } @@ -444,6 +449,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, /* Parse word as the type in arg_needed */ switch (arg_needed) { case T_ARG_UINT: + arg_float = strtof(word, &suffix); str_to_uint(word, &arg_uint); if (*suffix) { switch(*suffix) From 08cecc1a32f0413973c0043db0795e9561b0a782 Mon Sep 17 00:00:00 2001 From: bvernoux Date: Mon, 22 Aug 2016 23:19:16 +0200 Subject: [PATCH 03/15] Manage strict suffix only "k", "m" or "g" or no suffix for T_ARG_UINT & T_ARG_FLOAT Manage invalid value for T_ARG_FLOAT --- tokenline.c | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/tokenline.c b/tokenline.c index 8c50c04..9a7feac 100644 --- a/tokenline.c +++ b/tokenline.c @@ -265,14 +265,32 @@ static void history_add(t_tokenline *tl) * Converts string to uint32_t. Takes decimal, hex prefixed with 0x, * binary prefixed with 0b and octal prefixed with 0. Returns FALSE * if conversion in any of these fails. + * endptr: Reference to an object of type char*, whose value is set by + the function to the next character in str after the numerical value. + This parameter can also be a null pointer, in which case it is not used. */ -static int str_to_uint(char *s, uint32_t *out) +static int str_to_uint(char *s, uint32_t *out, char **endptr) { int i; char *suffix; + char character; + + if (endptr != 0) + *endptr = NULL; if (strncmp(s, "0b", 2)) { *out = strtoul(s, &suffix, 0); + character = *suffix; + if (character == 'k' || + character == 'm' || + character == 'g' ) + { + if (endptr != 0) + *endptr = suffix; + + suffix++; + } + if (*suffix != '\0') return FALSE; } else { @@ -309,7 +327,7 @@ static int find_token(t_token *tokens, t_token_dict *token_dict, char *word) for (i = 0; tokens[i].token; i++) { token = tokens[i].token; if (token == T_ARG_UINT) { - if (str_to_uint(word, &arg_uint)) + if (str_to_uint(word, &arg_uint, NULL)) return i; } else if (token > T_ARG_UINT) { continue; @@ -372,7 +390,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, /* Token needed. */ if ((suffix = strchr(word, TL_TOKEN_DELIMITER))) { *suffix++ = 0; - if (!str_to_uint(suffix, &suffix_uint)) { + if (!str_to_uint(suffix, &suffix_uint, NULL)) { tl->print(tl->user, "Invalid number."NL); return FALSE; } @@ -385,7 +403,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, p->tokens[cur_tp++] = t; if (t == T_ARG_UINT) { /* Integer token. */ - str_to_uint(word, &arg_uint); + str_to_uint(word, &arg_uint, NULL); p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); @@ -449,9 +467,13 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, /* Parse word as the type in arg_needed */ switch (arg_needed) { case T_ARG_UINT: - arg_float = strtof(word, &suffix); - str_to_uint(word, &arg_uint); - if (*suffix) { + if( str_to_uint(word, &arg_uint, &suffix) == FALSE) + { + if (!complete_tokens) + tl->print(tl->user, "Invalid value."NL); + return FALSE; + } + if (suffix) { switch(*suffix) { case 'k': @@ -476,7 +498,19 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, break; case T_ARG_FLOAT: arg_float = strtof(word, &suffix); + if( arg_float == 0.0F ) + { + if (!complete_tokens) + tl->print(tl->user, "Invalid value."NL); + return FALSE; + } if (*suffix) { + if( strlen(suffix) > 1 ) + { + if (!complete_tokens) + tl->print(tl->user, "Invalid value."NL); + return FALSE; + } switch(*suffix) { case 'k': From ecf4f518055265a95ec6a7a7ab5256eb83211e3e Mon Sep 17 00:00:00 2001 From: bvernoux Date: Tue, 27 Sep 2016 22:14:14 +0200 Subject: [PATCH 04/15] show_help() fix coverity scan bug "Dereferencing null pointer tl->parsed.last_token_entry" --- tokenline.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tokenline.c b/tokenline.c index 9a7feac..5485116 100644 --- a/tokenline.c +++ b/tokenline.c @@ -599,11 +599,15 @@ static void show_help(t_tokenline *tl, int *words, int num_words) } } - if (num_words == 1) + if (num_words == 1) { /* Just "help" -- global command overview. */ tokens = tl->token_levels[tl->token_level]; - else - tokens = tl->parsed.last_token_entry->subtokens; + } else { + if (tl->parsed.last_token_entry) { + tokens = tl->parsed.last_token_entry->subtokens; + } + } + if (tokens) { for (i = 0; tokens[i].token; i++) { tl->print(tl->user, INDENT); From 418f65c89589eff56d1313857bafe69ba3409d6a Mon Sep 17 00:00:00 2001 From: bvernoux Date: Tue, 27 Sep 2016 22:21:03 +0200 Subject: [PATCH 05/15] show_help() fix 'tokens' may be used uninitialized in this function --- tokenline.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tokenline.c b/tokenline.c index 5485116..684b54d 100644 --- a/tokenline.c +++ b/tokenline.c @@ -605,6 +605,8 @@ static void show_help(t_tokenline *tl, int *words, int num_words) } else { if (tl->parsed.last_token_entry) { tokens = tl->parsed.last_token_entry->subtokens; + } else { + tokens = NULL; } } From 423be864bef3cfbf6a75ea448dd9d62cb6725d67 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Wed, 25 Sep 2019 16:19:48 +0200 Subject: [PATCH 06/15] Various fixes for BP compatibility + TL_TOKEN_DELIMITER issue --- tokenline.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/tokenline.c b/tokenline.c index 684b54d..4ba3487 100644 --- a/tokenline.c +++ b/tokenline.c @@ -23,6 +23,7 @@ #define INDENT " " #define NO_HELP "No help available."NL #define NL "\r\n" +#define CHARS "[]{}/\\_-!^.&%~" static void line_clear(t_tokenline *tl); static void line_backspace(t_tokenline *tl); @@ -30,6 +31,21 @@ static void set_line(t_tokenline *tl, char *line); static char space[] = " "; +static void add_charz(t_tokenline *tl, int c) +{ + if (tl->pos == tl->buf_len) { + tl->buf[tl->buf_len++] = c; + tl->buf[tl->buf_len] = 0; + tl->pos++; + } else { + memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos, + tl->buf_len - tl->pos + 1); + tl->buf[tl->pos] = c; + tl->buf_len++; + tl->pos++; + } +} + static void unsplit_line(t_tokenline *tl) { int quoted, i; @@ -54,10 +70,10 @@ static void unsplit_line(t_tokenline *tl) static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) { - int state, quoted, i; - + int state, quoted, tokened, i, x; state = 1; quoted = FALSE; + tokened = FALSE; *num_words = 0; for (i = 0; i < tl->buf_len && *num_words < TL_MAX_WORDS; i++) { switch (state) { @@ -67,10 +83,45 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) continue; if (tl->buf[i] == '"') quoted = TRUE; + + if (!quoted && strchr(CHARS, tl->buf[i])) { + if(tl->buf[i+1] != '\x20' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { + if((tl->buf_len+1 <= TL_MAX_LINE_LEN) && (*num_words+1 <=TL_MAX_WORDS)){ + tl->pos=i+1; + add_charz(tl, '\x20'); + } else { + tl->print(tl->user, "Too much tokens."NL); + unsplit_line(tl); + return FALSE; + } + } + } words[(*num_words)++] = i + (quoted ? 1 : 0); state = 2; break; case 2: + if(!quoted && tl->buf[i] != ' '){ + tokened = FALSE; + for (x = 1; tl->token_dict[x].token; x++) { + if (!strncmp(tl->buf+words[(*num_words-1)], tl->token_dict[x].tokenstr, + i-words[(*num_words-1)]+1) && strlen(tl->token_dict[x].tokenstr) > 1) + { + tokened = TRUE; + } + } + if (!tokened && strchr(CHARS, tl->buf[i])){ + if (tl->buf[i-1] != '\x20' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ + if((tl->buf_len+1 <= TL_MAX_LINE_LEN) && (*num_words+1 <= TL_MAX_WORDS)){ + tl->pos=i; + add_charz(tl, '\x20'); + } else { + tl->print(tl->user, "Too much tokens."NL); + unsplit_line(tl); + return FALSE; + } + } + } + } /* In a word. */ if (quoted && tl->buf[i] == '"') { quoted = FALSE; @@ -89,12 +140,13 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) unsplit_line(tl); return FALSE; } - if (*num_words == TL_MAX_WORDS) { + if (*num_words >= TL_MAX_WORDS) { if (!silent) tl->print(tl->user, "Too many words."NL); unsplit_line(tl); return FALSE; } + tl->pos=tl->buf_len; return TRUE; } @@ -420,6 +472,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &suffix_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); + *(suffix-1) = TL_TOKEN_DELIMITER; } } p->last_token_entry = &token_stack[cur_tsp][t_idx]; From 05859eb4641355ce065e1355f252905e406e0f1c Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Mon, 30 Sep 2019 14:24:55 +0200 Subject: [PATCH 07/15] Fixes some old bug --- tokenline.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/tokenline.c b/tokenline.c index 4ba3487..d63dc46 100644 --- a/tokenline.c +++ b/tokenline.c @@ -86,7 +86,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) if (!quoted && strchr(CHARS, tl->buf[i])) { if(tl->buf[i+1] != '\x20' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { - if((tl->buf_len+1 <= TL_MAX_LINE_LEN) && (*num_words+1 <=TL_MAX_WORDS)){ + if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <=TL_MAX_WORDS)){ tl->pos=i+1; add_charz(tl, '\x20'); } else { @@ -96,7 +96,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) } } } - words[(*num_words)++] = i + (quoted ? 1 : 0); + words[(*num_words)++] = i ; state = 2; break; case 2: @@ -111,7 +111,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) } if (!tokened && strchr(CHARS, tl->buf[i])){ if (tl->buf[i-1] != '\x20' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ - if((tl->buf_len+1 <= TL_MAX_LINE_LEN) && (*num_words+1 <= TL_MAX_WORDS)){ + if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <= TL_MAX_WORDS)){ tl->pos=i; add_charz(tl, '\x20'); } else { @@ -140,12 +140,18 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) unsplit_line(tl); return FALSE; } - if (*num_words >= TL_MAX_WORDS) { + if (*num_words > TL_MAX_WORDS) { if (!silent) tl->print(tl->user, "Too many words."NL); unsplit_line(tl); return FALSE; } + if (tl->buf_len+1 > TL_MAX_LINE_LEN) { + if (!silent) + tl->print(tl->user, "Too many chars."NL); + unsplit_line(tl); + return FALSE; + } tl->pos=tl->buf_len; return TRUE; @@ -450,7 +456,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, suffix_uint = 0; } - if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1) { + if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1 && word[0] != 0x22) { t = token_stack[cur_tsp][t_idx].token; p->tokens[cur_tp++] = t; if (t == T_ARG_UINT) { @@ -505,9 +511,15 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, if (token_stack[cur_tsp][i].token) { /* Add it in as a token. */ p->tokens[cur_tp++] = T_ARG_STRING; - p->tokens[cur_tp++] = cur_bufsize; - size = strlen(word) + 1; - memcpy(p->buf + cur_bufsize, word, size); + if (word[0] != 0x22) { + p->tokens[cur_tp++] = cur_bufsize; + size = strlen(word) + 1; + memcpy(p->buf + cur_bufsize, word, size); + } else { + p->tokens[cur_tp++] = cur_bufsize + 1; + size = strlen(word+1) + 1; + memcpy(p->buf + cur_bufsize + 1, word + 1, size); + } cur_bufsize += size; p->buf[cur_bufsize] = 0; } else { @@ -588,9 +600,15 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, break; case T_ARG_STRING: p->tokens[cur_tp++] = T_ARG_STRING; - p->tokens[cur_tp++] = cur_bufsize; - size = strlen(word) + 1; - memcpy(p->buf + cur_bufsize, word, size); + if (word[0] != 0x22) { + p->tokens[cur_tp++] = cur_bufsize; + size = strlen(word) + 1; + memcpy(p->buf + cur_bufsize, word, size); + } else { + p->tokens[cur_tp++] = cur_bufsize + 1; + size = strlen(word + 1) + 1; + memcpy(p->buf + cur_bufsize + 1, word + 1, size); + } cur_bufsize += size; p->buf[cur_bufsize] = 0; break; From e5674a12ca029ee374c962e54eef7344b9b2f054 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Mon, 30 Sep 2019 14:37:40 +0200 Subject: [PATCH 08/15] Fix typo --- tokenline.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tokenline.c b/tokenline.c index d63dc46..c8249e4 100644 --- a/tokenline.c +++ b/tokenline.c @@ -456,7 +456,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, suffix_uint = 0; } - if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1 && word[0] != 0x22) { + if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1 && word[0] != '"') { t = token_stack[cur_tsp][t_idx].token; p->tokens[cur_tp++] = t; if (t == T_ARG_UINT) { @@ -511,7 +511,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, if (token_stack[cur_tsp][i].token) { /* Add it in as a token. */ p->tokens[cur_tp++] = T_ARG_STRING; - if (word[0] != 0x22) { + if (word[0] != '"') { p->tokens[cur_tp++] = cur_bufsize; size = strlen(word) + 1; memcpy(p->buf + cur_bufsize, word, size); @@ -600,7 +600,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, break; case T_ARG_STRING: p->tokens[cur_tp++] = T_ARG_STRING; - if (word[0] != 0x22) { + if (word[0] != '"') { p->tokens[cur_tp++] = cur_bufsize; size = strlen(word) + 1; memcpy(p->buf + cur_bufsize, word, size); @@ -608,7 +608,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, p->tokens[cur_tp++] = cur_bufsize + 1; size = strlen(word + 1) + 1; memcpy(p->buf + cur_bufsize + 1, word + 1, size); - } + } cur_bufsize += size; p->buf[cur_bufsize] = 0; break; From 6378dca12846f17254dcef7f628de26e73434891 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Tue, 1 Oct 2019 22:43:09 +0200 Subject: [PATCH 09/15] Fix typos --- tokenline.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tokenline.c b/tokenline.c index c8249e4..59a60df 100644 --- a/tokenline.c +++ b/tokenline.c @@ -23,29 +23,14 @@ #define INDENT " " #define NO_HELP "No help available."NL #define NL "\r\n" -#define CHARS "[]{}/\\_-!^.&%~" +#define HYDRABUS_SPECIAL_CHARS "[]{}/\\_-!^.&%~" static void line_clear(t_tokenline *tl); static void line_backspace(t_tokenline *tl); static void set_line(t_tokenline *tl, char *line); - +static void add_char_silent(t_tokenline *tl, int c); static char space[] = " "; -static void add_charz(t_tokenline *tl, int c) -{ - if (tl->pos == tl->buf_len) { - tl->buf[tl->buf_len++] = c; - tl->buf[tl->buf_len] = 0; - tl->pos++; - } else { - memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos, - tl->buf_len - tl->pos + 1); - tl->buf[tl->pos] = c; - tl->buf_len++; - tl->pos++; - } -} - static void unsplit_line(t_tokenline *tl) { int quoted, i; @@ -84,11 +69,11 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) if (tl->buf[i] == '"') quoted = TRUE; - if (!quoted && strchr(CHARS, tl->buf[i])) { - if(tl->buf[i+1] != '\x20' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { + if (!quoted && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])) { + if(tl->buf[i+1] != ' ' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <=TL_MAX_WORDS)){ tl->pos=i+1; - add_charz(tl, '\x20'); + add_char_silent(tl, ' '); } else { tl->print(tl->user, "Too much tokens."NL); unsplit_line(tl); @@ -109,11 +94,11 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) tokened = TRUE; } } - if (!tokened && strchr(CHARS, tl->buf[i])){ - if (tl->buf[i-1] != '\x20' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ + if (!tokened && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])){ + if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <= TL_MAX_WORDS)){ tl->pos=i; - add_charz(tl, '\x20'); + add_char_silent(tl, ' '); } else { tl->print(tl->user, "Too much tokens."NL); unsplit_line(tl); @@ -751,6 +736,21 @@ static void process_line(t_tokenline *tl) tl->print(tl->user, tl->prompt); } +static void add_char_silent(t_tokenline *tl, int c) +{ + if (tl->pos == tl->buf_len) { + tl->buf[tl->buf_len++] = c; + tl->buf[tl->buf_len] = 0; + tl->pos++; + } else { + memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos, + tl->buf_len - tl->pos + 1); + tl->buf[tl->pos] = c; + tl->buf_len++; + tl->pos++; + } +} + static void add_char(t_tokenline *tl, int c) { int i; From 24e7873ba1c26c7206480d7459852399a9628646 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Wed, 2 Oct 2019 11:55:12 +0200 Subject: [PATCH 10/15] Fix quoted string --- tokenline.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tokenline.c b/tokenline.c index 59a60df..cba7467 100644 --- a/tokenline.c +++ b/tokenline.c @@ -495,18 +495,19 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, } if (token_stack[cur_tsp][i].token) { /* Add it in as a token. */ - p->tokens[cur_tp++] = T_ARG_STRING; - if (word[0] != '"') { - p->tokens[cur_tp++] = cur_bufsize; - size = strlen(word) + 1; - memcpy(p->buf + cur_bufsize, word, size); - } else { + if (word[0] == '"' && word[1] != 0) { + p->tokens[cur_tp++] = T_ARG_STRING; p->tokens[cur_tp++] = cur_bufsize + 1; - size = strlen(word+1) + 1; - memcpy(p->buf + cur_bufsize + 1, word + 1, size); + size = strlen(word + 1) + 1; + memcpy(p->buf + cur_bufsize + 1, word + 1, size); + cur_bufsize += size; + p->buf[cur_bufsize] = 0; + } else if (word[0] == '"' && word[1] == 0){ + cur_bufsize += 2; + } else { + tl->print(tl->user, "Invalid command."NL); + return FALSE; } - cur_bufsize += size; - p->buf[cur_bufsize] = 0; } else { if (!complete_tokens) tl->print(tl->user, "Invalid command."NL); From 0d92e7478422238c969c514e895b2e288f6ec289 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Wed, 2 Oct 2019 15:40:40 +0200 Subject: [PATCH 11/15] Add error message when invalid token found with position highlighted --- tokenline.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tokenline.c b/tokenline.c index cba7467..8ca56bd 100644 --- a/tokenline.c +++ b/tokenline.c @@ -28,6 +28,7 @@ static void line_clear(t_tokenline *tl); static void line_backspace(t_tokenline *tl); static void set_line(t_tokenline *tl, char *line); +static void add_char(t_tokenline *tl, int c); static void add_char_silent(t_tokenline *tl, int c); static char space[] = " "; @@ -506,6 +507,15 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, cur_bufsize += 2; } else { tl->print(tl->user, "Invalid command."NL); + for (i = 0; i < num_words; i++) { + tl->print(tl->user, tl->buf + words[i]); + tl->print(tl->user, " "); + } + tl->print(tl->user,NL); + for(i=0; i < words[w];i++){ + tl->print(tl->user,"-"); + } + tl->print(tl->user,"^"NL); return FALSE; } } else { From 3702e1b8c2725d7016c9c0118e9f8835980c88a1 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Wed, 2 Oct 2019 17:20:13 +0200 Subject: [PATCH 12/15] Fix typos --- tokenline.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tokenline.c b/tokenline.c index 8ca56bd..58c53ca 100644 --- a/tokenline.c +++ b/tokenline.c @@ -511,11 +511,11 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, tl->print(tl->user, tl->buf + words[i]); tl->print(tl->user, " "); } - tl->print(tl->user,NL); - for(i=0; i < words[w];i++){ - tl->print(tl->user,"-"); + tl->print(tl->user, NL); + for(i = 0; i < words[w]; i++){ + tl->print(tl->user, "-"); } - tl->print(tl->user,"^"NL); + tl->print(tl->user, "^"NL); return FALSE; } } else { From 26f23de47e6748d26e1dfcb52df30267a8d420b3 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Fri, 4 Oct 2019 14:53:38 +0200 Subject: [PATCH 13/15] Bug fixes related to line and words length (see tokenline.h also) --- tokenline.c | 49 +++++++++++++++++++++++++++++++++++++------------ tokenline.h | 4 ++-- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/tokenline.c b/tokenline.c index 58c53ca..b51a7ce 100644 --- a/tokenline.c +++ b/tokenline.c @@ -72,13 +72,9 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) if (!quoted && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])) { if(tl->buf[i+1] != ' ' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { - if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <=TL_MAX_WORDS)){ + if((tl->buf_len + 1 < TL_MAX_LINE_LEN - 1) && (*num_words + 1 < TL_MAX_WORDS)){ tl->pos=i+1; add_char_silent(tl, ' '); - } else { - tl->print(tl->user, "Too much tokens."NL); - unsplit_line(tl); - return FALSE; } } } @@ -97,13 +93,9 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) } if (!tokened && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])){ if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ - if((tl->buf_len+2 <= TL_MAX_LINE_LEN) && (*num_words+1 <= TL_MAX_WORDS)){ + if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){ tl->pos=i; add_char_silent(tl, ' '); - } else { - tl->print(tl->user, "Too much tokens."NL); - unsplit_line(tl); - return FALSE; } } } @@ -126,7 +118,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) unsplit_line(tl); return FALSE; } - if (*num_words > TL_MAX_WORDS) { + if (*num_words > TL_MAX_WORDS - 1) { if (!silent) tl->print(tl->user, "Too many words."NL); unsplit_line(tl); @@ -444,10 +436,18 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1 && word[0] != '"') { t = token_stack[cur_tsp][t_idx].token; + if (!(cur_tp + 1 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = t; if (t == T_ARG_UINT) { /* Integer token. */ str_to_uint(word, &arg_uint, NULL); + if (!(cur_tp + 1 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); @@ -460,6 +460,10 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, return FALSE; } if (suffix_uint > 1) { + if (!(cur_tp + 2 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = T_ARG_TOKEN_SUFFIX_INT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &suffix_uint, sizeof(uint32_t)); @@ -497,6 +501,10 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, if (token_stack[cur_tsp][i].token) { /* Add it in as a token. */ if (word[0] == '"' && word[1] != 0) { + if (!(cur_tp + 2 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = T_ARG_STRING; p->tokens[cur_tp++] = cur_bufsize + 1; size = strlen(word + 1) + 1; @@ -552,6 +560,10 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, return FALSE; } } + if (!(cur_tp + 2 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = T_ARG_UINT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); @@ -589,12 +601,20 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, return FALSE; } } + if (!(cur_tp + 2 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = T_ARG_FLOAT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float)); cur_bufsize += sizeof(float); break; case T_ARG_STRING: + if (!(cur_tp + 2 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = T_ARG_STRING; if (word[0] != '"') { p->tokens[cur_tp++] = cur_bufsize; @@ -610,6 +630,10 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, break; case T_ARG_TOKEN: if ((t_idx = find_token(arg_tokens, tl->token_dict, word)) > -1) { + if (!(cur_tp + 1 < TL_MAX_WORDS)){ + tl->print(tl->user, "Too many words."NL); + return FALSE; + } p->tokens[cur_tp++] = arg_tokens[t_idx].token; p->last_token_entry = &arg_tokens[t_idx]; } else { @@ -868,7 +892,8 @@ static void complete(t_tokenline *tl) } else { for (i = strlen(word); i < strlen(tl->token_dict[partial->token].tokenstr); i++) add_char(tl, tl->token_dict[partial->token].tokenstr[i]); - add_char(tl, ' '); + if(i+1 < TL_MAX_LINE_LEN - 1) + add_char(tl, ' '); } } } diff --git a/tokenline.h b/tokenline.h index e480920..0d2e9f8 100644 --- a/tokenline.h +++ b/tokenline.h @@ -20,9 +20,9 @@ #include -#define TL_MAX_LINE_LEN 128 +#define TL_MAX_LINE_LEN 128 //=>127 input chars max #define TL_MAX_ESCAPE_LEN 8 -#define TL_MAX_WORDS 64 +#define TL_MAX_WORDS 64 //=>62 - 63 tokens depending on type #define TL_MAX_TOKEN_LEVELS 8 #define TL_MAX_HISTORY_SIZE 512 #define TL_TOKEN_DELIMITER ':' From 46974de1c6bc4781aa37ab2c61b3081a85fe0886 Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Sun, 6 Oct 2019 12:34:16 +0200 Subject: [PATCH 14/15] Small fixes --- tokenline.c | 42 +++++++++++++++++++++++++++++++----------- tokenline.h | 4 ++-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/tokenline.c b/tokenline.c index b51a7ce..512c71b 100644 --- a/tokenline.c +++ b/tokenline.c @@ -72,9 +72,14 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) if (!quoted && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])) { if(tl->buf[i+1] != ' ' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) { - if((tl->buf_len + 1 < TL_MAX_LINE_LEN - 1) && (*num_words + 1 < TL_MAX_WORDS)){ + if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)) { tl->pos=i+1; add_char_silent(tl, ' '); + } else { + if (!silent) + tl->print(tl->user, "Uncompressed form too big"NL); + unsplit_line(tl); + return FALSE; } } } @@ -92,10 +97,15 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) } } if (!tokened && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])){ - if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':' && i < tl->buf_len){ + if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':') { if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){ tl->pos=i; add_char_silent(tl, ' '); + } else { + if (!silent) + tl->print(tl->user, "Uncompressed form too big"NL); + unsplit_line(tl); + return FALSE; } } } @@ -104,6 +114,17 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) if (quoted && tl->buf[i] == '"') { quoted = FALSE; tl->buf[i] = 0; + if (tl->buf[i+1] != ' ') { + if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){ + tl->pos=i+1; + add_char_silent(tl, ' '); + } else { + if (!silent) + tl->print(tl->user, "Uncompressed form too big"NL); + unsplit_line(tl); + return FALSE; + } + } state = 1; } else if (!quoted && tl->buf[i] == ' ') { tl->buf[i] = 0; @@ -124,12 +145,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) unsplit_line(tl); return FALSE; } - if (tl->buf_len+1 > TL_MAX_LINE_LEN) { - if (!silent) - tl->print(tl->user, "Too many chars."NL); - unsplit_line(tl); - return FALSE; - } + tl->pos=tl->buf_len; return TRUE; @@ -517,10 +533,13 @@ static int tokenize(t_tokenline *tl, int *words, int num_words, tl->print(tl->user, "Invalid command."NL); for (i = 0; i < num_words; i++) { tl->print(tl->user, tl->buf + words[i]); + if (*(tl->buf + words[i]) == '"') { + tl->print(tl->user, "\""); + } tl->print(tl->user, " "); } tl->print(tl->user, NL); - for(i = 0; i < words[w]; i++){ + for(i = 0; i < words[w]; i++) { tl->print(tl->user, "-"); } tl->print(tl->user, "^"NL); @@ -892,8 +911,9 @@ static void complete(t_tokenline *tl) } else { for (i = strlen(word); i < strlen(tl->token_dict[partial->token].tokenstr); i++) add_char(tl, tl->token_dict[partial->token].tokenstr[i]); - if(i+1 < TL_MAX_LINE_LEN - 1) - add_char(tl, ' '); + + if(tl->pos+1 < TL_MAX_LINE_LEN - 1) + add_char(tl, ' '); } } } diff --git a/tokenline.h b/tokenline.h index 0d2e9f8..e480920 100644 --- a/tokenline.h +++ b/tokenline.h @@ -20,9 +20,9 @@ #include -#define TL_MAX_LINE_LEN 128 //=>127 input chars max +#define TL_MAX_LINE_LEN 128 #define TL_MAX_ESCAPE_LEN 8 -#define TL_MAX_WORDS 64 //=>62 - 63 tokens depending on type +#define TL_MAX_WORDS 64 #define TL_MAX_TOKEN_LEVELS 8 #define TL_MAX_HISTORY_SIZE 512 #define TL_TOKEN_DELIMITER ':' From 36584268d402237c1c69875f62661971f07c649a Mon Sep 17 00:00:00 2001 From: 0x800835 Date: Wed, 9 Oct 2019 14:19:08 +0200 Subject: [PATCH 15/15] Name in header + Fix on '"' --- tokenline.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tokenline.c b/tokenline.c index 512c71b..6843bfb 100644 --- a/tokenline.c +++ b/tokenline.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Bert Vermeulen + * Copyright (C) 2019 Karim SUDKI * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -96,7 +97,7 @@ static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) tokened = TRUE; } } - if (!tokened && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])){ + if (!tokened && (strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i]) || tl->buf[i] == '"')){ if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':') { if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){ tl->pos=i;