Fix memory leak and NULL-deref in shm_initialize (#8565)
## Summary
Two related bugs in `shm_initialize()` (`csrc/cpu/comm/shm.cpp`), both
found via static analysis and manually verified against the real code
before fixing.
### 1. Memory leak (CWE-401)
```c
workspace_buf = (struct allreduce_workspace*)malloc(sizeof(struct allreduce_workspace));
snprintf(shm_name, NAME_BUF_SIZE, "%s_%d", shm_name_prefix, rank);
shared_create(&allreduce_buffer, shm_name, workspace_buf, sizeof(struct allreduce_workspace));
workspace_buf = (struct allreduce_workspace*)allreduce_buffer.bytes; // original pointer overwritten here
```
`shared_create()` only reads `workspace_buf`'s contents (via a `write()`
call that seeds the shared-memory segment) — it never stores or frees
the pointer it's given. Once `workspace_buf` is reassigned to
`allreduce_buffer.bytes`, the original `malloc()`'d block is unreachable
for the rest of the process. Verified by reading `shared_create()`'s
implementation directly and confirming there is no `free()` call
anywhere in `shm.cpp`.
**Fix:** free the original buffer right after `shared_create()` returns
(its contents have already been copied out at that point), before
`workspace_buf` is repointed at the shared mapping.
### 2. NULL pointer dereference on unchecked calloc (CWE-476)
```c
workspace = (struct allreduce_workspace**)calloc(size, sizeof(struct allreduce_workspace*));
symmetric_buffer[0] = (char**)calloc(size, sizeof(char*));
symmetric_buffer[1] = (char**)calloc(size, sizeof(char*));
distributed_buffer[0] = (char**)calloc(size, sizeof(char*));
distributed_buffer[1] = (char**)calloc(size, sizeof(char*));
// (no NULL check anywhere)
```
All five are dereferenced unconditionally in the loop immediately after
(`workspace[i] = ...`, `workspace[i]->buffer`, `symmetric_buffer[*][i] =
...`, etc.). `calloc()` can return `NULL` under memory pressure; any of
these failing would then be a NULL-pointer write. Verified there is no
existing check on any of these five variables anywhere in the file.
**Fix:** a single guard checking all five allocations before the loop
uses any of them, matching this file's existing fail-and-return style
(`shared_create()`'s own printf-then-return error handling).
## Test plan
- [x] `clang-format` (repo's own `.clang-format`) — no reformatting
needed for either commit, diffs already conform
- [x] `codespell` — clean on both commits
- [x] Manually traced `shared_create()`'s implementation and confirmed
no `free()` anywhere in the file (leak fix)
- [x] Grepped the whole file for any existing NULL check on
`workspace`/`symmetric_buffer`/`distributed_buffer` — none found
(NULL-deref fix)
- [x] Syntax-checked each modified block in isolation against realistic
stand-in types matching the real struct/function shapes (`g++
-fsyntax-only`, clean)
- [ ] Could not build/run the full CUDA/PyTorch extension in this
environment to exercise `shm_initialize()` at runtime — flagging this
limitation explicitly rather than claiming a test run that didn't happen
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: tritsystem <gbranaa4@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Olatunji Ruwase <tunji.ruwase@snowflake.com>
Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>