Skip to content

Commit

Permalink
checker: warn when accessing union fields outside unsafe (#7869)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrel authored Jan 4, 2021
1 parent 7c9fb73 commit 040b923
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
5 changes: 4 additions & 1 deletion vlib/strconv/atof.v
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ pub fn atof64(s string) f64 {

res_parsing,pn = parser(s + ' ') // TODO: need an extra char for now
// println(pn)
unsafe {
match res_parsing {
parser_ok {
res.u = converter(mut pn)
Expand All @@ -539,6 +540,8 @@ pub fn atof64(s string) f64 {
res.u = double_minus_infinity
}
else {
}}
}
}
return res.f
}
}
2 changes: 2 additions & 0 deletions vlib/strconv/atofq.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn atof_quick(s string) f64 {
i++
}
}
unsafe {
// infinite
if s[i] == `i` && i + 2 < s.len && s[i + 1] == `n` && s[i + 2] == `f` {
if sign > 0.0 {
Expand Down Expand Up @@ -133,6 +134,7 @@ pub fn atof_quick(s string) f64 {
}
f.f = f.f * sign
return f.f
}
}

const (
Expand Down
8 changes: 4 additions & 4 deletions vlib/strconv/f32_str.v
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ pub fn f32_to_decimal(mant u32, exp u32) Dec32 {
// f32_to_str return a string in scientific notation with max n_digit after the dot
pub fn f32_to_str(f f32, n_digit int) string {
mut u1 := Uf32{}
u1.f = f
u := u1.u
unsafe { u1.f = f }
u := unsafe {u1.u}

neg := (u>>(mantbits32+expbits32)) != 0
mant := u & ((u32(1)<<mantbits32) - u32(1))
Expand All @@ -348,8 +348,8 @@ pub fn f32_to_str(f f32, n_digit int) string {
// f32_to_str return a string in scientific notation with max n_digit after the dot
pub fn f32_to_str_pad(f f32, n_digit int) string {
mut u1 := Uf32{}
u1.f = f
u := u1.u
unsafe { u1.f = f }
u := unsafe {u1.u}

neg := (u>>(mantbits32+expbits32)) != 0
mant := u & ((u32(1)<<mantbits32) - u32(1))
Expand Down
8 changes: 4 additions & 4 deletions vlib/strconv/f64_str.v
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
// f64_to_str return a string in scientific notation with max n_digit after the dot
pub fn f64_to_str(f f64, n_digit int) string {
mut u1 := Uf64{}
u1.f = f
u := u1.u
unsafe { u1.f = f }
u := unsafe {u1.u}

neg := (u>>(mantbits64+expbits64)) != 0
mant := u & ((u64(1)<<mantbits64) - u64(1))
Expand All @@ -395,8 +395,8 @@ pub fn f64_to_str(f f64, n_digit int) string {
// f64_to_str return a string in scientific notation with max n_digit after the dot
pub fn f64_to_str_pad(f f64, n_digit int) string {
mut u1 := Uf64{}
u1.f = f
u := u1.u
unsafe { u1.f = f }
u := unsafe {u1.u}

neg := (u>>(mantbits64+expbits64)) != 0
mant := u & ((u64(1)<<mantbits64) - u64(1))
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
// No automatic lock for struct access
explicit_lock_needed = true
}
if struct_info.is_union && !c.inside_unsafe {
c.warn('accessing union fields requires `unsafe`', expr.pos)
}
}
.array, .string {
// This should only happen in `builtin`
Expand Down

0 comments on commit 040b923

Please sign in to comment.