From 644c2708478617ce78e2d12f973a6c496ee5dbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 14 Mar 2016 13:35:31 +0100 Subject: text-regex: fix possible infinite loop when searching backwards --- text-regex.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/text-regex.c b/text-regex.c index c4134aa..c3d1cb2 100644 --- a/text-regex.c +++ b/text-regex.c @@ -1,4 +1,5 @@ #include +#include #include #include "text-regex.h" @@ -58,7 +59,16 @@ int text_search_range_backward(Text *txt, size_t pos, size_t len, Regex *r, size pmatch[i].start = match[i].rm_so == -1 ? EPOS : pos + (size_t)(cur - buf) + match[i].rm_so; pmatch[i].end = match[i].rm_eo == -1 ? EPOS : pos + (size_t)(cur - buf) + match[i].rm_eo; } - cur += match[0].rm_eo; + if (match[0].rm_so == 0 && match[0].rm_eo == 0) { + /* empty match at the beginning of cur, advance to next line */ + if ((cur = strchr(cur, '\n'))) + cur++; + else + break; + + } else { + cur += match[0].rm_eo; + } } free(buf); return ret; -- cgit v1.2.3