|
| 1 | +--[[ |
| 2 | +SPDX-License-Identifier: ISC |
| 3 | +Copyright (c) 2023-2025, Sergey Bronnikov. |
| 4 | +
|
| 5 | +6.5 – UTF-8 Support |
| 6 | +https://www.lua.org/manual/5.3/manual.html#6.5 |
| 7 | +
|
| 8 | +'utf8.codes' does not raise an error on spurious continuation bytes, |
| 9 | +https://github.com/lua/lua/commit/a1089b415a3f5c753aa1b40758ffdaf28d5701b0 |
| 10 | +
|
| 11 | +Synopsis: utf8.char(...) |
| 12 | +]] |
| 13 | + |
| 14 | +-- TODO: https://github.com/tst2005/lua-utf8string |
| 15 | +-- - https://www.lua.org/manual/5.3/manual.html#6.5 |
| 16 | +-- - Numbers taken from table 3-7 in |
| 17 | +-- - www.unicode.org/versions/Unicode6.2.0/UnicodeStandard-6.2.pdf |
| 18 | +-- - Find-based solution inspired by |
| 19 | +-- - http://notebook.kulchenko.com/programming/fixing-malformed-utf8-in-lua |
| 20 | +-- - check utf8 https://www.cl.cam.ac.uk/~mgk25/ucs/utf8_check.c |
| 21 | +-- - corpus: https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt |
| 22 | +-- - https://git.neulandlabor.de/j3d1/json/commit/15b6421d0789a5402275358d43719f4b37979929 |
| 23 | +-- - https://github.com/geoffmcl/utf8-test/tree/master/src |
| 24 | +-- - https://github.com/kikito/utf8_validator.lua/blob/master/utf8_validator.lua |
| 25 | +-- - https://en.wikipedia.org/wiki/UTF-8#Invalid_sequences_and_error_handling |
| 26 | + |
| 27 | +local luzer = require("luzer") |
| 28 | +local test_lib = require("lib") |
| 29 | +local MAX_INT = test_lib.MAX_INT |
| 30 | + |
| 31 | +-- The function introduced in Lua 5.3. |
| 32 | +if test_lib.lua_version() == "LuaJIT" then |
| 33 | + os.exit() |
| 34 | +end |
| 35 | + |
| 36 | +local function TestOneInput(buf) |
| 37 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 38 | + local n = fdp:consume_integer(1, MAX_INT) |
| 39 | + local ch = fdp:consume_integers(0, MAX_INT, n) |
| 40 | + os.setlocale(test_lib.random_locale(fdp), "all") |
| 41 | + utf8.char(unpack(ch)) |
| 42 | +end |
| 43 | + |
| 44 | +local args = { |
| 45 | + artifact_prefix = "utf8_char_", |
| 46 | +} |
| 47 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments