Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check for invalid utf8 string #23721

Merged
merged 5 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions vlib/v/checker/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module checker

import v.ast
import v.token
import encoding.utf8

fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 {
if ftyp.has_option_or_result() {
Expand Down Expand Up @@ -131,6 +132,10 @@ const unicode_lit_overflow_message = 'unicode character exceeds max allowed valu
// https://stackoverflow.com/questions/52203351/why-unicode-is-restricted-to-0x10ffff
@[direct_array_access]
fn (mut c Checker) string_lit(mut node ast.StringLiteral) ast.Type {
valid_utf8 := utf8.validate(node.val.str, node.val.len)
if valid_utf8 == false {
kbkpbot marked this conversation as resolved.
Show resolved Hide resolved
c.warn("invalid utf8 string, please check your file's encoding is utf8", node.pos)
}
mut idx := 0
for idx < node.val.len {
match node.val[idx] {
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/invalid_utf8_string.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/checker/tests/invalid_utf8_string.vv:1:6: warning: invalid utf8 string, please check your file's encoding is utf8
1 | _ := '²âÊÔtxt'
| ~~~~~~~
1 change: 1 addition & 0 deletions vlib/v/checker/tests/invalid_utf8_string.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_ := '²âÊÔtxt'
Loading