# json: disallow overlong and out-of-range UTF-8 · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/2933a8241a54af436f2df5eac73aa2acf5eabd40)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 2933a8241a54af436f2df5eac73aa2acf5eabd40

Requested commit: 2933a8241a54af436f2df5eac73aa2acf5eabd40

Commit: 2933a8241a54af436f2df5eac73aa2acf5eabd40

Tree: b0a937c67db1a30df6251273ab51a80c9682f053

Author: hryx

Committer: Andrew Kelley

## Message

```
json: disallow overlong and out-of-range UTF-8

Fixes #2379

= Overlong (non-shortest) sequences

UTF-8's unique encoding scheme allows for some Unicode codepoints
to be represented in multiple ways. For any of these characters,
the spec forbids all but the shortest form. These disallowed longer
sequences are called "overlong". As an interesting side effect of
this rule, the bytes C0 and C1 never appear in valid UTF-8.

= Codepoint range

UTF-8 disallows representation of codepoints beyond U+10FFFF,
which is the highest character which can be encoded in UTF-16.
Because a 4-byte sequence is capable of resulting in such characters,
they must be explicitly rejected. This rule also has an interesting
side effect, which is that bytes F5 to FF never appear.

= References

Detecting an overlong version of a codepoint could get gnarly, but
luckily The Unicode Consortium did the hard work by creating this
handy table of valid byte sequences:

https://unicode.org/versions/corrigendum1.html

I thought this mapped nicely to the parser's state machine, so I
rearranged the relevant states to make use of it.

```

## Parents

- [9390e8b84883331757d3ce15cfca89279aceb090](https://git.cafe/gitcafe/zig/commit/9390e8b84883331757d3ce15cfca89279aceb090?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/2933a8241a54af436f2df5eac73aa2acf5eabd40?format=markdown)
