ConfettiFX / The-Forge

The Forge Cross-Platform Rendering Framework PC Windows, Steamdeck (native), Ray Tracing, macOS / iOS, Android, XBOX, PS4, PS5, Switch, Quest 2
Apache License 2.0
4.63k stars 487 forks source link

Possible issue in fsFetchZipEntryName function #291

Closed ilyachastikov closed 3 months ago

ilyachastikov commented 6 months ago

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;
}
wolfgangfengel commented 4 months ago

I hope this is fixed with the latest release. Can you confirm?