From 5bb56e1e603a8ea98a5f13b7abdaa5f252b3170f Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Mon, 16 Feb 2026 16:39:08 +0530 Subject: [PATCH] Fix pgindent truncates last line of files missing a trailing newline. pgindent was incorrectly dropping the final line of a file if that line did not end with a newline character (\n). This occurred because the EOF logic triggered a termination of the processing loop before the remaining buffer was flushed to the output. This patch ensures that had_eof states do not bypass the final buffer processing, preserving the integrity of the source code regardless of trailing whitespace. --- src/tools/pg_bsd_indent/lexi.c | 4 +++- src/tools/pg_bsd_indent/tests/no_trailing_newline.0 | 4 ++++ src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/tools/pg_bsd_indent/tests/no_trailing_newline.0 create mode 100644 src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout diff --git a/src/tools/pg_bsd_indent/lexi.c b/src/tools/pg_bsd_indent/lexi.c index 943bf7ce6b0..3e46309b257 100644 --- a/src/tools/pg_bsd_indent/lexi.c +++ b/src/tools/pg_bsd_indent/lexi.c @@ -219,6 +219,7 @@ lexi(struct parser_state *state) * forces a following operator to be unary */ int code; /* internal code to be returned */ char qchar; /* the delimiter character for a string */ + int eof_before_fill; /* had_eof before fill_buffer() call */ e_token = s_token; /* point to start of place to save token */ unary_delim = false; @@ -446,6 +447,7 @@ lexi(struct parser_state *state) *e_token++ = *buf_ptr; /* if it is only a one-character token, it is * moved here */ *e_token = '\0'; + eof_before_fill = had_eof; if (++buf_ptr >= buf_end) fill_buffer(); @@ -453,7 +455,7 @@ lexi(struct parser_state *state) case '\n': unary_delim = state->last_u_d; state->last_nl = true; /* remember that we just had a newline */ - code = (had_eof ? 0 : newline); + code = (eof_before_fill ? 0 : newline); /* * if data has been exhausted, the newline is a dummy, and we should diff --git a/src/tools/pg_bsd_indent/tests/no_trailing_newline.0 b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0 new file mode 100644 index 00000000000..3233990e11f --- /dev/null +++ b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0 @@ -0,0 +1,4 @@ +/* Test: file without trailing newline */ +void test(void) { +int x = 1; +} \ No newline at end of file diff --git a/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout new file mode 100644 index 00000000000..c632d62f16c --- /dev/null +++ b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout @@ -0,0 +1,6 @@ +/* Test: file without trailing newline */ +void +test(void) +{ + int x = 1; +} -- 2.51.0