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

cgen: cast 'vstring'.str to char * if it is the expected type #19827

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions vlib/net/mbedtls/mbedtls.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ fn C.mbedtls_entropy_free(&C.mbedtls_entropy_context)

fn C.mbedtls_ssl_config_defaults(&C.mbedtls_ssl_config, int, int, int) int

fn C.mbedtls_x509_crt_parse(&C.mbedtls_x509_crt, &char, int) int
fn C.mbedtls_pk_parse_key(&C.mbedtls_pk_context, &char, int, &char, int, voidptr, voidptr) int
fn C.mbedtls_x509_crt_parse(&C.mbedtls_x509_crt, &u8, int) int
fn C.mbedtls_pk_parse_key(&C.mbedtls_pk_context, &u8, int, &char, int, voidptr, voidptr) int
Comment on lines -166 to +167
Copy link
Member Author

@ttytm ttytm Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing this to use &u8 where types are declared as unsigned char * allows for green CI.

int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
const unsigned char *key, size_t keylen,
const unsigned char *pwd, size_t pwdlen,

fn C.mbedtls_x509_crt_parse_file(&C.mbedtls_x509_crt, &char) int
fn C.mbedtls_pk_parse_keyfile(&C.mbedtls_pk_context, &char, &char, voidptr, voidptr) int

fn C.mbedtls_net_connect(&C.mbedtls_net_context, &u8, &u8, int) int
fn C.mbedtls_net_connect(&C.mbedtls_net_context, &char, &char, int) int

fn C.mbedtls_net_bind(&C.mbedtls_net_context, voidptr, &u8, int) int
fn C.mbedtls_net_bind(&C.mbedtls_net_context, voidptr, &char, int) int
fn C.mbedtls_net_accept(&C.mbedtls_net_context, &C.mbedtls_net_context, voidptr, int, voidptr) int
fn C.mbedtls_ssl_session_reset(&C.mbedtls_ssl_context)
fn C.mbedtls_ssl_conf_authmode(&C.mbedtls_ssl_config, int)
Expand All @@ -188,8 +188,8 @@ fn C.mbedtls_ssl_set_bio(&C.mbedtls_ssl_context, &C.mbedtls_net_context, voidptr

fn C.mbedtls_ssl_handshake(&C.mbedtls_ssl_context) int

fn C.mbedtls_ssl_read(&C.mbedtls_ssl_context, &char, int) int
fn C.mbedtls_ssl_write(&C.mbedtls_ssl_context, &char, int) int
fn C.mbedtls_ssl_read(&C.mbedtls_ssl_context, &u8, int) int
fn C.mbedtls_ssl_write(&C.mbedtls_ssl_context, &u8, int) int

fn C.mbedtls_high_level_strerr(int) &char

Expand Down
19 changes: 11 additions & 8 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2544,14 +2544,17 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
g.write('->val')
return
}
if got_is_ptr && !expected_is_ptr && neither_void && exp_sym.kind != .placeholder
&& expr !is ast.InfixExpr {
got_deref_type := got_type.deref()
deref_sym := g.table.sym(got_deref_type)
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result)
if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() {
g.write('*')
if got_is_ptr && neither_void && exp_sym.kind != .placeholder && expr !is ast.InfixExpr {
if !expected_is_ptr {
got_deref_type := got_type.deref()
deref_sym := g.table.sym(got_deref_type)
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result)
if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() {
g.write('*')
}
} else if expr !is ast.StringLiteral && exp_sym.kind == .char && got_sym.kind == .u8 {
g.write('(${exp_styp})')
}
}
if expr is ast.IntegerLiteral {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/testdata/c_fn_args.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barbaz
9 changes: 9 additions & 0 deletions vlib/v/gen/c/testdata/c_fn_args.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn foo(bar &char, baz &char) {
println(unsafe { bar.vstring() + baz.vstring() })
}

fn main() {
baz := 'baz'.str
// Should compile with `-cc [gcc/clang] -cstrict`
foo('bar'.str, baz)
}
Comment on lines +1 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should not compile with -cstrict. Such hacks are brittle.

It should need explicit casts, like this: foo(&char('bar'.str), &char(baz)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why should the user do the explicit cast with flags? I feel they don't need to do it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what flags are you talking about?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-cstrict

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should have done the explicit cast in all cases, -cstrict just makes the C compilers that support it much more strict, and makes them produce more errors for situations, that normally they accept.

Loading