Skip to content

autofree: runtime invalid string free #25221

@dy-tea

Description

@dy-tea

Describe the bug

See below snippet. Runs fine without autofree.

Reproduction Steps

Run with v run test.v and note normal exit, run with v -autofree run test.v and note abort:

fn main() {
	b := byteptr(c'a')
	s := unsafe { b.vstring() }
	println(s)
}

Expected Behavior

Should exit normally as with the gc.

Current Behavior

❯ v -autofree run main.v
a
free(): invalid pointer
0x7fb6a0c550db: at ???: RUNTIME ERROR: abort() called

gdb output:

a
free(): invalid pointer

Program received signal SIGABRT, Aborted.
__pthread_kill_implementation (threadid=<optimized out>, signo=6, no_tid=0) at pthread_kill.c:44
44	      return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
(gdb) bt
#0  __pthread_kill_implementation (threadid=<optimized out>, signo=6, no_tid=0) at pthread_kill.c:44
#1  __pthread_kill_internal (threadid=<optimized out>, signo=6) at pthread_kill.c:89
#2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at pthread_kill.c:100
#3  0x00007ffff7d787c8 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff7d5960d in __GI_abort () at abort.c:77
#5  0x00007ffff7d5a8c0 in __libc_message_impl (fmt=fmt@entry=0x7ffff7f0cd63 "%s\n")
    at ../sysdeps/posix/libc_fatal.c:138
#6  0x00007ffff7de8ca7 in malloc_printerr (str=<optimized out>) at malloc.c:5892
#7  0x00007ffff7de8cc1 in malloc_printerr_tail (str=<optimized out>) at malloc.c:5909
#8  0x000000000060b589 in _v_free () at /tmp/v_1000/main.tmp.c:4030
#9  0x0000000000610615 in string_free () at /tmp/v_1000/main.tmp.c:5378
#10 0x0000000000614262 in main__main () at /tmp/v_1000/main.tmp.c:6148
#11 0x000000000061449b in main () at /tmp/v_1000/main.tmp.c:6184

main function:

VV_LOC void main__main(void) {
	byteptr b = ((byteptr)("a"));
	string s = byteptr_vstring(b);
	println(s);
	string_free(&s); // autofreed var main false
}

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.11 487feb9

Environment details (OS name and version, etc.)

V full version V 0.4.11 344f7cc.487feb9
OS linux, "CachyOS"
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 5700G with Radeon Graphics
Memory 5.82GB/13.55GB
V executable /home/dylan/Repos/v/v
V last modified time 2025-09-02 14:05:13
V home dir OK, value: /home/dylan/Repos/v
VMODULES OK, value: /home/dylan/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/dylan
Git version git version 2.51.0
V git status weekly.2025.36-dirty (2 commit(s) behind V master)
.git/config present true
cc version cc (GCC) 15.2.1 20250813
gcc version gcc (GCC) 15.2.1 20250813
clang version clang version 20.1.8
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (GNU libc) 2.42

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions