Commit 48e8d5a7ac for asterisk.org
commit 48e8d5a7acbfaeab1b62ae8bf07007cc6658d482
Author: Naveen Albert <asterisk@phreaknet.org>
Date: Mon Sep 14 08:26:00 2026 -0400
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
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 @@
<parameter name="offset">
<para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
of bytes back from the end of the file.</para>
+ <note>
+ <para>Line offsets begin at 0, not 1.</para>
+ </note>
</parameter>
<parameter name="length">
<para>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.</para>
<optionlist>
<option name="u">
- <para>Unix newline format.</para>
+ <para>Unix newline format (LF).</para>
</option>
<option name="d">
- <para>DOS newline format.</para>
+ <para>DOS newline format (CR LF).</para>
</option>
<option name="m">
- <para>Macintosh newline format.</para>
+ <para>Macintosh newline format (CR).</para>
</option>
</optionlist>
</parameter>
@@ -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) { \