public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
* [PATCH] [PATCH v2] gpg-interface.c: trim CR only before LF
@ 2025-10-16 20:03 Okhuomon Ajayi
  2025-10-17 11:55 ` Christian Couder
  0 siblings, 1 reply; 3+ messages in thread
From: Okhuomon Ajayi @ 2025-10-16 20:03 UTC (permalink / raw)
  To: git; +Cc: Okhuomon Ajayi

Problem:
The function remove_cr_after() stripped CRs blindly. The comment suggested
NEEDSWORK: trim only CRs before LF. This caused potential confusion.

Solution:
Rename remove_cr_after() to trim_cr_before_lf() and update the comment:
"Trim CR characters only when they appear before LF (\r\n) line endings."
This keeps lone CRs intact and documents intent clearly.

Also improved formatting.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail•com>
---
 gpg-interface.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/gpg-interface.c b/gpg-interface.c
index c961607444..2d114e05e8 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -964,23 +964,37 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
 	return use_format->sign_buffer(buffer, signature, signing_key);
 }
 
-/*
- * Trim CR characters only when they appear before LF (\r\n) line endings.
- * This avoids removing legitimate lone CRs from teh content.
- */
+/* Convert CRLF to LF, in case we are on Windows */
 static void trim_cr_before_lf(struct strbuf *buffer, size_t offset)
 {
 	size_t i, j;
 
+	for (i = j = offset; i < buffer->len; i++) {
+		/* Skip CR only if it comes right before LF */
+		if (buffer->buf[i] == '\r' && i + 1 < buffer->len &&
+		    buffer->buf[i + 1] == '\n')
+			continue;
+
+		if (i != j)
+			buffer->buf[j] = buffer->buf[i];
+		j++;
+	}
+	strbuf_setlen(buffer, j);
+}
+
+static void trim_cr_before_lf(struct strbuf *buffer, size_t offset)
+{
+        size_t i, j;
+
 	for (i = j = offset; i < buffer->len; i++) {
 	     /* skip CR only if it comes right before LF */
-		if (buffer->buf[i] == '\r' && i + 1 < buffer->len && buffer->buf[i+1] == '\n')
-		    continue;
+	     if (buffer->buf[i] == '\r' && i + 1 < buffer->len &&
+		 buffer->buf[i+1] == '\n')
+		     continue;
  
-			if (i != j)
-				buffer->buf[j] = buffer->buf[i];
-			j++;
-		
+             if (i != j)
+		     buffer->buf[j] = buffer->buf[i];
+	     j++;
 	}
 	strbuf_setlen(buffer, j);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-17 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-16 20:03 [PATCH] [PATCH v2] gpg-interface.c: trim CR only before LF Okhuomon Ajayi
2025-10-17 11:55 ` Christian Couder
2025-10-17 19:12   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox