fix(fileio): replace characters over INT_MAX with U+FFFD (#16354)

fixes #11877
credit: @zubairabid https://github.com/neovim/neovim/pull/12010
This commit is contained in:
Javier Lopez 2021-11-23 23:07:47 -05:00 committed by GitHub
parent dd8a4e2c22
commit a2e5c2f7c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -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;

View File

@ -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)