Re: [hackers] [st][PATCH] fix: realloc kpress buffer as needed.

From: Storkman <storkman_AT_storkman.nl>
Date: Tue, 13 May 2025 17:26:35 +0200

On Tue, May 13, 2025 at 10:56:23AM +0000, NRK wrote:
> On Mon, May 12, 2025 at 01:53:08AM +0900, yahei wrote:
> > If XBufferOverflow occuers, the client should recall the function with
> > the same event and a buffer of adequate size to obtain the string.
>

I don't get the original message, so from the ML archive...

        + len = XmbLookupString(xw.ime.xic, e, buf, bufsize, &ksym, &status);
        + if (status == XBufferOverflow) {
        + bufsize = len;
        + buf = xrealloc(buf, bufsize * sizeof(char));

POSIX char is 1 byte. Also, from the man page:
        XmbLookupString and Xutf8LookupString return the length of the string in bytes.

By the way, what's the reason it's Xmb and not Xutf8?
I vaguely remember this being discussed, but I can't find it in the archives...

> Do you have any actual usecase where the current buffer size is a
> problem? If yes, I think it's better to increase the buffer size to
> something more reasonable if possible than to mess around with static
> variables.
>
> - NRK
>

The current behavior of st is just incorrect in the corner case.
I think somebody assumed that ignoring the KeyEvent will discard the
over-long input, but that's not how it works.

There's nothing stopping you from typing an entire paragraph of text
into the IM pre-edit window, and then st will just silently ignore any
IM input until you kill and re-start either the IM or st itself. There's
no indication to the user that this needs to be done, other than... well,
not working.

Funny things also happen when you have a Compose sequence longer than
255 bytes, but it seems that something more is going on there.

Since this is something that's going to happen rarely (maybe less than
once per second, maybe once per st instance, maybe never), I'd prefer
allocating a larger buffer when needed and freeing it immediately.
Patch attached.

If you're against allocating memory on principle, It'd at least be nice to
call XCloseIM(), XOpenIM(), so that the buffered input gets dropped instead
of getting clogged indefinitely.

-- 
Storkman
Received on Tue May 13 2025 - 17:26:35 CEST

This archive was generated by hypermail 2.3.0 : Tue May 13 2025 - 17:36:38 CEST