Skip to content

Commit

Permalink
Parser: check argument size on static array parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
wrongnull authored Aug 5, 2024
1 parent 45e41b2 commit 399ac84
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/aro/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub const Options = struct {
@"shift-count-overflow": Kind = .default,
@"constant-conversion": Kind = .default,
@"sign-conversion": Kind = .default,
nonnull: Kind = .default,
};

const Diagnostics = @This();
Expand Down
15 changes: 15 additions & 0 deletions src/aro/Diagnostics/messages.def
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,21 @@ expected_arguments
.extra = .arguments
.kind = .@"error"

callee_with_static_array
.msg = "callee declares array parameter as static here"
.kind = .note

array_argument_too_small
.msg = "array argument is too small; contains {d} elements, callee requires at least {d}"
.extra = .arguments
.kind = .warning
.opt = W("array-bounds")

non_null_argument
.msg = "null passed to a callee that requires a non-null argument"
.kind = .warning
.opt = W("nonnull")

expected_arguments_old
.msg = expected_arguments
.extra = .arguments
Expand Down
19 changes: 18 additions & 1 deletion src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3079,7 +3079,7 @@ fn directDeclarator(p: *Parser, base_type: Type, d: *Declarator, kind: Declarato
arr_ty.len = max_elems;
}
res_ty.data = .{ .array = arr_ty };
res_ty.specifier = .array;
res_ty.specifier = if (static != null) .static_array else .array;
}

try res_ty.combine(outer);
Expand Down Expand Up @@ -7683,6 +7683,23 @@ fn callExpr(p: *Parser, lhs: Result) Error!Result {
continue;
}
const p_ty = params[arg_count].ty;
if (p_ty.specifier == .static_array) {
const arg_array_len: u64 = arg.ty.arrayLen() orelse std.math.maxInt(u64);
const param_array_len: u64 = p_ty.arrayLen().?;
if (arg_array_len < param_array_len) {
const extra = Diagnostics.Message.Extra{ .arguments = .{
.expected = @intCast(arg_array_len),
.actual = @intCast(param_array_len),
} };
try p.errExtra(.array_argument_too_small, param_tok, extra);
try p.errTok(.callee_with_static_array, params[arg_count].name_tok);
}
if (arg.val.isZero(p.comp)) {
try p.errTok(.non_null_argument, param_tok);
try p.errTok(.callee_with_static_array, params[arg_count].name_tok);
}
}

if (call_expr.shouldCoerceArg(arg_count)) {
try arg.coerce(p, p_ty, param_tok, .{ .arg = params[arg_count].name_tok });
}
Expand Down
19 changes: 19 additions & 0 deletions test/cases/array argument is null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//aro-args -std=c23
#include <stddef.h>
void foo(int x[static 10]) {

}

void bar(void) {
foo(NULL);
foo(nullptr);
foo(0);
}

#define EXPECTED_ERRORS "array argument is null.c:8:9: warning: null passed to a callee that requires a non-null argument [-Wnonnull]"\
"stddef.h:17:14: note: expanded from here"\
"array argument is null.c:3:14: note: callee declares array parameter as static here"\
"array argument is null.c:9:9: warning: null passed to a callee that requires a non-null argument [-Wnonnull]"\
"array argument is null.c:3:14: note: callee declares array parameter as static here"\
"array argument is null.c:10:9: warning: null passed to a callee that requires a non-null argument [-Wnonnull]"\
"array argument is null.c:3:14: note: callee declares array parameter as static here"
11 changes: 11 additions & 0 deletions test/cases/array argument too small.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
void foo(int x[static 10]) {

}

void bar(void) {
int x[5];
foo(x);
}

#define EXPECTED_ERRORS "array argument too small.c:7:9: warning: array argument is too small; contains 5 elements, callee requires at least 10 [-Warray-bounds]"\
"array argument too small.c:1:14: note: callee declares array parameter as static here"

0 comments on commit 399ac84

Please sign in to comment.