soundness: an assignment to a `const` is accepted, and a `roarray` const is written in place

Aperta Adatta ai principianti
#249 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
compilers

Direzione di ricerca

Inizia dal TODO in lang/check/type.go intorno alle righe 625–634, dove il checker risolve un nome a cui viene assegnato un valore come const; confronta i controlli del parser sugli assignee in lang/parse/parse.go intorno alle righe 960–975. Aggiungi o esegui un test di regressione del checker per entrambi i repro dell’issue, quello con roarray e quello scalare. Il lavoro è concluso quando il checking rifiuta le assegnazioni ai nomi const.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi,

General context of this work is in #188, including the AI-disclaimers.

No in-tree codec is affected by this issue; std never assigns to a const. Claude judges the prospective risk of it as "moderate": assigning to a roarray const writes static const storage, crashing under MSVC and gcc -O0; gcc -O1 drops the write. Assigning to a scalar const generates C that does not compile.

The following is Sol 6.1's writeup of the issue. Includes two repros, generated C, MSVC and gcc driver runs for the array case, and gcc and clang compile errors for the scalar case. The cause is reasoned from source.


The checker accepts an assignment to a const. For a roarray const, cgen emits a memcpy into the static const array; for a scalar const, it emits the constant's value as the assignee.

Minimal repro:

pri const T : roarray[2] base.u8 = [1, 2]

pub struct tester?(
    d : array[2] base.u8,
)

pub func tester.set!() {
    T = this.d
}

Actual: at b36a81d3, wuffs-c gen -package_name repro array.wuffs exits 0 with empty stderr. T is emitted as static const uint8_t WUFFS_REPRO__T[2], and set as:

  memcpy(WUFFS_REPRO__T,self->private_impl.f_d, sizeof(WUFFS_REPRO__T));

MSVC 19.51 warns at that line. A driver that calls set crashes with exit code -1073741819 (0xC0000005):

...\array-release.c(26165): warning C4090: 'function': different 'const' qualifiers
calling set!(), T = {1, 2}
exit code: -1073741819

gcc 13.3 warns passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]. Its -O0 build of the same driver exits with SIGSEGV (139); its -O1 build drops the write and prints returned, T = {1, 2}.

The scalar form, pri const N : base.u32 = 5 with N = 6 in the same function, is also accepted. It generates 5u = 6u;, which neither gcc nor clang compiles:

scalar-release.c:26162:6: error: lvalue required as left operand of assignment
scalar-release.c:26162:6: error: expression is not assignable

Expected: the checker rejects assignments to a const.

Cause: the parser rejects literals and a fixed set of names as assignees, but does not check for const names:

https://github.com/google/wuffs/blob/b36a81d3352604fee1fee3661f39909706b1b966/lang/parse/parse.go#L960-L975

The checker resolves the name to the const and has a TODO for this check:

https://github.com/google/wuffs/blob/b36a81d3352604fee1fee3661f39909706b1b966/lang/check/type.go#L625-L634

Lingua principale
C
Stelle
4.9k
Fork
150
Merge medio
4g 1h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/wuffs

Tutte le issue di google/wuffs

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.