Skip to content

Commit

Permalink
Fix sizeof bug, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Nov 7, 2023
1 parent 302f066 commit 5a1f8b1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ncc/examples/telnet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void on_new_conn(u64 socket_id)
puts("got new connection");

char client_addr[128];
u64 conn_sock = net_accept(socket_id, client_addr, 128, on_incoming_data);
u64 conn_sock = net_accept(socket_id, client_addr, sizeof(client_addr), on_incoming_data);
}

void on_incoming_data(u64 socket_id, u64 num_bytes)
Expand Down
5 changes: 5 additions & 0 deletions ncc/src/typedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ impl Stmt
// Local variable declaration
Stmt::VarDecl { var_type, var_name, init_expr } => {
resolve_types(var_type, typedefs, None)?;

if let Some(expr) = init_expr {
expr.resolve_types(typedefs)?;
}
}

Stmt::Block(stmts) => {
Expand Down Expand Up @@ -235,6 +239,7 @@ impl Expr
}

Expr::SizeofType { t } => {
// If this is a reference to a named type
if let Type::Named(name) = t {
if let Some(dt) = typedefs.get(name) {
*t = (**dt).borrow().clone();
Expand Down
7 changes: 7 additions & 0 deletions ncc/tests/arrays.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdint.h>
#include <stddef.h>

int int_array[3] = { 7, 1, 2 };

Expand Down Expand Up @@ -51,5 +52,11 @@ int main()
array2d[0][0] = 1;
assert(array2d[0][0] == 1);

// Regression: sizeof of local array inside var decl
char buf[128];
sizeof(buf);
size_t sz = sizeof(buf);
assert(sz == 128);

return 0;
}

0 comments on commit 5a1f8b1

Please sign in to comment.