[PATCH] pgindent truncates last line of files missing a trailing newline
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:
0001-Fix-pgindent-truncates-last-line-of-files.patchapplication/octet-stream; name=0001-Fix-pgindent-truncates-last-line-of-files.patchDownload+13-2
On 2026-02-16 Mo 9:09 AM, Akshay Joshi wrote:
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.
Yeah. I wonder if we shouldn't be just ensuring that there is a line
feed at the end of the buffer, i.e. add one if it's not there. We
shouldn't be adding files without a trailing LF, and ensuring there is
one seems like a reasonable thing to do in pg_bsd_indent.
cheers
andrew
--
Andrew Dunstan
EDB:https://www.enterprisedb.com
Andrew Dunstan <andrew@dunslane.net> writes:
Yeah. I wonder if we shouldn't be just ensuring that there is a line
feed at the end of the buffer, i.e. add one if it's not there. We
shouldn't be adding files without a trailing LF, and ensuring there is
one seems like a reasonable thing to do in pg_bsd_indent.
+1
regards, tom lane
On Mon, Feb 16, 2026 at 10:20 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Andrew Dunstan <andrew@dunslane.net> writes:
Yeah. I wonder if we shouldn't be just ensuring that there is a line
feed at the end of the buffer, i.e. add one if it's not there. We
shouldn't be adding files without a trailing LF, and ensuring there is
one seems like a reasonable thing to do in pg_bsd_indent.+1
+1. That will allow me to remove some code from my patch preparation
script. Ref. [1]/messages/by-id/CAExHW5v8u7-2H2LqWP3ybhh5GnAVVeCOYuTfkg9pmdnrLwAtNA@mail.gmail.com
[1]: /messages/by-id/CAExHW5v8u7-2H2LqWP3ybhh5GnAVVeCOYuTfkg9pmdnrLwAtNA@mail.gmail.com
--
Best Wishes,
Ashutosh Bapat
I have addressed the review comments from Andrew.
Attached is the v2 patch, ready for review.
On Tue, Feb 17, 2026 at 7:50 AM Ashutosh Bapat
<ashutosh.bapat.oss@gmail.com> wrote:
Show quoted text
On Mon, Feb 16, 2026 at 10:20 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Andrew Dunstan <andrew@dunslane.net> writes:
Yeah. I wonder if we shouldn't be just ensuring that there is a line
feed at the end of the buffer, i.e. add one if it's not there. We
shouldn't be adding files without a trailing LF, and ensuring there is
one seems like a reasonable thing to do in pg_bsd_indent.+1
+1. That will allow me to remove some code from my patch preparation
script. Ref. [1][1] /messages/by-id/CAExHW5v8u7-2H2LqWP3ybhh5GnAVVeCOYuTfkg9pmdnrLwAtNA@mail.gmail.com
--
Best Wishes,
Ashutosh Bapat