Open Fojtik opened 7 years ago
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; }
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; }
Original code:
Proposed fix: