https://git.reactos.org/?p=reactos.git;a=commitdiff;h=97db8a258dc49de5bf8cb…
commit 97db8a258dc49de5bf8cba8c432a9d37be5eacc0
Author: Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
AuthorDate: Mon Feb 13 08:32:09 2023 +0900
Commit: GitHub <noreply(a)github.com>
CommitDate: Mon Feb 13 08:32:09 2023 +0900
[NOTEPAD] Fix and improve DoSaveFile (#5066)
- Use EM_GETHANDLE to get the text.
- Check zero size and handle it elegantly.
CORE-18832
---
base/applications/notepad/dialog.c | 41 +++++++++++++++++++-------------------
1 file changed, 20 insertions(+), 21 deletions(-)
diff --git a/base/applications/notepad/dialog.c b/base/applications/notepad/dialog.c
index 06618147121..8233082be1b 100644
--- a/base/applications/notepad/dialog.c
+++ b/base/applications/notepad/dialog.c
@@ -4,6 +4,7 @@
* Copyright 1998,99 Marcel Baur <mbaur(a)g26.ethz.ch>
* Copyright 2002 Sylvain Petreolle <spetreolle(a)yahoo.fr>
* Copyright 2002 Andriy Palamarchuk
+ * Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -332,48 +333,46 @@ GetPrintingRect(HDC hdc, RECT margins)
static BOOL DoSaveFile(VOID)
{
- BOOL bRet = TRUE;
+ BOOL bRet = FALSE;
HANDLE hFile;
- LPTSTR pTemp;
- DWORD size;
+ DWORD cchText;
- hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if(hFile == INVALID_HANDLE_VALUE)
+ hFile = CreateFileW(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
+ NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (hFile == INVALID_HANDLE_VALUE)
{
ShowLastError();
return FALSE;
}
- size = GetWindowTextLength(Globals.hEdit) + 1;
- pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*pTemp));
- if (!pTemp)
+ cchText = GetWindowTextLengthW(Globals.hEdit);
+ if (cchText <= 0)
{
- CloseHandle(hFile);
- ShowLastError();
- return FALSE;
+ bRet = TRUE;
}
- size = GetWindowText(Globals.hEdit, pTemp, size);
-
- if (size)
+ else
{
- if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.encFile, Globals.iEoln))
+ HLOCAL hLocal = (HLOCAL)SendMessageW(Globals.hEdit, EM_GETHANDLE, 0, 0);
+ LPWSTR pszText = LocalLock(hLocal);
+ if (pszText)
{
- ShowLastError();
- bRet = FALSE;
+ bRet = WriteText(hFile, pszText, cchText, Globals.encFile, Globals.iEoln);
+ if (!bRet)
+ ShowLastError();
+
+ LocalUnlock(hLocal);
}
else
{
- SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
- bRet = TRUE;
+ ShowLastError();
}
}
CloseHandle(hFile);
- HeapFree(GetProcessHeap(), 0, pTemp);
if (bRet)
{
+ SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
SetFileName(Globals.szFileName);
}