1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add checks for byte casting (#5917)

This commit is contained in:
Swastik Baranwal 2020-08-02 02:47:00 +05:30 committed by GitHub
parent 4b8652755d
commit d56d622a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 3 deletions

View File

@ -252,7 +252,7 @@ pub fn (mut ws Client) write(payload byteptr, payload_len int, code OPCode) int
fbdata := byteptr(frame_buf.data)
masking_key := create_masking_key()
mut header := [`0`].repeat(header_len)
header[0] = byte(code) | 0x80
header[0] = byte(int(code)) | 0x80
if payload_len <= 125 {
header[1] = byte(payload_len | 0x80)
header[2] = masking_key[0]
@ -608,7 +608,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b
frame_len := header_len + payload.len
mut control_frame := [`0`].repeat(frame_len)
masking_key := create_masking_key()
control_frame[0] = byte(code | 0x80)
control_frame[0] = byte(int(code) | 0x80)
control_frame[1] = byte(payload.len | 0x80)
control_frame[2] = masking_key[0]
control_frame[3] = masking_key[1]

View File

@ -789,7 +789,7 @@ pub:
pub struct CastExpr {
pub:
expr Expr // `buf`
expr Expr // `buf` in `string(buf, n)`
arg Expr // `n` in `string(buf, n)`
typ table.Type // `string` TODO rename to `type_to_cast_to`
pos token.Position

View File

@ -2286,6 +2286,11 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
}
c.error(error_msg, node.pos)
}
} else if !node.expr_type.is_int() && node.expr_type != table.voidptr_type &&
!node.expr_type.is_ptr() &&
to_type_sym.kind == .byte {
type_name := c.table.type_to_str(node.expr_type)
c.error('cannot cast type `$type_name` to `byte`', node.pos)
}
if node.has_arg {
c.expr(node.arg)