]> git.ipfire.org Git - thirdparty/vim.git/commit
patch 9.1.1623: Buffer menu does not handle unicode names correctly v9.1.1623
authorYee Cheng Chin <ychin.git@gmail.com>
Sun, 10 Aug 2025 08:06:14 +0000 (10:06 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 10 Aug 2025 08:09:38 +0000 (10:09 +0200)
commit8f9de4991e84dfdc9bcc9dad0eaa2b3544ef963e
treeeb10b71437d1eaf787692cf34261eaad02dc2c03
parentcda0d17f5937c9e09d6821da64e57d589fd3c404
patch 9.1.1623: Buffer menu does not handle unicode names correctly

Problem:  Buffer menu does not handle unicode names correctly
          (after v9.1.1622)
Solution: Fix the BMHash() function (Yee Cheng Chin)

The Buffers menu uses a BMHash() function to generate a sortable number
to be used for the menu index. It used a naive (and incorrect) way of
encoding multiple ASCII values into a single integer, but assumes each
character to be only in the ASCII 32-96 range. This means if we use
non-ASCII file names (e.g. Unicode values like CJK or emojis) we get
integer underflow and overflow, causing the menu index to wrap around.
Vim's GUI implementations internally use a signed 32-bit integer for the
`gui_mch_add_menu_item()` function and so we need to make sure the menu
index is in the (0, 2^31-1) range.

To do this, if the file name starts with a non-ASCII value, we just use
the first character's value and set the high bit so it sorts after the
other ASCII ones. Otherwise, we just take the first 5 characters, and
use 5 bit for each character to encode a 30-bit number that can be
sorted.

This means Unicode file names won't be sorted beyond the first
character. This is likely going to be fine as there are lots of ways to
query buffers.

related: #17403
closes: #17928

Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/menu.vim
src/testdir/test_gui.vim
src/version.c