Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c compilation bug] when using -autofree with json library #23834

Open
AQMpolyface opened this issue Mar 1, 2025 · 2 comments
Open

[c compilation bug] when using -autofree with json library #23834

AQMpolyface opened this issue Mar 1, 2025 · 2 comments

Comments

@AQMpolyface
Copy link

AQMpolyface commented Mar 1, 2025

V version: V 0.4.9 f83af8a, press to see full `v doctor` output
V full version V 0.4.9 f83af8a
OS linux, "CachyOS"
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 Pro 7735U with Radeon Graphics
Memory 7.32GB/30.12GB
V executable /home/polyface/git/v/v
V last modified time 2025-03-01 09:25:10
V home dir OK, value: /home/polyface/git/v
VMODULES OK, value: /home/polyface/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/polyface/Documents/v/bug
Git version git version 2.48.1
V git status weekly.2025.09-23-gf83af8a1
.git/config present true
cc version cc (GCC) 14.2.1 20250207
gcc version gcc (GCC) 14.2.1 20250207
clang version clang version 19.1.7
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (GNU libc) 2.41

What did you do?
./v -g -o vdbg cmd/v && ./vdbg -autofree src/main.v && src/main

// main.v
module main

import json

fn main() {
	input := '{"methode":"test"}'
	decode_message(input) or {}
}

pub fn decode_message(msg string) !string {
	decoded_json := json.decode(BaseMessage, msg.str())!
	return decoded_json.str()
}

type BaseMessage = struct {
	methode string @[json: methode]
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/main.01JN8GDSBDE2C9DZA5SNJJAHF4.tmp.c:6913: error: '_arg_expr_json_decode_1_177' undeclared
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

What did you expect to see?

A normal compilation. The bug only happens when passing -autofree to the compiler.

The bug seems to happen when you call .str() inside json.decode function. If you do something like this below, the bug doesn't happen

s := msg.str()
decoded_json := json.decode(BaseMessage, s)!

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.

Copy link

Connected to Huly®: V_0.6-22239

@AQMpolyface
Copy link
Author

AQMpolyface commented Mar 1, 2025

Also, I get a bug with this one. It seems like if there is any manipulation of the data inside json.decode while using autofree, it blows up.

pub fn decode_message(msg []u8) !(string, []u8) {
	json_content := msg.bytestr().split('\r\n\r\n')
	if json_content.len < 2 {
		return error("Erros: didnt received '\r\n\r\n'")
	}

	decoded_json := json.decode(BaseMessage, json_content[1])!
	return decoded_json.methode, json_content[1].bytes()
}

It is also fixed by using a variable instead of manipulating the data directly inside json.decode

pub fn decode_message(msg []u8) !(string, []u8) {
	json_content := msg.bytestr().split('\r\n\r\n')
	if json_content.len < 2 {
		return error("Erros: didnt received '\r\n\r\n'")
	}
        //note the new variable below
	to_decode := json_content[1]
	decoded_json := json.decode(BaseMessage, to_decode)!
	return decoded_json.methode, json_content[1].bytes()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant