[Bug]: Heap corruption / deterministic `free(): invalid pointer` crash in PlayerManager destructor on shutdown (SDK2013 engine target)

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

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
game-dev

Direzione di ricerca

Inizia da PlayerManager::OnClientConnect(), dove m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = client; scrive senza controllare il valore restituito; confrontalo con il guard aggiunto a InvalidatePlayer() nel commit 8c380e4 (issue #1107). Applica lo stesso controllo di limiti/-1 lì, poi ricostruisci il target sdk2013 e conferma che lo spegnimento con MALLOC_CHECK_=3 non interrompe più in PlayerManager::~PlayerManager(). Controlla prima il thread di commenti esistente per le indicazioni dei maintainer sull'ambito.

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

Descrizione

Bug
Prerequisites
  • I have checked that my issue doesn't exist yet in the issue tracker
Operating System and Version

Ubuntu 24

Game / AppID and Version

Source SDK Multiplayer

SourceMod Version

Newest stable

Metamod:Source Version

Newest Stable

Version Verification
Updated SourceMod Version

1.12 - build 7253

Updated Metamod:Source Version

.12 - build 1226:

Description

SourceMod Version

1.12.0.7253 (stock build, sourcemod.2.sdk2013.so)

Description of the problem

On every clean server shutdown/restart (quit, +restart), the server deterministically crashes with:

free(): invalid pointer

Steps to Reproduce

This happens even with zero third-party plugins loaded (clean SourceMod install, only Core) and zero players connected at the time of shutdown. Reproduced with MALLOC_CHECK_=3 set, which confirms this is a genuine invalid/corrupted heap pointer, not a random race.

Backtrace (gdb, no debug symbols but resolved dynamic symbols)

#0 abort () from libc.so.6
#1 ?? () from libc.so.6
#2 ?? () from libc.so.6
#3 ?? () from libc.so.6
#4 free () from libc.so.6
#5 operator delete(void*) () from libstdc++.so.6
#6 operator delete () from libstdc++.so.6
#7 PlayerManager::~PlayerManager () from sourcemod.2.sdk2013.so
#8 __cxa_finalize () from libc.so.6
#9 __do_global_dtors_aux () from sourcemod.2.sdk2013.so
...
#19 UnloadInterface_MMS () from sourcemod_mm_i486.so
#20 Unloader::ReadyToUnload(int) () from metamod.2.sdk2013.so
...
#29 SV_ShutdownGameDLL () from engine_srv.so

The crash occurs on the second delete[] call inside PlayerManager::~PlayerManager() during the global destructor chain triggered by dlclose() on module unload.

Suspected root cause

This looks structurally identical to the historically fixed issue #1107 (unchecked -1 return of engine->GetPlayerUserId() writing out of bounds into m_UserIdLookUp, corrupting the heap chunk header, with the crash surfacing much later at free()).

That specific instance (in InvalidatePlayer()) was fixed in commit 8c380e4 (Oct 2019). However, the same unchecked pattern still exists in PlayerManager::OnClientConnect():

m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = client;

No bounds/-1 check is performed here, same as the originally reported bug. Given this reproduces 100% deterministically on a custom SDK2013-based mod (non-standard edict/userid handling), it's plausible this specific engine target hits the -1 edge case reliably where stock games rarely do.

Suggested fix

Apply the same guard as in the #1107 fix:

int userid = engine->GetPlayerUserId(pEntity);
if (userid >= 0 && userid <= USHRT_MAX)
{
    m_UserIdLookUp[userid] = client;
}

Environment

  • Engine target: SDK2013 (custom mod, "sgm_beta")
  • OS: Linux (i386/x86 32-bit binaries)
  • Reproducible on every shutdown, independent of player count/session activity
Relevant Log Output

Lingua principale
C++
Stelle
1.2k
Fork
476
Merge medio
2g 9h
PR unite (30g)
13

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 alliedmodders/sourcemod

Tutte le issue di alliedmodders/sourcemod

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.