Skip to content

Commit 296cb5e

Browse files
authored
Merge pull request libgit2#4763 from cschlack/fix_ng_packets
Fix 'invalid packet line' for ng packets containing errors
2 parents 5b0258a + 50dd7fe commit 296cb5e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/transports/smart_pkt.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static int ok_pkt(git_pkt **out, const char *line, size_t len)
282282
static int ng_pkt(git_pkt **out, const char *line, size_t len)
283283
{
284284
git_pkt_ng *pkt;
285-
const char *ptr;
285+
const char *ptr, *eol;
286286
size_t alloclen;
287287

288288
pkt = git__malloc(sizeof(*pkt));
@@ -291,11 +291,13 @@ static int ng_pkt(git_pkt **out, const char *line, size_t len)
291291
pkt->ref = NULL;
292292
pkt->type = GIT_PKT_NG;
293293

294+
eol = line + len;
295+
294296
if (len < 3)
295297
goto out_err;
296298
line += 3; /* skip "ng " */
297-
len -= 3;
298-
if (!(ptr = memchr(line, ' ', len)))
299+
300+
if (!(ptr = memchr(line, ' ', eol - line)))
299301
goto out_err;
300302
len = ptr - line;
301303

@@ -306,11 +308,11 @@ static int ng_pkt(git_pkt **out, const char *line, size_t len)
306308
memcpy(pkt->ref, line, len);
307309
pkt->ref[len] = '\0';
308310

309-
if (len < 1)
310-
goto out_err;
311311
line = ptr + 1;
312-
len -= 1;
313-
if (!(ptr = memchr(line, '\n', len)))
312+
if (line >= eol)
313+
goto out_err;
314+
315+
if (!(ptr = memchr(line, '\n', eol - line)))
314316
goto out_err;
315317
len = ptr - line;
316318

0 commit comments

Comments
 (0)