The-Forge icon indicating copy to clipboard operation
The-Forge copied to clipboard

Possible issue in fsFetchZipEntryName function

Open ilyachastikov opened this issue 1 year ago • 0 comments

As I understood typical pattern of using fsFetchZipEntryName function is to send NULL as pBuffer parameter to get size of it and after this a second time invoke to get name of entry. For example:

char* pcName = NULL;
size_t fileNameSize = 0;
if (fsFetchZipEntryName(&gZipReadFileSystem, 0, NULL, &fileNameSize, fileNameSize)) {
     pcName = (char*)tf_malloc(fileNameSize + 1);
     fsFetchZipEntryName(&gZipReadFileSystem, 0, pcName, NULL, fileNameSize);
}

But in this case function will fail. May be I wrong but there is no example of using it in ZipFileSystem unit test. I think that fixed version of this function should be:

bool fsFetchZipEntryName(IFileSystem* pIO, uint64_t index, char* pBuffer, size_t* pSize, size_t bufferSize)
{
	ASSERT(pIO && pIO->pUser);
	// Only one must be 0
	ASSERT(!pBuffer != !pSize);
	if (!isZipIO(pIO))
		return false;

	// make sure that zip file is opened
	if (!fsOpenZipFile(pIO))
	{
		LOGF(eERROR, "Failed to open zip file, while trying to fetch zip entry filename.");
		return false;
	}

	ZipFile* pZipFile = (ZipFile*)pIO->pUser;
	void* zip = pZipFile->pHandle;

	bool noerr = mz_zip_goto_entry(zip, index);
	mz_zip_file* pFileInfo = NULL;
	if (noerr)
		noerr = mz_zip_entry_get_info(zip, &pFileInfo);
	
	if (noerr && pFileInfo)
	{
		if (pBuffer)
		{
			size_t sizeToWrite = pFileInfo->filename_size < bufferSize ? pFileInfo->filename_size : bufferSize - 1;
			memcpy(pBuffer, pFileInfo->filename, sizeToWrite);
			pBuffer[pFileInfo->filename_size] = '\0';
		}
		else
		{
			*pSize = pFileInfo->filename_size;
		}		
	}

	if (!fsCloseZipFile(pIO))
	{
		LOGF(eERROR, "Failed to close zip file, while trying to fetch zip entry filename.");
		return false;
	}
	return noerr;
}

ilyachastikov avatar Dec 26 '23 06:12 ilyachastikov