vlib: add [unsafe] tag to more functions: tos, string_from_wide, strings.Builder: write_bytes, free (#8766)

pull/8662/head
Nick Treleaven 2021-02-15 15:15:52 +00:00 committed by GitHub
parent 4bdbb0cfa8
commit 4a0367a63c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 82 additions and 60 deletions

View File

@ -292,8 +292,10 @@ fn (vd VDoc) gen_html(d doc.Doc) string {
}
modules_toc_str := modules_toc.str()
symbols_toc_str := symbols_toc.str()
unsafe {
modules_toc.free()
symbols_toc.free()
}
return html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}',
header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}',
vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}',
@ -471,7 +473,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
dnw.writeln('</section>')
dnw_str := dnw.str()
defer {
dnw.free()
unsafe { dnw.free() }
}
return dnw_str
}

View File

@ -142,11 +142,11 @@ fn get_reg_value(reg_env_key voidptr, key string) ?string {
$if windows {
// query the value (shortcut the sizing step)
reg_value_size := 4095 // this is the max length (not for the registry, but for the system %PATH%)
mut reg_value := &u16(malloc(reg_value_size))
mut reg_value := unsafe { &u16(malloc(reg_value_size)) }
if C.RegQueryValueEx(reg_env_key, key.to_wide(), 0, 0, reg_value, &reg_value_size) != 0 {
return error('Unable to get registry value for "$key", try rerunning as an Administrator')
}
return string_from_wide(reg_value)
return unsafe { string_from_wide(reg_value) }
}
return error('not on windows')
}

View File

