[WORDPAD] Use FILE_SHARE_READ and OPEN_ALWAYS for writing (#6883)
authorKatayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
Tue, 14 May 2024 16:23:30 +0000 (01:23 +0900)
committerGitHub <noreply@github.com>
Tue, 14 May 2024 16:23:30 +0000 (01:23 +0900)
Follow-up to #6880. Fix access denial on
writing file "C:\freeldr.ini".
JIRA issue: CORE-19575
- Add FILE_SHARE_READ flag in CreateFileW
  call.
- Use OPEN_ALWAYS instead of
  CREATE_ALWAYS, and then explicitly use
  SetEndOfFile function.

base/applications/wordpad/wordpad.c

index 1c0b820..85a9be9 100644 (file)
@@ -843,8 +843,15 @@ static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
     EDITSTREAM stream;
     LRESULT ret;
 
+#ifdef __REACTOS__
+    /* Use OPEN_ALWAYS instead of CREATE_ALWAYS in order to succeed
+     * even if the file has HIDDEN or SYSTEM attributes */
+    hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS,
+        FILE_ATTRIBUTE_NORMAL, NULL);
+#else
     hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
         FILE_ATTRIBUTE_NORMAL, NULL);
+#endif
 
     if(hFile == INVALID_HANDLE_VALUE)
     {
@@ -870,6 +877,10 @@ static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
 
     ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
 
+#ifdef __REACTOS__
+    /* Truncate the file and close it */
+    SetEndOfFile(hFile);
+#endif
     CloseHandle(hFile);
 
     SetFocus(hEditorWnd);