Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Critical memory leak in int ftplib::FtpOpenPort #13

Open
Fojtik opened this issue Dec 1, 2016 · 1 comment
Open

Critical memory leak in int ftplib::FtpOpenPort #13

Fojtik opened this issue Dec 1, 2016 · 1 comment

Comments

@Fojtik
Copy link

Fojtik commented Dec 1, 2016

Original code:

  ftphandle *ctrl = static_cast<ftphandle*>(calloc(1,sizeof(ftphandle)));
  if (ctrl == NULL)
  {
    perror("calloc");
    net_close(sData);
    return -1;
  }
  if ((mode == 'A') && ((ctrl->buf = static_cast<char*>(malloc(FTPLIB_BUFSIZ))) == NULL))
  {
    perror("calloc");
    net_close(sData);
    free(ctrl);
    return -1;
  }

  if (!FtpSendCmd(cmd, '1', nControl))
  {
    FtpClose(*nData);
    *nData = NULL;
    return -1;     /// THIS CAUSES MEMORY LEAK IN CRTL!!!
  }

Proposed fix:

ftphandle *ctrl = static_cast<ftphandle*>(calloc(1,sizeof(ftphandle)));
 if (ctrl == NULL)
 {
   perror("calloc");
   net_close(sData);
   return -1;
 }
 if ((mode == 'A') && ((ctrl->buf = static_cast<char*>(malloc(FTPLIB_BUFSIZ))) == NULL))
 {
   perror("calloc");
   net_close(sData);
   free(ctrl);
   return -1;
 }

 if (!FtpSendCmd(cmd, '1', nControl))
 {
   FtpClose(*nData);
   *nData = NULL;
   free(ctrl);			// Fixed by JFO - potential memory leak.
   return -1;
 }

@dcrivelli
Copy link

FtpClose(*nData); cannot work as nData has not been assigned yet... you should better do like:

if (!FtpSendCmd(cmd, '1', nControl))
{
FtpClose(ctrl);
*nData = NULL;
return -1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants