Subject: [PATCH] pgindent truncates last line of files missing a trailing newline

From: Akshay Joshi <[email-masked]>
Date: Mon, 16 Feb 2026 19:39:46 +0530

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

---

From: Andrew Dunstan <[email-masked]>
Date: Mon, 16 Feb 2026 11:28:58 -0500

On 2026-02-16 Mo 9:09 AM, Akshay Joshi wrote:
> 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.

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

---

From: Tom Lane <[email-masked]>
Date: Mon, 16 Feb 2026 11:50:34 -0500

> one seems like a reasonable thing to do in pg_bsd_indent.

+1

			regards, tom lane

---

From: Ashutosh Bapat <[email-masked]>
Date: Tue, 17 Feb 2026 07:50:42 +0530

> > one seems like a reasonable thing to do in pg_bsd_indent.

+1. That will allow me to remove some code from my patch preparation
script. Ref. [1]

[1] https://www.postgresql.org/message-id/flat/CAExHW5v8u7-2H2LqWP3ybhh5GnAVVeCOYuTfkg9pmdnrLwAtNA%40mail.gmail.com#fdbb87e825f89e7109dfb0ed959b2dd4

-- 
Best Wishes,
Ashutosh Bapat

---

From: Akshay Joshi <[email-masked]>
Date: Tue, 17 Feb 2026 13:24:05 +0530

I have addressed the review comments from Andrew.
Attached is the v2 patch, ready for review.

---

From: Andrew Dunstan <[email-masked]>
Date: Fri, 27 Mar 2026 15:08:22 -0400

On 2026-03-26 Th 12:29 PM, Tom Lane wrote:
> Akshay Joshi<[email-masked]> writes:
> I'm not sure I want to expend the brain cells to figure out whether
> this is a correct/complete patch.  If pg_bsd_indent were less of an
> undercommented spaghetti-code nightmare, maybe fixing it here would
> be reasonable.  But as things stand, why don't we just fix this in
> the perl wrapper, as attached?

Well, I thought we were trying to reduce the fixups we did in pgindent. 
However, I take your point about the ugly nature of the pg_bsd_indent 
code. So I'm ok with this fix, which is quite straightforward.

>
> (In any case, I'm not in favor of adding a test case, because that
> would require putting a trailing-newline-less .c file into our tree.
> At best there would be a permanent hazard of something "fixing"
> the file.)
>
> 			

fair point.

cheers

andrew

---

From: Bruce Momjian <[email-masked]>
Date: Sat, 28 Mar 2026 22:26:35 -0400

On Fri, Mar 27, 2026 at 03:28:16PM -0400, Tom Lane wrote:
> Yeah, other things being equal I too would prefer to fix it within
> pg_bsd_indent.  But other things are not equal: the complexity
> and reviewability of the proposed v2 patch are not comparable to
> a one-liner fix in the wrapper.

I think the only way forward if we want to continue modifying
pg_bsd_indent is to rename most of the variables and add some sanity to
the code.

-- 
  Bruce Momjian  <[email-masked]>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Do not let urgent matters crowd out time for investment in the future.