[PATCH v2 4/5] Cygwin: pty: Prevent pty from changing code page of parent console.
Corinna Vinschen
corinna-cygwin@cygwin.com
Mon Jan 18 12:39:01 GMT 2021
Hi Takashi,
On Jan 18 20:24, Takashi Yano via Cygwin-patches wrote:
> @@ -59,6 +59,46 @@ struct pipe_reply {
> DWORD error;
> };
>
> +extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
> +
> +static DWORD
> +get_console_process_id (DWORD pid, bool match)
> +{
> + DWORD list1;
> + DWORD num, num_req;
> + num = 1;
> + num_req = GetConsoleProcessList (&list1, num);
> + DWORD *list;
> + if (num_req == 1)
> + list = &list1;
> + else
> + while (true)
> + {
> + list = (DWORD *)
> + HeapAlloc (GetProcessHeap (), 0, num_req * sizeof (DWORD));
> + num = num_req;
> + num_req = GetConsoleProcessList (list, num);
> + if (num_req > num)
> + HeapFree (GetProcessHeap (), 0, list);
> + else
> + break;
> + }
> + num = num_req;
> +
> + DWORD res = 0;
> + /* Last one is the oldest. */
> + /* https://github.com/microsoft/terminal/issues/95 */
> + for (int i = (int) num - 1; i >= 0; i--)
> + if ((match && list[i] == pid) || (!match && list[i] != pid))
> + {
> + res = list[i];
> + break;
> + }
> + if (num > 1)
> + HeapFree (GetProcessHeap (), 0, list);
> + return res;
> +}
Sorry if I'm slow, but I was just mulling over this code snippet again,
and I was wondering if we couldn't do without the HeapAlloc loop.
Assuming you use a tmp_pathbuf here, you'd have space for 16384
processes per console. Shouldn't that be more than enough? I.e.
static DWORD
get_console_process_id (DWORD pid, bool match)
{
tmp_pathbuf tp;
DWORD *list = (DWORD *) tp.w_get ();
const DWORD num = NT_MAX_PATH * sizeof (WCHAR) / sizeof (DWORD);
DWORD res = 0;
num = GetConsoleProcessList (&list, num);
/* Last one is the oldest. */
/* https://github.com/microsoft/terminal/issues/95 */
for (int i = (int) num - 1; i >= 0; i--)
if ((match && list[i] == pid) || (!match && list[i] != pid))
{
res = list[i];
break;
}
return res;
}
What do you think?
Corinna
More information about the Cygwin-patches
mailing list