aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2026-03-19 20:02:09 +0100
committerlemon <lsof@mailbox.org>2026-03-19 20:02:09 +0100
commit0e1a2a18ac7056e7fe3a033b664e6ecb8faba651 (patch)
tree470f8e423f5e4ab10faf5e9c4e294121b7cac0dc
parent05e132851016d566629fcfd279eef025a14ddb32 (diff)
c: fix qualifiers for array declarators
`const int x[]` was being treated as `const int x[const]`, which is wrong, and matters when `x` is a function parameter that really decays to a pointer (`int const *const x`)
-rw-r--r--src/c.c17
-rw-r--r--test/17-misc.c6
2 files changed, 16 insertions, 7 deletions
diff --git a/src/c.c b/src/c.c
index 92f917e..c8a040b 100644
--- a/src/c.c
+++ b/src/c.c
@@ -2525,8 +2525,10 @@ static struct DeclList {
DeclList *prev, *next;
uchar t; /* TYPTR, TYARRAY or TYFUNC */
union {
- uchar qual; /* TYPTR */
- Expr count; /* TYARRAY */
+ struct {
+ uchar qual; /* TYPTR, TYARRAY */
+ Expr count; /* TYARRAY */
+ };
struct { /* TYFUNC */
Type *param;
internstr *pnames;
@@ -2623,14 +2625,14 @@ decltypes(CComp *cm, DeclList *list, internstr *name, Span *span, Span *namespan
for (;;) {
if (match(cm, &tk, '[')) {
node.span = tk.span;
- int q = 0;
+ node.qual = 0;
bool statik = 0;
if (in_range(peek(cm, &tk), TKWBEGIN_, TKWEND_)) {
- q = cvqual(cm);
+ node.qual = cvqual(cm);
statik = match(cm, NULL, TKWstatic);
- q |= cvqual(cm);
+ node.qual |= cvqual(cm);
}
- (void)q, (void)statik; /* stub */
+ (void)statik; /* stub */
if (match(cm, &tk, ']')) {
node.count.t = EARRAYUNSIZED;
@@ -2727,7 +2729,7 @@ static Decl
declarator(DeclState *st, CComp *cm, Span span0) {
Decl decl = { st->base, st->scls, .qual = st->qual, .span = span0 };
DeclList list = { &list, &list }, *l;
- Span namespan ={0};
+ Span namespan = {0};
static bool inidecltmp;
if (!inidecltmp) {
inidecltmp = 1;
@@ -2776,6 +2778,7 @@ declarator(DeclState *st, CComp *cm, Span span0) {
}
decl.ty = mkarrtype(decl.ty, decl.qual, n);
}
+ decl.qual = l->qual;
break;
case TYFUNC:
if (decl.ty.t == TYFUNC)
diff --git a/test/17-misc.c b/test/17-misc.c
index cec45b4..e6f9d11 100644
--- a/test/17-misc.c
+++ b/test/17-misc.c
@@ -52,6 +52,12 @@ struct S3 {
char q;
};
+int array_const_decay(const char x[3], int i) {
+ static const char y[3] = "11";
+ if (!x) x = y;
+ return x[i];
+}
+
extern int printf(const char *, ...);
#include <assert.h>
#include <string.h>