public inbox for [email protected]
help / color / mirror / Atom feedFrom: Akshay Joshi <[email protected]>
To: pgsql-hackers <[email protected]>
Subject: [PATCH] pgindent truncates last line of files missing a trailing newline
Date: Mon, 16 Feb 2026 19:39:46 +0530
Message-ID: <CANxoLDfca8O5SkeDxB_j6SVNXd+pNKaDmVmEW+2yyicdU8fy0w@mail.gmail.com> (raw)
Hi Hackers,
I have encountered a bug in "*src/tools/pg_bsd_indent/lexi.c"* where
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.
*Steps to Reproduce:*
1. Create a *header/c* file with some content but no newline at the very
end.
2. Run pgindent on those files.
3. Check the file content. The file is now empty or missing the last
line.
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.
Akshay Joshi
Principal Engineer | Engineering Manager | pgAdmin Hacker
enterprisedb.com
* Blog*: https://www.enterprisedb.com/akshay-joshi
* GitHub*: https://github.com/akshay-joshi
* LinkedIn*: https:// <http://goog_373708537;
www.linkedin.com/in/akshay-joshi-a9317b14
Attachments:
[application/octet-stream] 0001-Fix-pgindent-truncates-last-line-of-files.patch (2.9K, 3-0001-Fix-pgindent-truncates-last-line-of-files.patch)
download | inline diff:
From 5bb56e1e603a8ea98a5f13b7abdaa5f252b3170f Mon Sep 17 00:00:00 2001
From: Akshay Joshi <[email protected]>
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
view thread (7+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCH] pgindent truncates last line of files missing a trailing newline
In-Reply-To: <CANxoLDfca8O5SkeDxB_j6SVNXd+pNKaDmVmEW+2yyicdU8fy0w@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox