summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2020-11-01 14:15:41 -0700
committerTom Rini <trini@konsulko.com>2020-12-01 10:33:38 -0500
commit9528229f22b590cc4b5cf8bf0d3212d2ab08ffd5 (patch)
tree8ee940415eb9a1c3148acdc510d82ae7abd1c68f
parentd422c77ae8e0cb1211b34eb4af442600b0da8d5b (diff)
downloadu-boot-9528229f22b590cc4b5cf8bf0d3212d2ab08ffd5.tar.gz
setexpr: Correct dropping of final unmatched string
At present the 'nlen' variable increases with each loop. If the previous loop had back references, then subsequent loops without back references use the wrong value of nlen. The value is larger, meaning that the string terminator from nbuf is copied along to the main buffer, thus terminating the string prematurely. This leads to the final result being truncated, e.g. missing the last (unmatched) part of the string. So "match match tail" become "replaced replaced" instead of "replaced replaced tail". Fix this by resetting nlen to the correct value each time around the lop. Fixes: 855f18ea0e6 ("setexpr: add regex substring matching and substitution") Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r--cmd/setexpr.c6
-rw-r--r--test/cmd/setexpr.c5
2 files changed, 3 insertions, 8 deletions
diff --git a/cmd/setexpr.c b/cmd/setexpr.c
index dbb43b3be2..0cc7cf15bd 100644
--- a/cmd/setexpr.c
+++ b/cmd/setexpr.c
@@ -147,11 +147,6 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
}
len = strlen(data);
- if (s == NULL)
- nlen = 0;
- else
- nlen = strlen(s);
-
for (loop = 0;; loop++) {
struct cap caps[slre.num_caps + 2];
const char *old;
@@ -187,6 +182,7 @@ int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
old = caps[0].ptr;
olen = caps[0].len;
+ nlen = strlen(s);
if (nlen + 1 >= nbuf_size) {
printf("## error: pattern buffer overflow: have %d, need %d\n",
diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index a6940fd82d..d06dda260e 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -277,12 +277,11 @@ static int setexpr_test_backref(struct unit_test_state *uts)
ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE,
"(this) (is) (surely|indeed)",
"us \\1 \\2 \\3!", true));
-
- /* The following checks fail at present due to bugs in setexpr */
- return 0;
ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
buf);
+ /* The following checks fail at present due to a bug in setexpr */
+ return 0;
for (i = BUF_SIZE; i < 0x1000; i++) {
ut_assertf(buf[i] == (char)i,
"buf byte at %x should be %02x, got %02x)\n",