Skip to content

Commit b8b0cfd

Browse files
authored
checker: disallow string to voidptr cast entirely (#20351)
1 parent 3179880 commit b8b0cfd

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

vlib/builtin/js/builtin.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn (err IError) str() string {
3737
else {
3838
// >> Hack to allow old style custom error implementations
3939
// TODO: remove once deprecation period for `IError` methods has ended
40-
old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between them
40+
old_error_style := unsafe { voidptr(&err.msg.str) != voidptr(&err.code.str) } // if fields are not defined (new style) they don't have an offset between them
4141
if old_error_style {
4242
'${err.type_name()}: ${err.msg}'
4343
} else {

vlib/v/checker/checker.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,6 +3180,9 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
31803180
snexpr := node.expr.str()
31813181
tt := c.table.type_to_str(to_type)
31823182
c.error('cannot cast string to `${tt}`, use `${snexpr}[index]` instead.', node.pos)
3183+
} else if final_from_sym.kind == .string && to_type.is_voidptr()
3184+
&& !node.expr_type.has_flag(.generic) && !from_type.is_ptr() {
3185+
c.error('cannot cast string to `voidptr`, use voidptr(s.str) instead', node.pos)
31833186
} else if final_from_sym.kind == .string && to_type.is_pointer() && !c.inside_unsafe {
31843187
tt := c.table.type_to_str(to_type)
31853188
c.error('cannot cast string to `${tt}` outside `unsafe`, use ${tt}(s.str) instead',

vlib/v/checker/tests/invalid_string_cast_to_pointers_err.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vlib/v/checker/tests/invalid_string_cast_to_pointers_err.vv:2:9: error: cannot cast string to `voidptr` outside `unsafe`, use voidptr(s.str) instead
1+
vlib/v/checker/tests/invalid_string_cast_to_pointers_err.vv:2:9: error: cannot cast string to `voidptr`, use voidptr(s.str) instead
22
1 | test := 'test'
33
2 | println(voidptr(test))
44
| ~~~~~~~~~~~~~

0 commit comments

Comments
 (0)