Skip to content

Commit

Permalink
json: fix json.decode autofree codegen (fix #23834) (#23839)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Mar 2, 2025
1 parent c37309f commit 05a6e55
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2477,9 +2477,10 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
}
} else {
if use_tmp_var_autofree {
n := if node.name == 'json.decode' { i + 2 } else { i + 1 }
// TODO: copypasta, move to an inline fn
fn_name := node.name.replace('.', '_')
name := '_arg_expr_${fn_name}_${i + 1}_${node.pos.pos}'
name := '_arg_expr_${fn_name}_${n}_${node.pos.pos}'
g.write('/*af arg2*/' + name)
} else {
g.expr(arg.expr)
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/gen/c/testdata/autofree_json_decode.c.must_have
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
string _arg_expr_json_decode_2_271 = string_str(msg);
cJSON* _t2 = json__json_parse(/*af arg2*/_arg_expr_json_decode_2_271);
19 changes: 19 additions & 0 deletions vlib/v/gen/c/testdata/autofree_json_decode.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// vtest vflags: -autofree
module main

import json

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

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

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

0 comments on commit 05a6e55

Please sign in to comment.