More fixes discovered based on Deedra's work.

This commit is contained in:
Storm Dragon committed 2026-10-23 16:08:58 -04:00
1 parent de31e5b513
commit 17052c5147
22 files changed
+561 -76

No files matched your search

+75 -15
View File
@@ -19,6 +19,7 @@ typedef struct EditState {
BOOL history_key, history_replaced;
WCHAR pending_high;
DWORD pending_caret;
DWORD capacity;
} EditState;
static void selection(HWND window, DWORD *start, DWORD *end) {
@@ -37,22 +38,61 @@ static LRESULT query_text(HWND window, UINT message, WPARAM wp, LPARAM lp) {
}
static void snapshot(HWND window, EditState *state) {
LRESULT length = query_text(window, WM_GETTEXTLENGTH, 0, 0);
state->length = length >= 0 && (ULONGLONG)length <= MAXDWORD ? (DWORD)length : 0;
if (length < 0 || (ULONGLONG)length > MAX_SNAPSHOT_UNITS) goto discard;
if (state->capacity <= state->length) {
DWORD capacity = state->length + 1;
// Grow geometrically rather than allocating another transcript on every
// appended line. Never retain more than the original snapshot budget.
if (capacity < 4096) capacity = 4096;
if (capacity < state->capacity * 2) capacity = state->capacity * 2;
if (capacity > MAX_SNAPSHOT_UNITS + 1) capacity = MAX_SNAPSHOT_UNITS + 1;
WCHAR *text = realloc(state->text, (size_t)capacity * sizeof(WCHAR));
if (!text) goto discard;
state->text = text;
state->capacity = capacity;
}
LRESULT copied = query_text(window, WM_GETTEXT, state->length + 1, (LPARAM)state->text);
if (copied < 0 || (ULONGLONG)copied > state->length) goto discard;
state->length = copied;
state->text[state->length] = 0;
return;
discard:
free(state->text);
state->text = NULL;
state->length = query_text(window, WM_GETTEXTLENGTH, 0, 0);
if (state->length <= MAX_SNAPSHOT_UNITS) {
state->text = calloc((size_t)state->length + 1, sizeof(WCHAR));
if (state->text) {
LRESULT copied = query_text(window, WM_GETTEXT, state->length + 1, (LPARAM)state->text);
if (copied >= 0 && (ULONGLONG)copied <= state->length) {
state->length = copied;
} else {
// A failed query must never become a length used to index this buffer.
free(state->text);
state->text = NULL;
}
}
state->capacity = 0;
}
static LRESULT read_review_text(HWND window, EditState *state, WineAccessEditText *request) {
if (!request || !request->capacity || request->capacity > MAX_SNAPSHOT_UNITS + 1 ||
(GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD)) return 0;
state->busy = TRUE;
LRESULT length = query_text(window, WM_GETTEXTLENGTH, 0, 0);
LRESULT copied = -1;
if (!state->destroyed && !(GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD) && length >= 0) {
if ((ULONGLONG)length >= request->capacity) request->status = 2;
else copied = query_text(window, WM_GETTEXT, request->capacity, (LPARAM)request->text);
}
if (state->destroyed) {
free(state->text); free(state);
return 0;
}
BOOL password = (GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD) != 0;
if (password) {
// A provider can enable protection during the busy text query, bypassing
// handle_edit's ordinary password-change cleanup.
free(state->text); state->text = NULL; state->capacity = 0;
state->known = FALSE; state->pending_high = 0;
}
if (copied == length && copied >= 0 && (ULONGLONG)copied < request->capacity &&
!password) {
request->copied = copied;
request->text[copied] = 0;
request->status = 1;
}
state->busy = FALSE;
return request->status;
}
static void copy_range(WCHAR *output, const WCHAR *text, DWORD start, DWORD end) {
@@ -126,6 +166,20 @@ static __attribute__((noinline)) LRESULT handle_edit(HWND window, UINT message,
state->history_key = state->history_replaced = FALSE;
BOOL history_replacement = message == WM_SETTEXT && state->history_key;
// Programmatic output updates have no editor action to announce. Rebuild
// lazily on navigation/typing/focus, not on every incoming transcript line.
// Pretranslated command-history replacements still need immediate feedback.
BOOL programmatic = message == EM_REPLACESEL || message == EM_SETHANDLE ||
message == EM_FMTLINES || (message == WM_SETTEXT && !history_replacement);
if ((!focus && GetFocus() != window) || (changing && programmatic)) {
if (GetFocus() != window) {
free(state->text); state->text = NULL; state->capacity = 0;
}
state->known = FALSE;
state->pending_high = 0;
return DefSubclassProc(window, message, wp, lp);
}
state->busy = TRUE;
WCHAR pending_high = state->pending_high;
DWORD pending_caret = state->pending_caret;
@@ -159,7 +213,7 @@ static __attribute__((noinline)) LRESULT handle_edit(HWND window, UINT message,
selection(window, &start, &end);
password = (GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD) != 0;
if (password) {
free(state->text); state->text = NULL; state->known = FALSE;
free(state->text); state->text = NULL; state->capacity = 0; state->known = FALSE;
if (focus) notify_text(window, WINE_ACCESS_TEXT_PASSWORD, L"", start, end);
state->busy = FALSE;
return result;
@@ -265,11 +319,14 @@ static LRESULT CALLBACK edit_proc(HWND window, UINT message, WPARAM wp, LPARAM l
if (message == WM_NCDESTROY) {
RemoveWindowSubclass(window, edit_proc, id);
RemovePropW(window, EDIT_STATE_PROPERTY);
RemovePropW(window, EDIT_REVIEW_PROPERTY);
state->destroyed = TRUE;
if (!state->busy) { free(state->text); free(state); }
return DefSubclassProc(window, message, wp, lp);
}
if (state->busy) return DefSubclassProc(window, message, wp, lp);
if (message == RegisterWindowMessageW(EDIT_REVIEW_MESSAGE))
return read_review_text(window, state, (WineAccessEditText *)lp);
if (message == WM_KILLFOCUS)
state->history_key = state->history_replaced = FALSE;
BOOL relevant = FALSE;
@@ -313,7 +370,10 @@ static BOOL watch_edit(HWND window, EditState **edit_state) {
if (!SetWindowSubclass(window, edit_proc, EDIT_SUBCLASS_ID, (DWORD_PTR)state)) {
free(state); return FALSE;
}
if (!SetPropW(window, EDIT_STATE_PROPERTY, state)) {
if (!SetPropW(window, EDIT_STATE_PROPERTY, state) ||
!SetPropW(window, EDIT_REVIEW_PROPERTY, (HANDLE)1)) {
RemovePropW(window, EDIT_STATE_PROPERTY);
RemovePropW(window, EDIT_REVIEW_PROPERTY);
RemoveWindowSubclass(window, edit_proc, EDIT_SUBCLASS_ID);
free(state); return FALSE;
}