From ae3ac2d2654b0c54f52b37f1bceb40481d32e465 Mon Sep 17 00:00:00 2001 From: Naveen Albert Date: Mon, 14 Sep 2026 08:26:00 -0400 Subject: [PATCH] func_env: Fix line counting in FILE function for DOS (CR LF) endings. The DOS line counting mode was looking for LF CR, when it should have been looking for CR LF. As a result, line mode never worked properly for files with DOS (CR LF) line endings, instead erroneously triggering an error about the offset being negative. This bug has been present since line mode was introduced in commit 50d5f134c8d604081c4b9c208a23db9aa97cd560. LF CR is not a line ending sequence that exists in any line ending format. Swap the order around so that DOS mode works properly. Also clarify some of the documentation around FILE operation. Resolves: #2164 --- funcs/func_env.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/funcs/func_env.c b/funcs/func_env.c index 3302df3721..6830292026 100644 --- a/funcs/func_env.c +++ b/funcs/func_env.c @@ -144,6 +144,9 @@ Maybe specified as any number. If negative, offset specifies the number of bytes back from the end of the file. + + Line offsets begin at 0, not 1. + If specified, will limit the length of the data read to that size. If negative, @@ -173,13 +176,13 @@ used to delimit the type of line terminators in line mode. @@ -547,9 +550,9 @@ static int file_count_line(struct ast_channel *chan, const char *cmd, char *data #define LINE_COUNTER(cptr, term, counter) \ if (*cptr == '\n' && term == FF_UNIX) { \ counter++; \ - } else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \ + } else if (*cptr == '\r' && term == FF_DOS && dos_state == 0) { \ dos_state = 1; \ - } else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \ + } else if (*cptr == '\n' && term == FF_DOS && dos_state == 1) { \ dos_state = 0; \ counter++; \ } else if (*cptr == '\r' && term == FF_MAC) { \