aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-01-26 19:37:45 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-01-26 19:37:45 +0100
commit2240d20332a68a2a3575698343f157f851d93179 (patch)
treefd0bf87b6e25dab821140599ae6bcf799a8108a8
parent048050a0d8f7e283515bad35b411612e4b2af707 (diff)
Disallow 0-width matches with x// and h//
-rw-r--r--src/grab.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/grab.c b/src/grab.c
index c16b368..13a6e83 100644
--- a/src/grab.c
+++ b/src/grab.c
@@ -411,10 +411,13 @@ cmdh(struct sv sv, struct matches *ms, struct ops ops, size_t i,
if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
break;
- dapush(ms, ((struct sv){sv.p + rm.rm_so, rm.rm_eo - rm.rm_so}));
+ if (rm.rm_so < rm.rm_eo)
+ dapush(ms, ((struct sv){sv.p + rm.rm_so, rm.rm_eo - rm.rm_so}));
+ else {
+ rune unused;
+ rm.rm_eo += u8tor_uc(&unused, sv.p + rm.rm_eo);
+ }
- if (rm.rm_so == rm.rm_eo)
- rm.rm_eo++;
rm = (regmatch_t){
.rm_so = rm.rm_eo,
.rm_eo = sv.len,
@@ -492,20 +495,23 @@ cmdx(struct sv sv, struct matches *ms, struct ops ops, size_t i,
if (regexec(&op.pat, sv.p, 1, &rm, REG_STARTEND) == REG_NOMATCH)
break;
- nsv = (struct sv){
- .p = sv.p + rm.rm_so,
- .len = rm.rm_eo - rm.rm_so,
- };
- if (i + 1 == ops.len)
- putf(nsv, ms, filename);
- else {
- size_t save = ms->len;
- op_table[(uchar)ops.buf[i + 1].c](nsv, ms, ops, i + 1, filename);
- ms->len = save;
+ if (rm.rm_so < rm.rm_eo) {
+ nsv = (struct sv){
+ .p = sv.p + rm.rm_so,
+ .len = rm.rm_eo - rm.rm_so,
+ };
+ if (i + 1 == ops.len)
+ putf(nsv, ms, filename);
+ else {
+ size_t save = ms->len;
+ op_table[(uchar)ops.buf[i + 1].c](nsv, ms, ops, i + 1,
+ filename);
+ ms->len = save;
+ }
+ } else {
+ rune unused;
+ rm.rm_eo += u8tor_uc(&unused, sv.p + rm.rm_eo);
}
-
- if (rm.rm_so == rm.rm_eo)
- rm.rm_eo++;
rm = (regmatch_t){
.rm_so = rm.rm_eo,
.rm_eo = sv.len,