[NOTEPAD] Fix and improve DoSaveFile (#5066)

- Use EM_GETHANDLE to get the text.
- Check zero size and handle it elegantly.
CORE-18832
This commit is contained in:
Katayama Hirofumi MZ 2023-02-13 08:32:09 +09:00 committed by GitHub
parent a050be0895
commit 97db8a258d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@
* Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch> * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
* Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr> * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
* Copyright 2002 Andriy Palamarchuk * Copyright 2002 Andriy Palamarchuk
* Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -332,48 +333,46 @@ GetPrintingRect(HDC hdc, RECT margins)
static BOOL DoSaveFile(VOID) static BOOL DoSaveFile(VOID)
{ {
BOOL bRet = TRUE; BOOL bRet = FALSE;
HANDLE hFile; HANDLE hFile;
LPTSTR pTemp; DWORD cchText;
DWORD size;
hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE, hFile = CreateFileW(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE) if (hFile == INVALID_HANDLE_VALUE)
{ {
ShowLastError(); ShowLastError();
return FALSE; return FALSE;
} }
size = GetWindowTextLength(Globals.hEdit) + 1; cchText = GetWindowTextLengthW(Globals.hEdit);
pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*pTemp)); if (cchText <= 0)
if (!pTemp)
{ {
CloseHandle(hFile); bRet = TRUE;
ShowLastError();
return FALSE;
} }
size = GetWindowText(Globals.hEdit, pTemp, size); else
if (size)
{ {
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 = WriteText(hFile, pszText, cchText, Globals.encFile, Globals.iEoln);
bRet = FALSE; if (!bRet)
ShowLastError();
LocalUnlock(hLocal);
} }
else else
{ {
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); ShowLastError();
bRet = TRUE;
} }
} }
CloseHandle(hFile); CloseHandle(hFile);
HeapFree(GetProcessHeap(), 0, pTemp);
if (bRet) if (bRet)
{ {
SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
SetFileName(Globals.szFileName); SetFileName(Globals.szFileName);
} }