@ -142,14 +142,14 @@ fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {
nframe := frames - i - 1
mut lineinfo := ''
if C.SymGetLineFromAddr64(handle, frame_addr, &offset, &sline64) == 1 {
file_name := tos3(sline64.f_file_name)
file_name := unsafe { tos3(sline64.f_file_name) }
lnumber := sline64.f_line_number
lineinfo = '$file_name:$lnumber'
} else {
addr:
lineinfo = '?? : address = 0x${(&frame_addr):x}'
}
sfunc := tos3(fname)
sfunc := unsafe { tos3(fname) }
eprintln('${nframe:-2d}: ${sfunc:-25s} $lineinfo')
} else {
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes

View File

@ -51,6 +51,6 @@ pub fn (b []byte) bytestr() string {
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << `\0`
res := tos(copy.data, copy.len-1)
res := unsafe { tos(copy.data, copy.len-1) }
return res
}

View File

@ -73,7 +73,7 @@ pub fn vstrlen(s byteptr) int {
// tos converts a C string to a V string.
// String data is reused, not copied.
//[unsafe]
[unsafe]
pub fn tos(s byteptr, len int) string {
// This should never happen.
if s == 0 {
@ -86,12 +86,14 @@ pub fn tos(s byteptr, len int) string {
}
// tos_clone returns a copy of `s`.
[unsafe]
pub fn tos_clone(s byteptr) string {
return tos2(s).clone()
return unsafe { tos2(s) }.clone()
}
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
// Used only internally.
[unsafe]
pub fn tos2(s byteptr) string {
if s == 0 {
panic('tos2: nil string')
@ -103,6 +105,7 @@ pub fn tos2(s byteptr) string {
}
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
[unsafe]
pub fn tos3(s charptr) string {
if s == 0 {
panic('tos3: nil string')
@ -114,19 +117,21 @@ pub fn tos3(s charptr) string {
}
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
[unsafe]
pub fn tos4(s byteptr) string {
if s == 0 {
return ''
}
return tos2(s)
return unsafe { tos2(s) }
}
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
[unsafe]
pub fn tos5(s charptr) string {
if s == 0 {
return ''
}
return tos3(s)
return unsafe { tos3(s) }
}
[deprecated]
@ -207,7 +212,7 @@ pub fn (s string) cstr() byteptr {
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
[unsafe]
pub fn cstring_to_vstring(cstr byteptr) string {
return tos_clone(cstr)
return unsafe { tos_clone(cstr) }
}
// replace_once replaces the first occurence of `rep` with the string passed in `with`.

View File

@ -21,6 +21,7 @@ pub fn (_str string) to_wide() &u16 {
}
}
[unsafe]
pub fn string_from_wide(_wstr &u16) string {
$if windows {
unsafe {
@ -32,6 +33,7 @@ pub fn string_from_wide(_wstr &u16) string {
}
}
[unsafe]
pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows {
unsafe {

View File

@ -249,7 +249,7 @@ fn gg_cleanup_fn(user_data voidptr) {
fn gg_fail_fn(msg charptr, user_data voidptr) {
mut g := unsafe { &Context(user_data) }
vmsg := tos3(msg)
vmsg := unsafe { tos3(msg) }
if g.config.fail_fn != voidptr(0) {
g.config.fail_fn(vmsg, g.config.user_data)
} else {

View File

@ -108,7 +108,7 @@ fn decode_string(root &C.cJSON) string {
}
// println('decode string valuestring="$root.valuestring"')
// return tos(root.valuestring, _strlen(root.valuestring))
return tos_clone(root.valuestring) // , _strlen(root.valuestring))
return unsafe { tos_clone(root.valuestring) } // , _strlen(root.valuestring))
}
fn C.cJSON_IsTrue(voidptr) bool

View File

@ -45,17 +45,17 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
mut readcounter := 0
for {
readcounter++
len := C.BIO_read(web, buff, bufsize)
len := unsafe { C.BIO_read(web, buff, bufsize) }
if len <= 0 {
break
}
$if debug_http ? {
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
eprintln('-'.repeat(20))
eprintln(tos(buff, len))
eprintln(unsafe { tos(buff, len) })
eprintln('-'.repeat(20))
}
content.write_bytes(buff, len)
unsafe { content.write_bytes(buff, len) }
}
if web != 0 {
C.BIO_free_all(web)

View File

@ -35,7 +35,7 @@ pub fn (mut con TcpConn) read_line() string {
}
}
bufbp := byteptr(buf)
line = tos_clone(bufbp)
line = unsafe { tos_clone(bufbp) }
if eol_idx > 0 {
// At this point, we are sure that recv returned valid data,
// that contains *at least* one line.

View File

@ -72,7 +72,7 @@ pub fn environ() map[string]string {
mut estrings := C.GetEnvironmentStringsW()
mut eline := ''
for c := estrings; *c != 0; {
eline = string_from_wide(c)
eline = unsafe { string_from_wide(c) }
eq_index := eline.index_byte(`=`)
if eq_index > 0 {
res[eline[0..eq_index]] = eline[eq_index + 1..]

View File

@ -319,7 +319,7 @@ pub fn posix_get_error_msg(code int) string {
if ptr_text == 0 {
return ''
}
return tos3(ptr_text)
return unsafe { tos3(ptr_text) }
}
// vpclose will close a file pointer opened with `vpopen`.
@ -463,7 +463,7 @@ pub fn rmdir(path string) ? {
// print_c_errno will print the current value of `C.errno`.
fn print_c_errno() {
e := C.errno
se := tos_clone(byteptr(C.strerror(C.errno)))
se := unsafe { tos_clone(byteptr(C.strerror(C.errno))) }
println('errno=$e err=$se')
}
@ -498,12 +498,12 @@ pub fn get_raw_line() string {
} $else {
max := size_t(0)
mut buf := charptr(0)
nr_chars := C.getline(&buf, &max, C.stdin)
nr_chars := unsafe { C.getline(&buf, &max, C.stdin) }
// defer { unsafe{ free(buf) } }
if nr_chars == 0 || nr_chars == -1 {
return ''
}
return tos3(buf)
return unsafe { tos3(buf) }
// res := tos_clone(buf)
// return res
}
@ -610,7 +610,7 @@ pub fn executable() string {
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0)
if final_len < size {
ret := string_from_wide2(final_path, final_len)
ret := unsafe { string_from_wide2(final_path, final_len) }
// remove '\\?\' from beginning (see link above)
return ret[4..]
} else {
@ -619,7 +619,7 @@ pub fn executable() string {
}
C.CloseHandle(file)
}
return string_from_wide2(result, len)
return unsafe { string_from_wide2(result, len) }
}
$if macos {
mut result := vcalloc(max_path_len)

View File

@ -82,7 +82,7 @@ fn init_os_args(argc int, argv &&byte) []string {
pub fn ls(path string) ?[]string {
mut res := []string{}
dir := C.opendir(charptr(path.str))
dir := unsafe { C.opendir(charptr(path.str)) }
if isnil(dir) {
return error('ls() couldnt open dir "$path"')
}
@ -99,9 +99,9 @@ pub fn ls(path string) ?[]string {
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
continue
}
}
res << tos_clone(bptr)
}
}
C.closedir(dir)
return res
}

View File

@ -79,7 +79,7 @@ mut:
fn init_os_args_wide(argc int, argv &byteptr) []string {
mut args_ := []string{}
for i in 0 .. argc {
args_ << string_from_wide(unsafe { &u16(argv[i]) })
args_ << unsafe { string_from_wide(&u16(argv[i])) }
}
return args_
}
@ -102,12 +102,12 @@ pub fn ls(path string) ?[]string {
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
// we should use FindFirstFileW and FindNextFileW
h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&find_file_data))
first_filename := string_from_wide(&u16(find_file_data.c_file_name))
first_filename := unsafe { string_from_wide(&u16(find_file_data.c_file_name)) }
if first_filename != '.' && first_filename != '..' {
dir_files << first_filename
}
for C.FindNextFile(h_find_files, voidptr(&find_file_data)) > 0 {
filename := string_from_wide(&u16(find_file_data.c_file_name))
filename := unsafe { string_from_wide(&u16(find_file_data.c_file_name)) }
if filename != '.' && filename != '..' {
dir_files << filename.clone()
}
@ -218,7 +218,7 @@ pub fn get_error_msg(code int) string {
if ptr_text == 0 { // compare with null
return ''
}
return string_from_wide(ptr_text)
return unsafe { string_from_wide(ptr_text) }
}
// exec starts the specified command, waits for it to complete, and returns its output.
@ -273,15 +273,17 @@ pub fn exec(cmd string) ?Result {
mut bytes_read := u32(0)
mut read_data := strings.new_builder(1024)
for {
readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read),
0)
mut result := false
unsafe {
result = C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0)
read_data.write_bytes(buf, int(bytes_read))
if readfile_result == false || int(bytes_read) == 0 {
}
if result == false || int(bytes_read) == 0 {
break
}
}
soutput := read_data.str().trim_space()
read_data.free()
unsafe { read_data.free() }
exit_code := u32(0)
C.WaitForSingleObject(proc_info.h_process, C.INFINITE)
C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code))

View File

@ -170,7 +170,7 @@ fn rw_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
}
} else {
r += idx
mut s := tos(buf, r)
mut s := unsafe { tos(buf, r) }
mut out := p.out
unsafe {
out += fd * max_write

View File

@ -28,8 +28,10 @@ pub fn (mut r Request) parse_request(s string, max_headers int) int {
0
)
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
r.num_headers = num_headers
}
return pret
@ -46,9 +48,11 @@ pub fn (mut r Request) parse_request_path(s string) int {
&r.path, &path_len
)
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
}
return pret
}
@ -63,8 +67,10 @@ pub fn (mut r Request) parse_request_path_pipeline(s string) int {
&r.path, &path_len
)
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
}
return pret
}

View File

@ -530,7 +530,7 @@ fn (re RE) get_char_class(pc int) string {
buf_ptr[i] = byte(0)
}
return tos_clone( buf_ptr )
return unsafe { tos_clone( buf_ptr ) }
}
fn (re RE) check_char_class(pc int, ch rune) bool {

View File

@ -123,7 +123,7 @@ pub fn (db DB) q_string(query string) string {
stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0)
C.sqlite3_step(stmt)
res := tos_clone(C.sqlite3_column_text(stmt, 0))
res := unsafe { tos_clone(C.sqlite3_column_text(stmt, 0)) }
C.sqlite3_finalize(stmt)
return res
}
@ -145,7 +145,7 @@ pub fn (db DB) exec(query string) ([]Row, int) {
}
mut row := Row{}
for i in 0 .. nr_cols {
val := tos_clone(C.sqlite3_column_text(stmt, i))
val := unsafe { tos_clone(C.sqlite3_column_text(stmt, i)) }
row.vals << val
}
rows << row

View File

@ -183,7 +183,7 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
}
res[r_i] = 0
//println("result: [${tos(&res[0],r_i)}]")
return tos(res.data, r_i)
return unsafe { tos(res.data, r_i) }
} else {
if dec_digit > 0 {
mut c := 0
@ -194,7 +194,7 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
}
res[r_i] = 0
}
return tos(res.data, r_i)
return unsafe { tos(res.data, r_i) }
}
}

View File

@ -336,5 +336,5 @@ pub fn f64_to_str_l(f f64) string {
}
}
res[r_i] = 0
return tos(res.data,r_i)
return unsafe { tos(res.data,r_i) }
}

View File

@ -27,6 +27,7 @@ pub fn new_builder(initial_size int) Builder {
}
// write_bytes appends `bytes` to the accumulated buffer
[unsafe]
pub fn (mut b Builder) write_bytes(bytes byteptr, howmany int) {
b.buf.push_many(bytes, howmany)
b.len += howmany
@ -61,8 +62,7 @@ pub fn (mut b Builder) go_back(n int) {
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << byte(`\0`)
res := tos(copy.data, copy.len - 1)
return res
return unsafe { tos(copy.data, copy.len - 1) }
}
// cut_last cuts the last `n` bytes from the buffer and returns them
@ -137,6 +137,7 @@ pub fn (mut b Builder) str() string {
}
// free - manually free the contents of the buffer
[unsafe]
pub fn (mut b Builder) free() {
unsafe { free(b.buf.data) }
// b.buf = []byte{cap: b.initial_size}

View File

@ -29,8 +29,10 @@ pub const (
pub fn vhash() string {
mut buf := [50]byte{}
buf[0] = 0
unsafe { C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH) }
unsafe {
C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)
return tos_clone(buf)
}
}
pub fn full_hash() string {
@ -95,8 +97,10 @@ pub fn githash(should_get_from_filesystem bool) string {
}
mut buf := [50]byte{}
buf[0] = 0
unsafe { C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH) }
unsafe {
C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH)
return tos_clone(buf)
}
}
//

View File

@ -96,7 +96,7 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
ctx.done = true
mut sb := strings.new_builder(1024)
defer {
sb.free()
unsafe { sb.free() }
}
sb.write('HTTP/1.1 $ctx.status')
sb.write('\r\nContent-Type: $mimetype')

View File

@ -10,7 +10,7 @@ fn (mut ws Client) handshake() ? {
seckey := base64.encode(nonce)
mut sb := strings.new_builder(1024)
defer {
sb.free()
unsafe { sb.free() }
}
sb.write('GET ')
sb.write(ws.uri.resource)

View File

@ -35,7 +35,7 @@ fn create_key_challenge_response(seckey string) ?string {
sha1buf := seckey + guid
shabytes := sha1buf.bytes()
hash := sha1.sum(shabytes)
b64 := base64.encode(tos(hash.data, hash.len))
b64 := base64.encode(unsafe { tos(hash.data, hash.len) })
unsafe {
hash.free()
shabytes.free()
@ -50,5 +50,5 @@ fn get_nonce(nonce_size int) string {
for i in 0 .. nonce_size {
nonce[i] = alphanum[rand.intn(alphanum.len)]
}
return tos(nonce.data, nonce.len).clone()
return unsafe { tos(nonce.data, nonce.len) }.clone()
}