Skip to content

Commit 40fd84c

Browse files
committed
smart_pkt: explicitly avoid integer overflows when parsing packets
When parsing data, progress or error packets, we need to copy the contents of the rest of the current packet line into the flex-array of the parsed packet. To keep track of this array's length, we then assign the remaining length of the packet line to the structure. We do have a mismatch of types here, as the structure's `len` field is a signed integer, while the length that we are assigning has type `size_t`. On nearly all platforms, this shouldn't pose any problems at all. The line length can at most be 16^4, as the line's length is being encoded by exactly four hex digits. But on a platforms with 16 bit integers, this assignment could cause an overflow. While such platforms will probably only exist in the embedded ecosystem, we still want to avoid this potential overflow. Thus, we now simply change the structure's `len` member to be of type `size_t` to avoid any integer promotion.
1 parent 4a5804c commit 40fd84c

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/transports/smart.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ typedef struct {
9090

9191
typedef struct {
9292
git_pkt_type type;
93-
int len;
93+
size_t len;
9494
char data[GIT_FLEX_ARRAY];
9595
} git_pkt_data;
9696

9797
typedef git_pkt_data git_pkt_progress;
9898

9999
typedef struct {
100100
git_pkt_type type;
101-
int len;
101+
size_t len;
102102
char error[GIT_FLEX_ARRAY];
103103
} git_pkt_err;
104104

src/transports/smart_pkt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ static int err_pkt(git_pkt **out, const char *line, size_t len)
118118
GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
119119
pkt = git__malloc(alloclen);
120120
GITERR_CHECK_ALLOC(pkt);
121-
122121
pkt->type = GIT_PKT_ERR;
123-
pkt->len = (int)len;
122+
pkt->len = len;
123+
124124
memcpy(pkt->error, line, len);
125125
pkt->error[len] = '\0';
126126

@@ -142,7 +142,7 @@ static int data_pkt(git_pkt **out, const char *line, size_t len)
142142
GITERR_CHECK_ALLOC(pkt);
143143

144144
pkt->type = GIT_PKT_DATA;
145-
pkt->len = (int) len;
145+
pkt->len = len;
146146
memcpy(pkt->data, line, len);
147147

148148
*out = (git_pkt *) pkt;
@@ -163,7 +163,7 @@ static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len)
163163
GITERR_CHECK_ALLOC(pkt);
164164

165165
pkt->type = GIT_PKT_PROGRESS;
166-
pkt->len = (int) len;
166+
pkt->len = len;
167167
memcpy(pkt->data, line, len);
168168

169169
*out = (git_pkt *) pkt;

0 commit comments

Comments
 (0)