Skip to content

Commit

Permalink
Fix MP3 finding stack crash
Browse files Browse the repository at this point in the history
  • Loading branch information
erorcun committed Jun 28, 2021
1 parent cb3b385 commit 22e8e0e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 99 deletions.
145 changes: 47 additions & 98 deletions src/audio/sampman_oal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,32 @@ _FindMP3s(void)
bool8 bInitFirstEntry;
HANDLE hFind;
char path[MAX_PATH];
char filepath[MAX_PATH*2];
int total_ms;
WIN32_FIND_DATA fd;
char filepath[MAX_PATH + sizeof(fd.cFileName)];

if (getcwd(_mp3DirectoryPath, MAX_PATH) == NULL) {
perror("getcwd: ");
return;
}

if (strlen(_mp3DirectoryPath) + 1 > MAX_PATH - 10) {
// This is not gonna end well
printf("MP3 folder path is too long, no place left for file names. MP3 finding aborted.\n");
return;
}

OutputDebugString("Finding MP3s...");
strcpy(path, _mp3DirectoryPath);
strcat(path, "\\MP3\\");

#if !defined(_WIN32)
char *actualPath = casepath(path);
if (actualPath) {
strcpy(path, actualPath);
free(actualPath);
}
#endif

strcpy(_mp3DirectoryPath, path);
OutputDebugString(_mp3DirectoryPath);
Expand All @@ -470,95 +484,32 @@ _FindMP3s(void)
{
return;
}

strcpy(filepath, _mp3DirectoryPath);
strcat(filepath, fd.cFileName);

size_t filepathlen = strlen(filepath);

if ( filepathlen <= 0)
{
FindClose(hFind);
return;
}

if ( _ResolveLink(filepath, filepath) )
{
OutputDebugString("Resolving Link");
OutputDebugString(filepath);
bShortcut = TRUE;
} else
bShortcut = FALSE;

aStream[0] = new CStream(filepath, ALStreamSources[0], ALStreamBuffers[0]);
bShortcut = FALSE;
bInitFirstEntry = TRUE;

if (aStream[0] && aStream[0]->IsOpened())
{
total_ms = aStream[0]->GetLengthMS();
delete aStream[0];
aStream[0] = NULL;

OutputDebugString(fd.cFileName);

_pMP3List = new tMP3Entry;

if ( _pMP3List == NULL )
{
FindClose(hFind);
return;
}

nNumMP3s = 1;

strcpy(_pMP3List->aFilename, fd.cFileName);

_pMP3List->nTrackLength = total_ms;

_pMP3List->pNext = NULL;

pList = _pMP3List;

if ( bShortcut )
{
_pMP3List->pLinkPath = new char[MAX_PATH*2];
strcpy(_pMP3List->pLinkPath, filepath);
}
else
{
_pMP3List->pLinkPath = NULL;
}
do
{
strcpy(filepath, _mp3DirectoryPath);
strcat(filepath, fd.cFileName);

if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
continue;

bInitFirstEntry = FALSE;
}
else
{
strcat(filepath, " - NOT A VALID MP3");

OutputDebugString(filepath);
size_t filepathlen = strlen(filepath);

bInitFirstEntry = TRUE;
}

while ( TRUE )
{
if ( !FindNextFile(hFind, &fd) )
break;

if ( bInitFirstEntry )
{
strcpy(filepath, _mp3DirectoryPath);
strcat(filepath, fd.cFileName);

size_t filepathlen = strlen(filepath);

if ( filepathlen > 0 )
if (filepathlen > 0)
{
if ( _ResolveLink(filepath, filepath) )
if (_ResolveLink(filepath, filepath))
{
OutputDebugString("Resolving Link");
OutputDebugString(filepath);
bShortcut = TRUE;
} else {
}
else
{
bShortcut = FALSE;
if (filepathlen > MAX_PATH) {
continue;
Expand All @@ -571,31 +522,31 @@ _FindMP3s(void)
total_ms = aStream[0]->GetLengthMS();
delete aStream[0];
aStream[0] = NULL;

OutputDebugString(fd.cFileName);

_pMP3List = new tMP3Entry;
if ( _pMP3List == NULL)

if (_pMP3List == NULL)
break;

nNumMP3s = 1;

strcpy(_pMP3List->aFilename, fd.cFileName);

_pMP3List->nTrackLength = total_ms;
_pMP3List->pNext = NULL;
if ( bShortcut )

if (bShortcut)
{
_pMP3List->pLinkPath = new char [MAX_PATH*2];
_pMP3List->pLinkPath = new char[MAX_PATH + sizeof(fd.cFileName)];
strcpy(_pMP3List->pLinkPath, filepath);
}
else
{
_pMP3List->pLinkPath = NULL;
}

pList = _pMP3List;

bInitFirstEntry = FALSE;
Expand All @@ -606,22 +557,20 @@ _FindMP3s(void)
OutputDebugString(filepath);
}
}
else
break;
}
else
{
strcpy(filepath, _mp3DirectoryPath);
strcat(filepath, fd.cFileName);

size_t filepathlen = strlen(filepath);

if ( filepathlen > 0 )
{
if ( _ResolveLink(filepath, filepath) )
{
OutputDebugString("Resolving Link");
OutputDebugString(filepath);
bShortcut = TRUE;
} else
}
else
bShortcut = FALSE;

aStream[0] = new CStream(filepath, ALStreamSources[0], ALStreamBuffers[0]);
Expand All @@ -647,7 +596,7 @@ _FindMP3s(void)

if ( bShortcut )
{
e->pLinkPath = new char [MAX_PATH*2];
e->pLinkPath = new char [MAX_PATH + sizeof(fd.cFileName)];
strcpy(e->pLinkPath, filepath);
}
else
Expand All @@ -666,7 +615,7 @@ _FindMP3s(void)
}
}
}
}
} while (FindNextFile(hFind, &fd));

FindClose(hFind);
}
Expand Down
2 changes: 1 addition & 1 deletion src/skel/crossplatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ typedef void* HANDLE;

struct WIN32_FIND_DATA {
char extension[32]; // for searching
char folder[32]; // for searching
char folder[MAX_PATH]; // for searching
char cFileName[256]; // because tSkinInfo has it 256
time_t ftLastWriteTime;
};
Expand Down

0 comments on commit 22e8e0e

Please sign in to comment.