From 2ee372d4a83b53dd76377a9d6f64249dee6c9e92 Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Sun, 2 Sep 2001 20:34:44 +0000 Subject: [PATCH] * view.c (hex_search): Don't use sscanf() to search for quoted strings - use strchr instead. --- src/ChangeLog | 5 +++++ src/view.c | 34 ++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index fc1bd41e5..067d11b82 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2001-09-02 Pavel Roskin + + * view.c (hex_search): Don't use sscanf() to search for quoted + strings - use strchr instead. + 2001-08-31 23:14:21 Timur Bakeyev * utilunix.c: init_groups(), destroy_groups(), get_user_permissions() diff --git a/src/view.c b/src/view.c index 4fa7f3926..a7b31b739 100644 --- a/src/view.c +++ b/src/view.c @@ -1680,9 +1680,10 @@ block_search (WView *view, char *buffer, int len) static void hex_search (WView *view, char *text) { - char *buffer; /* Where we hold the information */ - long pos; /* Where did we found the string */ - int block_len = 0; + char *buffer; /* Parsed search string */ + char *cur; /* Current position in it */ + int block_len; /* Length of the search string */ + long pos; /* Position of the string in the file */ int parse_error = 0; if (!*text) { @@ -1692,12 +1693,19 @@ hex_search (WView *view, char *text) /* buffer will never be longer that text */ buffer = g_new (char, strlen (text)); + cur = buffer; /* First convert the string to a stream of bytes */ while (*text) { int val; int ptr; + /* Skip leading spaces */ + if (*text == ' ' || *text == '\t') { + text++; + continue; + } + /* %i matches octal, decimal, and hexadecimal numbers */ if (sscanf (text, "%i%n", &val, &ptr) > 0) { /* Allow signed and unsigned char in the user input */ @@ -1706,22 +1714,32 @@ hex_search (WView *view, char *text) break; } - buffer [block_len++] = (char) val; + *cur++ = (char) val; text += ptr; continue; } /* Try quoted string, strip quotes */ - if (sscanf (text, "\"%[^\"]\"%n", buffer + block_len, &ptr) > 0) { - text += ptr; - block_len += ptr - 2; - continue; + if (*text == '"') { + char *next_quote; + + text++; + next_quote = strchr (text, '"'); + if (next_quote) { + memcpy (cur, text, next_quote - text); + cur += next_quote - text; + text = next_quote + 1; + continue; + } + /* fall through */ } parse_error = 1; break; } + block_len = cur - buffer; + /* No valid bytes in the user input */ if (block_len <= 0 || parse_error) { message (0, _(" Search "), _("Invalid hex search expression"));