From aed348fb8094f4c8b853a8ccedbd1fff8359aef9 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 27 Feb 2021 08:11:17 +0000 Subject: [PATCH] os: tag some File methods that take voidptr as unsafe - write_bytes, write_bytes_at (#8985) --- examples/wkhtmltopdf.v | 2 +- vlib/os/file.c.v | 2 ++ vlib/os/os.v | 2 +- vlib/v/gen/x64/elf.v | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/wkhtmltopdf.v b/examples/wkhtmltopdf.v index 49248569e3..c519ed5211 100644 --- a/examples/wkhtmltopdf.v +++ b/examples/wkhtmltopdf.v @@ -78,7 +78,7 @@ fn main() { println('ERR: $err') return } - wrote := file.write_bytes(data, size) + wrote := unsafe { file.write_bytes(data, size) } println('write_bytes: $wrote [./google.pdf]') file.flush() file.close() diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 6374269945..5e008a3a24 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -79,10 +79,12 @@ pub fn (mut f File) write_to(pos int, buf []byte) ?int { return res } +[unsafe] pub fn (mut f File) write_bytes(data voidptr, size int) int { return int(C.fwrite(data, 1, size, f.cfile)) } +[unsafe] pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int { C.fseek(f.cfile, pos, C.SEEK_SET) res := int(C.fwrite(data, 1, size, f.cfile)) diff --git a/vlib/os/os.v b/vlib/os/os.v index 595debe065..d0b7548a4a 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -325,7 +325,7 @@ pub fn write_file(path string, text string) ? { // write_file_array writes the data in `buffer` to a file in `path`. pub fn write_file_array(path string, buffer array) ? { mut f := create(path) ? - f.write_bytes_at(buffer.data, (buffer.len * buffer.element_size), 0) + unsafe { f.write_bytes_at(buffer.data, (buffer.len * buffer.element_size), 0) } f.close() } diff --git a/vlib/v/gen/x64/elf.v b/vlib/v/gen/x64/elf.v index 1e4e3673c8..5ffbc73d25 100644 --- a/vlib/v/gen/x64/elf.v +++ b/vlib/v/gen/x64/elf.v @@ -101,7 +101,7 @@ pub fn (mut g Gen) generate_elf_footer() { // Create the binary mut f := os.create(g.out_name) or { panic(err) } os.chmod(g.out_name, 0o775) // make it an executable - f.write_bytes(g.buf.data, g.buf.len) + unsafe { f.write_bytes(g.buf.data, g.buf.len) } f.close() println('\nx64 elf binary has been successfully generated') }