From d56d622a433a68bf3142cace2573710b90a152dc Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 2 Aug 2020 02:47:00 +0530 Subject: [PATCH] checker: add checks for byte casting (#5917) --- vlib/net/websocket/ws.v | 4 ++-- vlib/v/ast/ast.v | 2 +- vlib/v/checker/checker.v | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index 5517660ea2..6d47d15c2b 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -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] diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 498cb3053d..d7e604a88e 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0b461382fb..9340fb5c09 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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)