Menu ▾ ▴

#4225 Variably modified pointer declarations bypass scope, linkage and member constraints

open
nobody
None
Front-end
5
2026-10-24
2026-10-24
No

: Sample code that reproduces the problem.

int n;
int (*p)[n];

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y --nostdinc -S bug.c -o bug.asm

3: SDCC version tested.

SDCC 4.6.4 #17068 (Linux), tested 23 October 2026.

4: Observed and expected behaviour.

SDCC exits successfully without emitting a diagnostic. A diagnostic is required.

The type of p is a pointer to a variable length array, and is therefore variably modified even though the pointer object itself has fixed length. N3886 6.7.7.3 paragraph 2 requires an identifier with variably modified type to be an ordinary identifier, have no linkage, and have block or function prototype scope. This file-scope declaration violates the scope and linkage constraints.

Reference: N3886.

5: Additional checks.

The following variants also compile silently: static int (*p)[n]; at file scope; extern int (*p)[n]; inside void f(int n) with (void)p; to avoid an unrelated unused-variable warning; and a structure member int (*p)[n]; in a structure declared inside void f(int n). Internal linkage does not satisfy the no-linkage constraint. A member name belongs to the structure/union member namespace, not the ordinary-identifier namespace.

All four variants compile silently on five targets in C99, C11, C23 and C2Y: 80 invalid checks. A block-scope ordinary pointer int (*p)[n] = 0; followed by (void)p; is valid and compiles silently. A file-scope VLA typedef is already rejected with error 289. A static VLA object receives error 288 (unsupported VLA object); this does not establish runtime VLA support. GCC 13.3.0 with -std=c2x -pedantic-errors -fsyntax-only rejects all four invalid variants and accepts the valid pointer case.

Targets tested: Z80, MCS51, STM8, PDK14 and MOS65C02. These are compile-time checks. GCC 16.2 and Clang were not tested.

Discussion


Log in to post a comment.