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

Abierto Apto para principiantes
#2,446 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
77/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c, erlang
Área
backend

Línea de trabajo

Lee src/libAtomVM/interop.c alrededor de interop_chardata_fold y los dos NIFs de src/libAtomVM/nifs.c cerca de las líneas 7278 y 7339; sigue cómo los argumentos de nivel superior no válidos llegan a la tupla de resultado. Corrige rest, que no está inicializado, o haz que los NIFs generen badarg para los argumentos que no sean listas ni binarios. El trabajo estará terminado cuando las entradas indicadas que no sean chardata generen badarg y pasen las pruebas Unicode pertinentes.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Erlang
Estrellas
2.3k
Forks
171
Merge medio
5 d 11 h
PR fusionados (30 d)
19

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de atomvm/AtomVM

Todos los issues de atomvm/AtomVM

Issues similares

Más issues de Backend & API Design

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.