git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pkt-line: don't check string length in packet_length()
@ 2023-07-01  7:05 René Scharfe
  2023-07-05  5:56 ` Junio C Hamano
  2023-07-07 21:47 ` [PATCH v2] pkt-line: add size parameter to packet_length() René Scharfe
  0 siblings, 2 replies; 8+ messages in thread
From: René Scharfe @ 2023-07-01  7:05 UTC (permalink / raw)
  To: Git List

hex2chr() takes care not to run over the end of a short string.
101736a14c (pkt-line: extern packet_length(), 2020-05-19) turned the
input parameter of packet_length() from a string pointer into an array
of known length, making string length checks unnecessary.  Get rid of
them by using hexval() directly.

The resulting branchless code is simpler and it becomes easier to see
that the function mirrors set_packet_header().

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 pkt-line.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/pkt-line.c b/pkt-line.c
index 62b4208b66..6e022029ca 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -375,8 +375,10 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,

 int packet_length(const char lenbuf_hex[4])
 {
-	int val = hex2chr(lenbuf_hex);
-	return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
+	return	hexval(lenbuf_hex[0]) << 12 |
+		hexval(lenbuf_hex[1]) <<  8 |
+		hexval(lenbuf_hex[2]) <<  4 |
+		hexval(lenbuf_hex[3]);
 }

 static char *find_packfile_uri_path(const char *buffer)
--
2.41.0

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

end of thread, other threads:[~2023-07-07 21:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-01  7:05 [PATCH] pkt-line: don't check string length in packet_length() René Scharfe
2023-07-05  5:56 ` Junio C Hamano
2023-07-05 16:15   ` René Scharfe
2023-07-05 20:33     ` Junio C Hamano
2023-07-05 21:11       ` René Scharfe
2023-07-05 22:27         ` Junio C Hamano
2023-07-06  5:07           ` René Scharfe
2023-07-07 21:47 ` [PATCH v2] pkt-line: add size parameter to packet_length() René Scharfe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).