From 46f32fc10cca328f95dba09d60d15eb2d9d7c3c4 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 20 Aug 2022 08:03:07 +0300 Subject: [PATCH] checker: require unsafe for free() --- examples/sokol/particles/particles.v | 4 +++- examples/tcp_echo_server.v | 4 +++- examples/term.ui/pong.v | 4 +++- vlib/clipboard/clipboard.v | 4 +++- vlib/net/http/server.v | 4 +++- vlib/v/checker/fn.v | 4 ++++ vlib/v/preludes/test_runner.v | 2 +- vlib/vweb/vweb.v | 4 +++- 8 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/sokol/particles/particles.v b/examples/sokol/particles/particles.v index 75fad1a96..f4fc2d63b 100644 --- a/examples/sokol/particles/particles.v +++ b/examples/sokol/particles/particles.v @@ -47,7 +47,9 @@ fn (mut a App) init() { } fn (mut a App) cleanup() { - a.ps.free() + unsafe { + a.ps.free() + } } fn (mut a App) run() { diff --git a/examples/tcp_echo_server.v b/examples/tcp_echo_server.v index 5c50f4640..72b2fedd5 100644 --- a/examples/tcp_echo_server.v +++ b/examples/tcp_echo_server.v @@ -26,7 +26,9 @@ fn handle_client(mut socket net.TcpConn) { eprintln('> new client: $client_addr') mut reader := io.new_buffered_reader(reader: socket) defer { - reader.free() + unsafe { + reader.free() + } } socket.write_string('server: hello\n') or { return } for { diff --git a/examples/term.ui/pong.v b/examples/term.ui/pong.v index dd735e189..8423d8f8c 100644 --- a/examples/term.ui/pong.v +++ b/examples/term.ui/pong.v @@ -470,7 +470,9 @@ fn frame(x voidptr) { fn cleanup(x voidptr) { mut app := &App(x) - app.free() + unsafe { + app.free() + } } fn fail(error string) { diff --git a/vlib/clipboard/clipboard.v b/vlib/clipboard/clipboard.v index d99009a57..7efc7c7f2 100644 --- a/vlib/clipboard/clipboard.v +++ b/vlib/clipboard/clipboard.v @@ -23,7 +23,9 @@ pub fn (mut cb Clipboard) clear_all() { // destroy destroys the clipboard and frees its resources. pub fn (mut cb Clipboard) destroy() { - cb.free() + unsafe { + cb.free() + } } // check_ownership returns `true` if the `Clipboard` has the content ownership. diff --git a/vlib/net/http/server.v b/vlib/net/http/server.v index dbe40909d..45edc5094 100644 --- a/vlib/net/http/server.v +++ b/vlib/net/http/server.v @@ -88,7 +88,9 @@ fn (mut s Server) parse_and_respond(mut conn net.TcpConn) { mut reader := io.new_buffered_reader(reader: conn) defer { - reader.free() + unsafe { + reader.free() + } } req := parse_request(mut reader) or { $if debug { diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 6d9ee4f01..e900b295a 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1164,6 +1164,10 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { return ast.int_type } } + if !c.is_builtin_mod && !c.inside_unsafe && method_name == 'free' { + c.warn('manual memory management with `free()` is only allowed in unsafe code', + node.pos) + } if left_type == ast.void_type { // No need to print this error, since this means that the variable is unknown, // and there already was an error before. diff --git a/vlib/v/preludes/test_runner.v b/vlib/v/preludes/test_runner.v index f48249398..4c7771776 100644 --- a/vlib/v/preludes/test_runner.v +++ b/vlib/v/preludes/test_runner.v @@ -100,8 +100,8 @@ mut: pub fn change_test_runner(x &TestRunner) { pobj := unsafe { &C.main__TestRunner(&test_runner)._object } if pobj != 0 { - test_runner.free() unsafe { + test_runner.free() (&C.main__TestRunner(&test_runner))._object = nil } } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 4cbfe23ff..b9b61c89d 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -442,7 +442,9 @@ fn handle_conn(mut conn net.TcpConn, mut app T, routes map[string]Route) { mut reader := io.new_buffered_reader(reader: conn) defer { - reader.free() + unsafe { + reader.free() + } } page_gen_start := time.ticks() -- 2.30.2