Software /
code /
prosody
Comparison
util/format.lua @ 12031:87bc26f23d9b
util.format: Escape invalid UTF-8 by passing trough serialization
Should prevent invalid UTF-8 from making it into the logs, which can
cause trouble with terminals or log viewers or other tools, such as when
grep determines that log files are binary.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 10 Dec 2021 22:48:45 +0100 |
parent | 11648:96d3cbeb9275 |
child | 12032:3db09eb4c43b |
comparison
equal
deleted
inserted
replaced
12030:9f8206e99b89 | 12031:87bc26f23d9b |
---|---|
3 -- | 3 -- |
4 | 4 |
5 local tostring = tostring; | 5 local tostring = tostring; |
6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack | 6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack |
7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+ | 7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+ |
8 local valid_utf8 = require "util.encodings".utf8.valid; | |
8 local type = type; | 9 local type = type; |
9 local dump = require "util.serialization".new("debug"); | 10 local dump = require "util.serialization".new("debug"); |
10 local num_type = math.type or function (n) | 11 local num_type = math.type or function (n) |
11 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; | 12 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
12 end | 13 end |
58 spec = "(%s)"; | 59 spec = "(%s)"; |
59 elseif option == "q" then | 60 elseif option == "q" then |
60 args[i] = dump(arg); | 61 args[i] = dump(arg); |
61 spec = "%s"; | 62 spec = "%s"; |
62 elseif option == "s" then | 63 elseif option == "s" then |
63 args[i] = tostring(arg):gsub("[%z\1-\8\11-\31\127]", control_symbols):gsub("\n\t?", "\n\t"); | 64 arg = tostring(arg); |
65 if arg:find("[\128-\255]") and not valid_utf8(arg) then | |
66 args[i] = dump(arg); | |
67 else | |
68 args[i] = arg:gsub("[%z\1-\8\11-\31\127]", control_symbols):gsub("\n\t?", "\n\t"); | |
69 end | |
64 elseif type(arg) ~= "number" then -- arg isn't number as expected? | 70 elseif type(arg) ~= "number" then -- arg isn't number as expected? |
65 args[i] = tostring(arg); | 71 args[i] = tostring(arg); |
66 spec = "[%s]"; | 72 spec = "[%s]"; |
73 option = "s"; | |
74 spec = "[%s]"; | |
75 t = "string"; | |
67 elseif expects_integer[option] and num_type(arg) ~= "integer" then | 76 elseif expects_integer[option] and num_type(arg) ~= "integer" then |
68 args[i] = tostring(arg); | 77 args[i] = tostring(arg); |
69 spec = "[%s]"; | 78 spec = "[%s]"; |
70 end | 79 end |
71 end | 80 end |