Skip to content

Commit efa9d0b

Browse files
authored
Merge pull request #67 from csware/defensive
More defensive programming
2 parents 8989f81 + 1cf36d3 commit efa9d0b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/PluginDefinition.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,22 @@ void pluginCleanUp()
5858
//
5959
static bool parseConfig(editorconfig_handle eh)
6060
{
61-
TCHAR fileNamet[MAX_PATH];
62-
char fileName[MAX_PATH];
63-
61+
WCHAR wcFileName[MAX_PATH];
6462
// get the file name
65-
::SendMessage(nppData._nppHandle, NPPM_GETFULLCURRENTPATH, MAX_PATH, (LPARAM)fileNamet);
66-
wcstombs(fileName, fileNamet, MAX_PATH);
63+
if (::SendMessage(nppData._nppHandle, NPPM_GETFULLCURRENTPATH, sizeof(wcFileName), reinterpret_cast<LPARAM>(wcFileName)) == FALSE)
64+
return false;
65+
const size_t bytes_needed = wcstombs(nullptr, wcFileName, 0);
66+
if (bytes_needed == static_cast<size_t>(-1))
67+
return false;
68+
std::unique_ptr<char[]> fileName = std::unique_ptr<char[]>(new (std::nothrow) char[bytes_needed + 1]);
69+
if (!fileName)
70+
return false;
71+
if (wcstombs(fileName.get(), wcFileName, bytes_needed) == static_cast<size_t>(-1))
72+
return false;
6773

6874
// start parsing
6975
int err_num;
70-
if ((err_num = editorconfig_parse(fileName, eh)) != 0 &&
76+
if ((err_num = editorconfig_parse(fileName.get(), eh)) != 0 &&
7177
/* Ignore full path error, whose error code is
7278
* EDITORCONFIG_PARSE_NOT_FULL_PATH */
7379
err_num != EDITORCONFIG_PARSE_NOT_FULL_PATH) {
@@ -113,7 +119,7 @@ void loadConfig()
113119

114120
// Parse the EditorConfig file
115121
eh = editorconfig_handle_init();
116-
if (!parseConfig(eh))
122+
if (!eh || !parseConfig(eh))
117123
return;
118124

119125
// Get the current scintilla
@@ -280,7 +286,7 @@ void onBeforeSave(HWND hWnd, uptr_t idFrom)
280286

281287
editorconfig_handle eh = editorconfig_handle_init();
282288

283-
if (!parseConfig(eh))
289+
if (!eh || !parseConfig(eh))
284290
{
285291
selectBufferId(hWnd, keepBufferID);
286292
return;
@@ -394,7 +400,7 @@ void showEditorConfigSettings()
394400

395401
// Parse the EditorConfig file
396402
eh = editorconfig_handle_init();
397-
if (!parseConfig(eh))
403+
if (!eh || !parseConfig(eh))
398404
return;
399405

400406
std::tstringstream settings;

0 commit comments

Comments
 (0)