# HG changeset patch # User al # Date 1353450218 0 # Node ID c7c59b8e3af83766a4ab5ab1f13d51fd1911af6e # Parent 4fe8922d6242fb7984130dcb92b826f14cb416e8 stream ftp: Allocate command buffer on-heap diff -r 4fe8922d6242 -r c7c59b8e3af8 stream/stream_ftp.c --- a/stream/stream_ftp.c Tue Nov 20 22:22:04 2012 +0000 +++ b/stream/stream_ftp.c Tue Nov 20 22:23:38 2012 +0000 @@ -52,6 +52,7 @@ int handle; int cavail,cleft; char *buf; + char *cmd_buf; } stream_priv_dflts = { "anonymous","no@spam", NULL, @@ -62,9 +63,12 @@ -1, 0,0, - NULL + NULL, + NULL, }; +#define CMD_BUFSIZE 256 + #define BUFSIZE 2048 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f) @@ -289,7 +293,7 @@ static int FtpOpenData(stream_t* s,off_t newpos) { struct stream_priv_s* p = s->priv; int resp; - char str[256],rsp_txt[256]; + char rsp_txt[256]; // Open a new connection s->fd = FtpOpenPort(p); @@ -297,21 +301,21 @@ if(s->fd < 0) return 0; if(newpos > 0) { - snprintf(str,255,"REST %"PRId64, (int64_t)newpos); + snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"REST %"PRId64, (int64_t)newpos); - resp = FtpSendCmd(str,p,rsp_txt); + resp = FtpSendCmd(p->cmd_buf,p,rsp_txt); if(resp != 3) { - mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); + mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt); newpos = 0; } } // Get the file - snprintf(str,255,"RETR %s",p->filename); - resp = FtpSendCmd(str,p,rsp_txt); + snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"RETR %s",p->filename); + resp = FtpSendCmd(p->cmd_buf,p,rsp_txt); if(resp != 1) { - mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); + mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt); return 0; } @@ -406,6 +410,7 @@ } free(p->buf); + free(p->cmd_buf); m_struct_free(&stream_opts,p); } @@ -416,7 +421,7 @@ int resp; int64_t len = 0; struct stream_priv_s* p = (struct stream_priv_s*)opts; - char str[256],rsp_txt[256]; + char rsp_txt[256]; if(mode != STREAM_READ) { mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode); @@ -432,8 +437,9 @@ // Allocate buffers p->buf = malloc(BUFSIZE); + p->cmd_buf = malloc(CMD_BUFSIZE); - if (!p->buf) { + if (!p->buf || !p->cmd_buf) { close_f(stream); m_struct_free(&stream_opts,opts); return STREAM_ERROR; @@ -458,20 +464,20 @@ } // Login - snprintf(str,255,"USER %s",p->user); - resp = FtpSendCmd(str,p,rsp_txt); + snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"USER %s",p->user); + resp = FtpSendCmd(p->cmd_buf,p,rsp_txt); // password needed if(resp == 3) { - snprintf(str,255,"PASS %s",p->pass); - resp = FtpSendCmd(str,p,rsp_txt); + snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"PASS %s",p->pass); + resp = FtpSendCmd(p->cmd_buf,p,rsp_txt); if(resp != 2) { - mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); + mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt); close_f(stream); return STREAM_ERROR; } } else if(resp != 2) { - mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); + mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt); close_f(stream); return STREAM_ERROR; } @@ -485,10 +491,10 @@ } // Get the filesize - snprintf(str,255,"SIZE %s",p->filename); - resp = FtpSendCmd(str,p,rsp_txt); + snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"SIZE %s",p->filename); + resp = FtpSendCmd(p->cmd_buf,p,rsp_txt); if(resp != 2) { - mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); + mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt); } else { int dummy; sscanf(rsp_txt,"%d %"SCNd64,&dummy,&len);