From 623507af3bc94184df7683cf3e9cdceb2f3410c8 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 3 Jul 2025 17:14:04 -0400 Subject: [PATCH] fix(prevent-crash-on-invalid-UTF-8-when-encoding-request-body): prevent crash on invalid UTF-8 when encoding request body --- lua/codeium/io.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lua/codeium/io.lua b/lua/codeium/io.lua index 9f447df..e775625 100644 --- a/lua/codeium/io.lua +++ b/lua/codeium/io.lua @@ -343,7 +343,7 @@ function M.gunzip(path, callback) vim.o.shell = "powershell" end vim.o.shellcmdflag = - "-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;" + "-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;" vim.o.shellredir = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode" vim.o.shellpipe = "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode" vim.o.shellquote = "" @@ -402,12 +402,18 @@ function M.post(url, params) local tmpfname = os.tmpname() if type(params.body) == "table" then - local f = io.open(tmpfname, 'w+') + local ok, json = pcall(vim.fn.json_encode, params.body) + if not ok then + log.error("Failed to encode Codeium payload (invalid UTF-8?)") + return + end + + local f = io.open(tmpfname, "w+") if f == nil then - log.error('Cannot open temporary message file: ' .. tmpfname) + log.error("Cannot open temporary message file: " .. tmpfname) return end - f:write(vim.fn.json_encode(params.body)) + f:write(json) f:close() params.headers = params.headers or {}