diff options
author | Javier Lopez <graulopezjavier@gmail.com> | 2021-11-23 23:07:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-23 21:07:47 -0700 |
commit | a2e5c2f7c8ec2b236a0c33cf9fc3cbab17c6cfcd (patch) | |
tree | 741a6d16b8439853e79256b4d475ec18d54b6c54 | |
parent | dd8a4e2c22ea8018ce3af989134b1e9c4607ce37 (diff) | |
download | rneovim-a2e5c2f7c8ec2b236a0c33cf9fc3cbab17c6cfcd.tar.gz rneovim-a2e5c2f7c8ec2b236a0c33cf9fc3cbab17c6cfcd.tar.bz2 rneovim-a2e5c2f7c8ec2b236a0c33cf9fc3cbab17c6cfcd.zip |
fix(fileio): replace characters over INT_MAX with U+FFFD (#16354)
fixes #11877
credit: @zubairabid https://github.com/neovim/neovim/pull/12010
-rw-r--r-- | src/nvim/fileio.c | 4 | ||||
-rw-r--r-- | test/functional/core/fileio_spec.lua | 9 |
2 files changed, 13 insertions, 0 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 7573064fa9..1b39a7410a 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1360,6 +1360,10 @@ retry: u8c += (unsigned)(*--p) << 16; u8c += (unsigned)(*--p) << 24; } + // Replace characters over INT_MAX with Unicode replacement character + if (u8c > INT_MAX) { + u8c = 0xfffd; + } } else { // UTF-8 if (*--p < 0x80) { u8c = *p; diff --git a/test/functional/core/fileio_spec.lua b/test/functional/core/fileio_spec.lua index f4c476560d..c68bc18eed 100644 --- a/test/functional/core/fileio_spec.lua +++ b/test/functional/core/fileio_spec.lua @@ -15,6 +15,7 @@ local read_file = helpers.read_file local trim = helpers.trim local currentdir = helpers.funcs.getcwd local iswin = helpers.iswin +local assert_alive = helpers.assert_alive describe('fileio', function() before_each(function() @@ -26,6 +27,7 @@ describe('fileio', function() os.remove('Xtest_startup_file1~') os.remove('Xtest_startup_file2') os.remove('Xtest_ัะตัั.md') + os.remove('Xtest-u8-int-max') rmdir('Xtest_startup_swapdir') rmdir('Xtest_backupdir') end) @@ -128,5 +130,12 @@ describe('fileio', function() table.insert(text, '') eq(text, funcs.readfile(fname, 'b')) end) + it('read invalid u8 over INT_MAX doesn\'t segfault', function() + clear() + command('call writefile(0zFFFFFFFF, "Xtest-u8-int-max")') + -- This should not segfault + command('edit ++enc=utf32 Xtest-u8-int-max') + assert_alive() + end) end) |