vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

Terminated by signal 11 #6834

Closed axetroy closed 3 years ago

axetroy commented 3 years ago

V version:

V 0.1.29 5016350

OS:

macos

What did you do?

Sorry, I don’t know where the problem is, I can only paste my code

module main

import os { ls, join_path, getwd, is_dir, is_file, is_link, rmdir, rm, file_size }
import sync { RwMutex }
import flag

const (
    version    = 'v0.2.2'
    dir_prune  = ['node_modules', 'bower_components', '.temp', '.dist']
    file_prune = ['.DS_Store']
)

fn print_help() {
    print('prune - A tool for prune your file-system

USAGE:
  prune [OPTIONS] <dirs>

OPTIONS:
  --help        print help information
  --version     print version information
  --check-only  where check prune only without any file remove

EXAMPLE:
  prune ./dir1 ./dir2 ./dir3

SOURCE CODE:
  https://github.com/axetroy/prune
')
}

struct Result {
    check_mode bool
mut:
    folder     int // folder count
    file       int // file count
    size       int // the prune size
    mtx        &sync.RwMutex // r/w lock
}

fn (mut r Result) increase_size(i int) {
    r.mtx.w_lock()
    r.size += i
    r.mtx.w_unlock()
}

fn (mut r Result) increase_folder(i int) {
    r.mtx.w_lock()
    r.folder += i
    r.mtx.w_unlock()
}

fn (mut r Result) increase_file(i int) {
    r.mtx.w_lock()
    r.file += i
    r.mtx.w_unlock()
}

fn calc_size(filepath string, mut result &Result) int {
    if is_dir(filepath) {
        files := ls(filepath) or {
            panic(err)
        }
        result.increase_folder(1)
        mut size := 0
        for file in files {
            target := join_path(filepath, file)
            if is_dir(target) {
                size += calc_size(target, mut result)
            } else if is_link(target) {
                result.increase_file(1)
                size += file_size(target)
            } else if is_file(target) {
                result.increase_file(1)
                size += file_size(target)
            }
        }
        return size
    } else {
        result.increase_file(1)
        return file_size(filepath)
    }
}

fn main() {
    mut fp := flag.new_flag_parser(os.args)
    fp.skip_executable()
    is_help := fp.bool('help', 0, false, 'prine help information')
    is_version := fp.bool('version', 0, false, 'prine version information')
    is_check_only := fp.bool('check-only', 0, false, 'where check prune only without any file remove')
    additional_args := fp.finalize() or {
        eprintln(err)
        print_help()
        return
    }
    if is_help {
        print_help()
        return
    }
    if is_version {
        println(version)
        return
    }
    mut targets := []string{}
    cwd := getwd()
    for _, dir in additional_args {
        if is_abs_path(dir) {
            targets << dir
        } else {
            targets << join_path(cwd, dir)
        }
    }
    if targets.len < 1 {
        print_help()
        return
    }
    mut result := Result{
        check_mode: is_check_only
        size: 0
        mtx: sync.new_rwmutex()
    }
    mut wg := sync.new_waitgroup()
    wg.add(targets.len)
    for _, target in targets {
        go walk(target, mut wg, mut &result)
    }
    wg.wait()
    println('prune folder: $result.folder file: $result.file size: $result.size Bytes')
}

fn remove_dir(dir string, mut group sync.WaitGroup, mut result &Result) {
    size := calc_size(dir, mut result)
    if result.check_mode == false {
        rmdir(dir) or {
            panic(err)
        }
    }
    println(dir)
    result.increase_size(size)
    group.done()
}

fn remove_file(file string, mut group sync.WaitGroup, mut result &Result) {
    size := calc_size(file, mut result)
    if result.check_mode == false {
        rm(file) or {
            panic(err)
        }
    }
    println(file)
    result.increase_size(size)
    group.done()
}

fn walk(dir string, mut group sync.WaitGroup, mut result &Result) {
    files := ls(dir) or {
        panic(err)
    }
    for file in files {
        filepath := join_path(dir, file)
        if is_dir(filepath) {
            group.add(1)
            if file in dir_prune {
                go remove_dir(filepath, mut group, mut &result)
            } else {
                go walk(filepath, mut group, mut &result)
            }
        } else if is_file(filepath) {
            if file in file_prune {
                group.add(1)
                go remove_file(filepath, mut group, mut &result)
            }
        }
    }
    group.done()
}

What did you expect to see?

It should work well because the compiler did not check any errors.

What did you see instead?

$ v run main.v --check-only /Users/axetroy/gpm
Terminated by signal 11 (SIGSEGV)

DEBUG INFORMATION

$ v -v run main.v --check-only /Users/axetroy/gpm
builder.compile() pref:
all .v files before:
v.pref.lookup_path: ['/Users/axetroy/v/vlib', '/Users/axetroy/.vmodules']
v_files_from_dir ("/Users/axetroy/v/vlib/builtin")
get_v_files(main.v)
> just compile one file: "main.v"
user_files: ['main.v']
x: "/Users/axetroy/gpm/github.com/axetroy/prune/modules"
v.module_search_paths:
['/Users/axetroy/gpm/github.com/axetroy/prune', '/Users/axetroy/gpm/github.com/axetroy/prune/modules', '/Users/axetroy/v/vlib', '/Users/axetroy/.vmodules']
all .v files:
['/Users/axetroy/v/vlib/builtin/array.v', '/Users/axetroy/v/vlib/builtin/builtin.v', '/Users/axetroy/v/vlib/builtin/builtin_nix.c.v', '/Users/axetroy/v/vlib/builtin/cfns.c.v', '/Users/axetroy/v/vlib/builtin/chan.v', '/Users/axetroy/v/vlib/builtin/float.v', '/Users/axetroy/v/vlib/builtin/int.v', '/Users/axetroy/v/vlib/builtin/map.v', '/Users/axetroy/v/vlib/builtin/option.v', '/Users/axetroy/v/vlib/builtin/rune.v', '/Users/axetroy/v/vlib/builtin/sorted_map.v', '/Users/axetroy/v/vlib/builtin/string.v', '/Users/axetroy/v/vlib/builtin/utf8.v', 'main.v']
build_c(/var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c)
  >> trying to find strings in /Users/axetroy/v/strings ..
  >> trying to find strings in /Users/axetroy/gpm/github.com/axetroy/prune/strings ..
  >> trying to find strings in /Users/axetroy/gpm/github.com/axetroy/prune/modules/strings ..
  >> trying to find strings in /Users/axetroy/v/vlib/strings ..
  << found /Users/axetroy/v/vlib/strings .
v_files_from_dir ("/Users/axetroy/v/vlib/strings")
  >> trying to find strconv in /Users/axetroy/v/strconv ..
  >> trying to find strconv in /Users/axetroy/gpm/github.com/axetroy/prune/strconv ..
  >> trying to find strconv in /Users/axetroy/gpm/github.com/axetroy/prune/modules/strconv ..
  >> trying to find strconv in /Users/axetroy/v/vlib/strconv ..
  << found /Users/axetroy/v/vlib/strconv .
v_files_from_dir ("/Users/axetroy/v/vlib/strconv")
  >> trying to find hash in /Users/axetroy/v/hash ..
  >> trying to find hash in /Users/axetroy/gpm/github.com/axetroy/prune/hash ..
  >> trying to find hash in /Users/axetroy/gpm/github.com/axetroy/prune/modules/hash ..
  >> trying to find hash in /Users/axetroy/v/vlib/hash ..
  << found /Users/axetroy/v/vlib/hash .
v_files_from_dir ("/Users/axetroy/v/vlib/hash")
  >> trying to find os in /Users/axetroy/gpm/github.com/axetroy/prune/os ..
  >> trying to find os in /Users/axetroy/gpm/github.com/axetroy/prune/modules/os ..
  >> trying to find os in /Users/axetroy/v/vlib/os ..
  << found /Users/axetroy/v/vlib/os .
v_files_from_dir ("/Users/axetroy/v/vlib/os")
  >> trying to find sync in /Users/axetroy/gpm/github.com/axetroy/prune/sync ..
  >> trying to find sync in /Users/axetroy/gpm/github.com/axetroy/prune/modules/sync ..
  >> trying to find sync in /Users/axetroy/v/vlib/sync ..
  << found /Users/axetroy/v/vlib/sync .
v_files_from_dir ("/Users/axetroy/v/vlib/sync")
  >> trying to find flag in /Users/axetroy/gpm/github.com/axetroy/prune/flag ..
  >> trying to find flag in /Users/axetroy/gpm/github.com/axetroy/prune/modules/flag ..
  >> trying to find flag in /Users/axetroy/v/vlib/flag ..
  << found /Users/axetroy/v/vlib/flag .
v_files_from_dir ("/Users/axetroy/v/vlib/flag")
  >> trying to find math.bits in /Users/axetroy/v/math/bits ..
  >> trying to find math.bits in /Users/axetroy/gpm/github.com/axetroy/prune/math/bits ..
  >> trying to find math.bits in /Users/axetroy/gpm/github.com/axetroy/prune/modules/math/bits ..
  >> trying to find math.bits in /Users/axetroy/v/vlib/math/bits ..
  << found /Users/axetroy/v/vlib/math/bits .
v_files_from_dir ("/Users/axetroy/v/vlib/math/bits")
  >> trying to find time in /Users/axetroy/v/time ..
  >> trying to find time in /Users/axetroy/gpm/github.com/axetroy/prune/time ..
  >> trying to find time in /Users/axetroy/gpm/github.com/axetroy/prune/modules/time ..
  >> trying to find time in /Users/axetroy/v/vlib/time ..
  << found /Users/axetroy/v/vlib/time .
v_files_from_dir ("/Users/axetroy/v/vlib/time")
  >> trying to find rand in /Users/axetroy/v/rand ..
  >> trying to find rand in /Users/axetroy/gpm/github.com/axetroy/prune/rand ..
  >> trying to find rand in /Users/axetroy/gpm/github.com/axetroy/prune/modules/rand ..
  >> trying to find rand in /Users/axetroy/v/vlib/rand ..
  << found /Users/axetroy/v/vlib/rand .
v_files_from_dir ("/Users/axetroy/v/vlib/rand")
  >> trying to find runtime in /Users/axetroy/v/runtime ..
  >> trying to find runtime in /Users/axetroy/gpm/github.com/axetroy/prune/runtime ..
  >> trying to find runtime in /Users/axetroy/gpm/github.com/axetroy/prune/modules/runtime ..
  >> trying to find runtime in /Users/axetroy/v/vlib/runtime ..
  << found /Users/axetroy/v/vlib/runtime .
v_files_from_dir ("/Users/axetroy/v/vlib/runtime")
  >> trying to find rand.util in /Users/axetroy/v/rand/util ..
  >> trying to find rand.util in /Users/axetroy/gpm/github.com/axetroy/prune/rand/util ..
  >> trying to find rand.util in /Users/axetroy/gpm/github.com/axetroy/prune/modules/rand/util ..
  >> trying to find rand.util in /Users/axetroy/v/vlib/rand/util ..
  << found /Users/axetroy/v/vlib/rand/util .
v_files_from_dir ("/Users/axetroy/v/vlib/rand/util")
  >> trying to find rand.wyrand in /Users/axetroy/v/rand/wyrand ..
  >> trying to find rand.wyrand in /Users/axetroy/gpm/github.com/axetroy/prune/rand/wyrand ..
  >> trying to find rand.wyrand in /Users/axetroy/gpm/github.com/axetroy/prune/modules/rand/wyrand ..
  >> trying to find rand.wyrand in /Users/axetroy/v/vlib/rand/wyrand ..
  << found /Users/axetroy/v/vlib/rand/wyrand .
v_files_from_dir ("/Users/axetroy/v/vlib/rand/wyrand")
------ resolved dependencies graph: ------

 * strconv -> strings
 * strconv -> math.bits
 * builtin -> strings
 * builtin -> strconv
 * builtin -> hash
 * os -> builtin
 * os -> strings
 * flag -> builtin
 * time -> builtin
 * runtime -> builtin
 * runtime -> os
 * rand.util -> builtin
 * rand.util -> time
 * rand.wyrand -> builtin
 * rand.wyrand -> math.bits
 * rand.wyrand -> rand.util
 * rand.wyrand -> hash
 * rand -> builtin
 * rand -> rand.util
 * rand -> rand.wyrand
 * rand -> time
 * sync -> builtin
 * sync -> time
 * sync -> rand
 * sync -> runtime
 * main -> builtin
 * main -> os
 * main -> sync
 * main -> flag

------------------------------------------
------ imported modules: ------
['strings', 'hash', 'math.bits', 'strconv', 'builtin', 'os', 'flag', 'time', 'runtime', 'rand.util', 'rand.wyrand', 'rand', 'sync', 'main']
-------------------------------
PARSE: 250 ms
CHECK: 59 ms
C GEN: 73 ms
builder.cc() pref.out_name="/Users/axetroy/gpm/github.com/axetroy/prune/main"
build_thirdparty_obj_files: v.table.cflags: [CFlag{ name: "-I" value: "/Users/axetroy/v/thirdparty/stdatomic/win" mod: "sync" os: "windows" cached: "" }, CFlag{ name: "-I" value: "/Users/axetroy/v/thirdparty/stdatomic/nix" mod: "sync" os: "linux" cached: "" }, CFlag{ name: "-I" value: "/Users/axetroy/v/thirdparty/stdatomic/nix" mod: "sync" os: "darwin" cached: "" }, CFlag{ name: "-I" value: "/Users/axetroy/v/thirdparty/stdatomic/nix" mod: "sync" os: "freebsd" cached: "" }, CFlag{ name: "-I" value: "/Users/axetroy/v/thirdparty/stdatomic/nix" mod: "sync" os: "solaris" cached: "" }, CFlag{ name: "-l" value: "pthread" mod: "sync" os: "" cached: "" }]
cc() isprod=false outname=/Users/axetroy/gpm/github.com/axetroy/prune/main
cc args=  -std=gnu99 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-unused-result -Wno-unused-function -Wno-missing-braces -Wno-unused-label -Werror=implicit-function-declaration -o "/Users/axetroy/gpm/github.com/axetroy/prune/main" -x objective-c "/var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c" -x none -mmacosx-version-min=10.7  -I "/Users/axetroy/v/thirdparty/stdatomic/nix" -lpthread   
['', '-std=gnu99', '-Wall', '-Wextra', '-Wno-unused-variable', '-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label', '-Werror=implicit-function-declaration', '-o "/Users/axetroy/gpm/github.com/axetroy/prune/main"', '-x objective-c', '"/var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c"', '-x none', '-mmacosx-version-min=10.7', '', '-I "/Users/axetroy/v/thirdparty/stdatomic/nix" -lpthread', '']

=====================
> C compiler cmd: cc @/var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c.rsp
=====================
C  cc: 1403 ms
>> remove tmp file: /var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c
>> remove tmp file: /var/folders/yv/6d78vvyx3cs0yd1r6qqkk_0m0000gn/T/v/main.15902725642687299632.tmp.c.rsp
cc took 1403 ms
=========

============ running /Users/axetroy/gpm/github.com/axetroy/prune/main ============
command to run executable: "/Users/axetroy/gpm/github.com/axetroy/prune/main" --check-only /Users/axetroy/gpm
Terminated by signal 11 (SIGSEGV)
spaceface777 commented 3 years ago

Your code runs properly (i.e. doesn't segfault) for me, but if I do v -show-c-output ..., it prints many C warnings; presumably it is caused by that. Not sure which warnings are caused by the modules you import, and which are caused by your code, yet, but wherever the issue is, V should have probably reported an error.

axetroy commented 3 years ago

Due to too long time, I close this issue.