From 44494b046145941d3bed279e0046cb0ef9279669 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 22 Jan 2023 19:51:30 +0200 Subject: [PATCH] fixed bug in line-buffered receive routine --- src/ftp.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ftp.c b/src/ftp.c index 806c766..ec6dbab 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -353,7 +353,13 @@ static int handle_control(struct ftp *ftp) int i, sz, rd; char *buf, *start, *end; - while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) { + for(;;) { + if((sz = sizeof ftp->crecv - ftp->num_crecv) <= 0) { + /* discard buffer */ + warnmsg("discard buffer\n"); + sz = sizeof ftp->crecv; + ftp->num_crecv = 0; + } start = ftp->crecv + ftp->num_crecv; if((rd = recv(ftp->ctl, start, sz, 0)) == -1) { if(errno == EINTR) continue; @@ -368,7 +374,9 @@ static int handle_control(struct ftp *ftp) end = start + rd; buf = ftp->crecv; for(i=0; icrecv && buf < end) { ftp->num_crecv = end - buf; memmove(ftp->crecv, buf, ftp->num_crecv); + } else { + ftp->num_crecv = 0; } } return 0; @@ -460,6 +470,7 @@ static void proc_control(struct ftp *ftp, const char *buf) } return; } + ftp->busy = 0; switch(code) { case 220: @@ -479,7 +490,6 @@ static void proc_control(struct ftp *ftp, const char *buf) errmsg("login failed\n"); break; } - ftp->busy = 0; } static int newconn(struct ftp *ftp) -- 1.7.10.4