Skip to content

Commit

Permalink
checker: add missing check for ref passing to non-ref (#22194)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Sep 14, 2024
1 parent 469a532 commit 4eb087a
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/gg/path_finding_algorithm_visualizer/aStar.v
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fn astar_path_finding(mut app App, mut grid [][]Cell, start Point, end Point) {

g_score[start.x][start.y] = 0
f_score[start.x][start.y] = g_score[start.x][start.y] + hf(start, end)
priority_queue.insert(&Node{
priority_queue.insert(Node{
f_score: f_score[start.x][start.y]
cell: &Point{
x: start.x
Expand Down Expand Up @@ -360,7 +360,7 @@ fn astar_path_finding(mut app App, mut grid [][]Cell, start Point, end Point) {
if temp_g_score < g_score[neighbor.x][neighbor.y] {
g_score[neighbor.x][neighbor.y] = temp_g_score
if !(neighbor.x == start.x && neighbor.y == start.y) {
priority_queue.insert(&Node{
priority_queue.insert(Node{
f_score: g_score[neighbor.x][neighbor.y] + hf(neighbor, end)
cell: neighbor
count: curr_node.count + 1
Expand Down
2 changes: 1 addition & 1 deletion vlib/crypto/ed25519/internal/edwards25519/element.v
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ pub fn (mut v Element) pow_22523(x Element) Element {
for i := 1; i < 100; i++ { // 2^200 - 2^100
t2.square(t2)
}
t1.multiply(t2, &t1) // 2^200 - 1
t1.multiply(t2, t1) // 2^200 - 1
t1.square(t1) // 2^201 - 2
for i := 1; i < 50; i++ { // 2^250 - 2^50
t1.square(t1)
Expand Down
2 changes: 1 addition & 1 deletion vlib/crypto/ed25519/internal/edwards25519/extra.v
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn (mut v Point) bytes_montgomery_generic(mut buf [32]u8) []u8 {
mut u := Element{}

y.multiply(v.y, y.invert(v.z)) // y = Y / Z
recip.invert(recip.subtract(fe_one, &y)) // r = 1/(1 - y)
recip.invert(recip.subtract(fe_one, y)) // r = 1/(1 - y)
u.multiply(u.add(fe_one, y), recip) // u = (1 + y)*r

return copy_field_element(mut buf, mut u)
Expand Down
8 changes: 4 additions & 4 deletions vlib/crypto/ed25519/internal/edwards25519/point.v
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ fn (mut v ProjectiveP1) sub(p Point, q ProjectiveCached) ProjectiveP1 {
ypx.add(p.y, p.x)
ymx.subtract(p.y, p.x)

pp.multiply(&ypx, q.ymx) // flipped sign
mm.multiply(&ymx, q.ypx) // flipped sign
pp.multiply(ypx, q.ymx) // flipped sign
mm.multiply(ymx, q.ypx) // flipped sign
tt2d.multiply(p.t, q.t2d)
zz2.multiply(p.z, q.z)

Expand All @@ -378,8 +378,8 @@ fn (mut v ProjectiveP1) add_affine(p Point, q AffineCached) ProjectiveP1 {
ypx.add(p.y, p.x)
ymx.subtract(p.y, p.x)

pp.multiply(&ypx, q.ypx)
mm.multiply(&ymx, q.ymx)
pp.multiply(ypx, q.ypx)
mm.multiply(ymx, q.ymx)
tt2d.multiply(p.t, q.t2d)

z2.add(p.z, p.z)
Expand Down
2 changes: 1 addition & 1 deletion vlib/crypto/ed25519/internal/edwards25519/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn (mut v ProjLookupTable) select_into(mut dest ProjectiveCached, x i8) {
for j := 1; j <= 8; j++ {
// Set dest = j*Q if |x| = j
cond := subtle.constant_time_byte_eq(xabs, u8(j))
dest.selected(&v.points[j - 1], dest, cond)
dest.selected(v.points[j - 1], dest, cond)
}
// Now dest = |x|*Q, conditionally negate to get x*Q
dest.cond_neg(int(xmask & 1))
Expand Down
4 changes: 2 additions & 2 deletions vlib/net/tcp.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ! {
// return err_option_wrong_type
// }
x := int(value)
s.set_option(C.SOL_SOCKET, int(opt), &x)!
s.set_option(C.SOL_SOCKET, int(opt), x)!
}

pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {
Expand All @@ -592,7 +592,7 @@ pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {

pub fn (mut s TcpSocket) set_dualstack(on bool) ! {
x := int(!on)
s.set_option(C.IPPROTO_IPV6, int(SocketOption.ipv6_only), &x)!
s.set_option(C.IPPROTO_IPV6, int(SocketOption.ipv6_only), x)!
}

fn (mut s TcpSocket) set_default_options() ! {
Expand Down
2 changes: 1 addition & 1 deletion vlib/net/unix/stream.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ pub fn (mut s StreamSocket) set_option_bool(opt net.SocketOption, value bool) !
return net.err_option_wrong_type
}
x := int(value)
s.set_option(C.SOL_SOCKET, int(opt), &x)!
s.set_option(C.SOL_SOCKET, int(opt), x)!
}

// set_option_bool sets an int option on the socket
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,23 @@ fn gen_all_registers(mut t Table, without_numbers []string, with_numbers map[str
return res
}

pub fn (expr Expr) is_reference() bool {
return match expr {
PrefixExpr {
expr.op == .amp
}
UnsafeExpr {
expr.expr.is_reference()
}
ParExpr {
expr.expr.is_reference()
}
else {
false
}
}
}

// is `expr` a literal, i.e. it does not depend on any other declarations (C compile time constant)
pub fn (expr Expr) is_literal() bool {
return match expr {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
return
}
} else {
// passing &expr where no-pointer is expected
if expected != ast.voidptr_type && !expected.is_ptr() && got.is_ptr()
&& arg.expr.is_reference() {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
}
if expected.has_flag(.option) {
got_is_ptr := got.is_ptr()
|| (arg.expr is ast.Ident && (arg.expr as ast.Ident).is_mut())
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/ref_to_non_ref_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vlib/v/checker/tests/ref_to_non_ref_err.vv:10:17: error: cannot use `&Foo` as `Foo` in argument 1 to `.index()`
8 | }
9 |
10 | dump(list.index(unsafe { &list[1] }))
| ~~~~~~~~~~~~~~~~~~~
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/ref_to_non_ref_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct Foo {
idx int
}

mut list := []Foo{}
for i in 0..3 {
list << Foo { idx: i }
}

dump(list.index(unsafe { &list[1] }))
2 changes: 2 additions & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
} else if arg.expr is ast.None {
g.expr_with_opt(arg.expr, arg_typ, expected_type)
return
} else if arg.expr.is_literal() {
g.write('(voidptr)')
} else {
needs_closing = true
if arg_typ_sym.kind in [.sum_type, .interface_] {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/js/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn (mut g JsGen) gen_array_index_method(left_type ast.Type) string {
fn_builder.writeln('\treturn new int(-1);')
fn_builder.writeln('}')
g.definitions.writeln(fn_builder.str())
left_sym.register_method(&ast.Fn{
left_sym.register_method(ast.Fn{
name: 'index'
params: [ast.Param{
typ: unwrap_left_type
Expand Down Expand Up @@ -199,7 +199,7 @@ fn (mut g JsGen) gen_array_contains_method(left_type ast.Type) string {
fn_builder.writeln('\treturn new bool(false);')
fn_builder.writeln('}')
g.definitions.writeln(fn_builder.str())
left_sym.register_method(&ast.Fn{
left_sym.register_method(ast.Fn{
name: 'contains'
params: [ast.Param{
typ: unwrap_left_type
Expand Down

0 comments on commit 4eb087a

Please sign in to comment.