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

Defect in ftplib::FtpXfer #12

Open
Fojtik opened this issue Dec 1, 2016 · 0 comments
Open

Defect in ftplib::FtpXfer #12

Fojtik opened this issue Dec 1, 2016 · 0 comments

Comments

@Fojtik
Copy link

Fojtik commented Dec 1, 2016

I have found critical defect inside ftplib::FtpXfer function.

The rv variable is signalised to 0 when 'short write' occurs. But the signal is not used in the further code. It means that file written is cut and user is not notified about it.

Anyway, rv variable is only assigned a value that is never used.

I propose following fix at the end of the function:
------------------CURRENT---------------

	free(dbuf);
	fflush(local);
	if (localfile != NULL) fclose(local);
	return FtpClose(nData);
}

-----------------FIXED-----------------------

	free(dbuf);
	fflush(local);
	if (localfile != NULL) fclose(local);
        if(rv) return FtpClose(nData);
        FtpClose(nData);
       return 0;
}

Any other idea?


/*
 * FtpXfer - issue a command and transfer data
 *
 * return 1 if successful, 0 otherwise
 */
int ftplib::FtpXfer(const char *localfile, const char *path, ftphandle *nControl, accesstype type, transfermode mode)
{
	int l,c;
	char *dbuf;
	FILE *local = NULL;
	ftphandle *nData;
int rv=1; // 3.1-1

	if (localfile != NULL)
	{
		// printf("localfile: -%s-", localfile);
	
		//local = fopen(localfile, (typ == ftplib::filewrite) ? "r" : "w");
		char ac[3] = "  ";
		if ((type == ftplib::dir) || (type == ftplib::dirverbose)) { ac[0] = 'w'; ac[1] = '\0'; }
		if (type == ftplib::fileread) { ac[0] = 'w'; ac[1] = '\0'; }
		if (type == ftplib::filewriteappend) { ac[0] = 'r'; ac[1] = '\0'; }
		if (type == ftplib::filereadappend) { ac[0] = 'a'; ac[1] = '\0'; }
		if (type == ftplib::filewrite) { ac[0] = 'r'; ac[1] = '\0'; }
		if (mode == ftplib::image) ac[1] = 'b';

#ifndef NOLFS
		local = fopen64(localfile, ac);
		if (type == ftplib::filewriteappend) fseeko64(local,mp_ftphandle->offset,SEEK_SET);
#else
		local = fopen(localfile, ac);
		if (type == ftplib::filewriteappend) fseek(local,mp_ftphandle->offset,SEEK_SET);	
#endif
		if (local == NULL)
		{
			strncpy(nControl->response, strerror(errno), sizeof(nControl->response));
			return 0;
		}
	}
	if (local == NULL) local = ((type == ftplib::filewrite)
		|| (type == ftplib::filewriteappend)) ? stdin : stdout;
	if (!FtpAccess(path, type, mode, nControl, &nData)) return 0;

	dbuf = static_cast<char*>(malloc(FTPLIB_BUFSIZ));
	if ((type == ftplib::filewrite) || (type == ftplib::filewriteappend))
	{
		while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
		{
			if ((c = FtpWrite(dbuf, l, nData)) < l)
			{
				printf("short write: passed %d, wrote %d\n", l, c);
				rv = 0;
				break;
			}
		}
	}
	else
	{
		while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
		{
			if (fwrite(dbuf, 1, l, local) <= 0)
			{
				perror("localfile write");
				break;
			}
		}
	}
	free(dbuf);
	fflush(local);
	if (localfile != NULL) fclose(local);
	return FtpClose(nData);
}
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

1 participant