unicode:characters_to_list/binary return an uninitialized term for a non-chardata argument

Aperta Adatta ai principianti
#2,446 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@petermm ci sta già lavorando.

Dal 30/10/2026.

  • #2458 di @petermm — aperta

Valutazione

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

Direzione di ricerca

Leggi src/libAtomVM/interop.c intorno a interop_chardata_fold e le due NIF in src/libAtomVM/nifs.c vicino alle righe 7278 e 7339; segui il percorso degli argomenti di primo livello non validi fino alla tupla del risultato. Correggi rest, che non è inizializzato, oppure fai in modo che le NIF sollevino badarg per gli argomenti che non sono liste né binari. Il lavoro è concluso quando gli input elencati che non sono chardata sollevano badarg e i test Unicode pertinenti passano.

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

Descrizione

Found while reviewing #2357; pre-existing (same behaviour on release-0.7 at 18b35870). BEAM raises badarg, AtomVM returns an {error, _, Rest} tuple whose Rest is an uninitialized stack value:

unicode:characters_to_list(id(foo))         %% BEAM badarg   AtomVM {error,[],<garbage>}
unicode:characters_to_binary(id({1, 2}))    %% BEAM badarg   AtomVM {error,<<>>,<garbage>}
unicode:characters_to_list(id(<<1:1>>))     %% same, once #2357 makes non-byte-aligned bitstrings possible

erlang:display/1 prints the third element as Unknown term type: 129947828638076, and valgrind reports "Conditional jump or move depends on uninitialised value(s)" in term_is_tuple and memory_shallow_copy_term as soon as the tuple is printed or copied by the GC, so a process keeping the result can crash the VM. A non-chardata element inside a list ([foo]) is correctly rejected with badarg; only the top-level argument is affected.

Cause: interop_chardata_fold (src/libAtomVM/interop.c:252-258) returns InteropBadArg for a top-level term that is neither a binary nor a list before rest_fun is ever called, so *rest is never written. interop_chardata_to_bytes maps that result to UnicodeError (the badarg flag is only set by rest_fun), and nif_unicode_characters_to_list (src/libAtomVM/nifs.c:7278-7279) and nif_unicode_characters_to_binary (:7339-7340) build the result tuple with the uninitialized rest.

Fix: validate argv[0] with term_is_list(t) || term_is_binary(t) in the two NIFs and raise badarg otherwise, or have interop_chardata_fold report the top-level term through rest_fun so that interop_chardata_to_bytes_size and interop_chardata_to_bytes return UnicodeBadArg

Lingua principale
Erlang
Stelle
2.3k
Fork
171
Merge medio
5g 11h
PR unite (30g)
19

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 atomvm/AtomVM

Tutte le issue di atomvm/AtomVM

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.