Skip to content

Commit 377d1ca

Browse files
committed
Merge branch 'rs/packet-length-simplify'
Code simplification. * rs/packet-length-simplify: pkt-line: add size parameter to packet_length()
2 parents 9187b27 + 3e81b89 commit 377d1ca

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

pkt-line.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,14 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
373373
return ret;
374374
}
375375

376-
int packet_length(const char lenbuf_hex[4])
376+
int packet_length(const char lenbuf_hex[4], size_t size)
377377
{
378-
int val = hex2chr(lenbuf_hex);
379-
return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
378+
if (size < 4)
379+
BUG("buffer too small");
380+
return hexval(lenbuf_hex[0]) << 12 |
381+
hexval(lenbuf_hex[1]) << 8 |
382+
hexval(lenbuf_hex[2]) << 4 |
383+
hexval(lenbuf_hex[3]);
380384
}
381385

382386
static char *find_packfile_uri_path(const char *buffer)
@@ -419,7 +423,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
419423
return PACKET_READ_EOF;
420424
}
421425

422-
len = packet_length(linelen);
426+
len = packet_length(linelen, sizeof(linelen));
423427

424428
if (len < 0) {
425429
if (options & PACKET_READ_GENTLE_ON_READ_ERROR)

pkt-line.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int packet_read(int fd, char *buffer, unsigned size, int options);
9494
* If lenbuf_hex contains non-hex characters, return -1. Otherwise, return the
9595
* numeric value of the length header.
9696
*/
97-
int packet_length(const char lenbuf_hex[4]);
97+
int packet_length(const char lenbuf_hex[4], size_t size);
9898

9999
/*
100100
* Read a packetized line into a buffer like the 'packet_read()' function but

remote-curl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ static void check_pktline(struct check_pktline_state *state, const char *ptr, si
763763
size -= digits_remaining;
764764

765765
if (state->len_filled == 4) {
766-
state->remaining = packet_length(state->len_buf);
766+
state->remaining = packet_length(state->len_buf,
767+
sizeof(state->len_buf));
767768
if (state->remaining < 0) {
768769
die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
769770
} else if (state->remaining == 2) {

0 commit comments

Comments
 (0)