Skip to content

Commit

Permalink
fix two cases of truncated lines causing cashes and don't drop the fi…
Browse files Browse the repository at this point in the history
…rst 1K chars of the name field when larger than one buffer
  • Loading branch information
cfillion committed Feb 4, 2024
1 parent 9700013 commit f009490
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions SnM/SnM_Notes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ MediaTrack* g_trNote = NULL;
// to distinguish internal marker/region updates from external ones
bool g_internalMkrRgnChange = false;

static bool DoImportSubTitleFile(const char *fn);

///////////////////////////////////////////////////////////////////////////////
// NotesWnd
Expand Down Expand Up @@ -486,6 +487,19 @@ void NotesWnd::OnTimer(WPARAM wParam)
}
}

void NotesWnd::OnDroppedFiles(HDROP _h)
{
if (g_locked || !(g_notesType == SNM_NOTES_MKR_SUB || g_notesType == SNM_NOTES_RGN_SUB || g_notesType == SNM_NOTES_MKRRGN_SUB))
return;
const int iFiles = DragQueryFile(_h, 0xFFFFFFFF, NULL, 0);
char fn[SNM_MAX_PATH];
for (int i = 0; i < iFiles; i++)
{
if (DragQueryFile(_h, i, fn, sizeof(fn)) && !DoImportSubTitleFile(fn))
break;
}
}

void NotesWnd::OnResize()
{
if (g_notesType != g_prevNotesType)
Expand Down Expand Up @@ -1415,13 +1429,16 @@ bool ImportAdvancedSubStationFile(const char* _fn)
WDL_FastString notes;
if (*commaPos != ',') { // if Name field isn't empty
notes.Append("[");
char* nextCommaPos = strchr(commaPos, ',');
while (!nextCommaPos && !strchr(buf, '\n') && fgets(buf, sizeof(buf), f) && *buf) { // in case comma
char* nextCommaPos;
while (!(nextCommaPos = strchr(commaPos, ',')) && !strchr(buf, '\n')) {
notes.Append(commaPos);
commaPos = buf;
nextCommaPos = strchr(buf, ',');
if (!fgets(buf, sizeof(buf), f) || !*buf)
break;
}
char nameBuf[1024];
if (!nextCommaPos)
break;
char nameBuf[sizeof(buf)];
memcpy(nameBuf, commaPos, nextCommaPos-commaPos);
nameBuf[nextCommaPos-commaPos] = '\0';
notes.Append(nameBuf);
Expand All @@ -1442,9 +1459,8 @@ bool ImportAdvancedSubStationFile(const char* _fn)
}
commaCount++;
}
if (commaCount != 9) {
if (commaCount != 9 || !commaPos)
break;
}
char *textPos = commaPos + 1;

char text[1024];
Expand Down Expand Up @@ -1547,7 +1563,7 @@ bool ImportAdvancedSubStationFile(const char* _fn)
return ok;
}

static bool ImportSubTitleFile(const char *fn)
bool DoImportSubTitleFile(const char *fn)
{
constexpr struct { const char *ext; bool (*reader)(const char *); } formats[] {
{ "srt", &ImportSubRipFile },
Expand Down Expand Up @@ -1578,7 +1594,7 @@ void ImportSubTitleFile(COMMAND_T* _ct)
if (char* fn = BrowseForFiles(__LOCALIZE("S&M - Import subtitle file","sws_DLG_152"), g_lastImportSubFn, NULL, false, SNM_SUB_IMPORT_EXT_LIST))
{
lstrcpyn(g_lastImportSubFn, fn, sizeof(g_lastImportSubFn));
ImportSubTitleFile(fn);
DoImportSubTitleFile(fn);
free(fn);
}
}
Expand Down Expand Up @@ -2020,15 +2036,3 @@ void NF_DoUpdateSWSMarkerRegionSubWindow()
w->ForceUpdateMkrRgnNameOrSub(g_notesType);
}
}

void NotesWnd::OnDroppedFiles(HDROP _h) {
if (g_locked || !(g_notesType == SNM_NOTES_MKR_SUB || g_notesType == SNM_NOTES_RGN_SUB || g_notesType == SNM_NOTES_MKRRGN_SUB))
return;
const int iFiles = DragQueryFile(_h, 0xFFFFFFFF, NULL, 0);
char fn[SNM_MAX_PATH];
for (int i = 0; i < iFiles; i++)
{
if (DragQueryFile(_h, i, fn, sizeof(fn)) && !ImportSubTitleFile(fn))
break;
}
}

0 comments on commit f009490

Please sign in to comment.