Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

[BUG] Windows: getErrorMessage() reads uninitialized pointer when FormatMessage fails

Abierto Apto para principiantes
#3,714 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
74/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
cpp

Línea de trabajo

Comienza con src/backend/common/module_loading_windows.cpp:31-41 e inspecciona la llamada a FormatMessage y sus invocadores en src/api/unified/symbol_manager.cpp y en los demás archivos de módulo indicados. Verifica la ruta de fallo antes de construir el string y la liberación de la asignación en la ruta exitosa; se considera terminado cuando un formateo fallido ya no desreferencia un puntero no inicializado y las llamadas exitosas no dejan fugas del búfer de LocalAlloc.

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

Descripción

bug

On Windows, getErrorMessage() constructs a std::string from lpMsgBuf without checking whether FormatMessage actually wrote to it. When FormatMessage fails, lpMsgBuf is still uninitialized and std::string's constructor runs strlen on an indeterminate stack value.

It also leaks the LocalAlloc'd buffer on the success path.

Description

src/backend/common/module_loading_windows.cpp:31-41:

string getErrorMessage() {
    const char* lpMsgBuf;                    // uninitialized
    DWORD dw = GetLastError();

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
                      FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                  (LPTSTR)&lpMsgBuf, 0, NULL);
    string error_message(lpMsgBuf);          // no check, no LocalFree
    return error_message;
}

FormatMessage returns 0 and does not touch the output pointer when the message cannot be formatted — which happens for error codes with no entry in the system message table (common for loader and NTSTATUS-derived codes, and for codes left behind by a DLL whose DllMain failed). The return value is discarded here, so lpMsgBuf is then dereferenced regardless.

FORMAT_MESSAGE_ALLOCATE_BUFFER also requires the caller to LocalFree the buffer; that never happens, so every successful call leaks.

Why this is on a hot path

getErrorMessage() is called on every failed LoadLibrary during backend probing, src/api/unified/symbol_manager.cpp:

AF_TRACE("Failed to load {}", getErrorMessage());

AF_TRACE evaluates its arguments unconditionally — the spdlog level filter applies after the call — so this runs even with tracing disabled. The unified loader tries roughly a dozen path prefixes across each backend, so on a typical process start most of those attempts fail and this function runs tens of times before any user code executes.

Other callers include src/backend/cuda/cusparseModule.cpp, src/backend/cuda/cudnnModule.cpp, and src/backend/common/graphics_common.cpp.

Impact

Reading an indeterminate pointer produces either a garbage error string or STATUS_ACCESS_VIOLATION (0xC0000005), during library initialization and before any user code runs. Intermittent, since it depends on the stack residue at that address.

Reproducible Code and/or Steps

System Information

Checklist

  • Using the latest available ArrayFire release
  • GPU drivers are up to date

Disclaimer

Found by Claude Opus 5. The prompt was to look for potential sources for 0xC0000005 errors on Windows.

Lenguaje dominante
C++
Estrellas
4.9k
Forks
555
Merge medio
1 h 24 min
PR fusionados (30 d)
1

Guía de contribución

Abrir la guía de contribución

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 arrayfire/arrayfire

Todos los issues de arrayfire/arrayfire

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.