{"thread":{"id":"58899","subject":"[PATCH 1/1] line-range: Fix infinite loop bug with degenerate regex","startedAt":"2022-12-05T19:35:58Z","lastAt":"2022-12-05T19:35:58Z","messageCount":1,"participants":["Lars Kellogg-Stedman"],"isPatch":true,"patchVersion":1,"patchTotal":1},"messages":[{"id":"468542","messageId":"20221205192338.2413155-1-lars@oddbit.com","threadId":"58899","inReplyTo":null,"subject":"[PATCH 1/1] line-range: Fix infinite loop bug with degenerate regex","fromName":"Lars Kellogg-Stedman","fromEmail":"lars@oddbit.com","sentAt":"2022-12-05T19:23:38Z","receivedAt":"2022-12-05T19:35:58Z","isPatch":true,"sender":{"key":"lars@oddbit.com","avatar":"https://avatars.githubusercontent.com/u/82622?v=4"},"body":"When the -L argument to \"git log\" is passed the degenerate regular\nexpression \"$\" (as in \"-L :$:line-range.c\"), this results in an\ninfinite loop in find_funcname_matching_regexp() (the function\niterates through the file correctly, but when it reaches the end of\nthe file it matches $ against the empty string, \"\", and at that points\nloops forever).\n\nModify the loop condition from while (1) to while (*start) so that the\nloop exits when start is the empty string. In this case, \"git log\" exits\nwith the error:\n\n    fatal: -L parameter '$' starting at line 1: no match\n\nOriginally reported in <https://stackoverflow.com/q/74690545/147356>.\n\nSigned-off-by: Lars Kellogg-Stedman <lars@oddbit.com>\n---\n line-range.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/line-range.c b/line-range.c\nindex 955a8a9535..9482d93d62 100644\n--- a/line-range.c\n+++ b/line-range.c\n@@ -135,7 +135,7 @@ static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char\n {\n \tint reg_error;\n \tregmatch_t match[1];\n-\twhile (1) {\n+\twhile (*start) {\n \t\tconst char *bol, *eol;\n \t\treg_error = regexec(regexp, start, 1, match, 0);\n \t\tif (reg_error == REG_NOMATCH)\n-- \n2.38.1\n\n"}]}