Commit 747a8ab
committed
command: fix OOM leaks and NULL-deref in command_{network,stdin,emscripten}_new
Three sibling functions that construct command_t handles
(command_network_new, command_stdin_new, command_emscripten_new)
all followed the same broken allocation order:
cmd = calloc(...);
inner = calloc(...);
if (!cmd)
return NULL;
if (!inner) { free(cmd); return NULL; }
If cmd's calloc fails but inner's succeeds, the '!cmd' early return
leaks inner. Likewise in command_network_new, whose error handler
was actually fine at the end of the function, the code before it
did
netcmd->net_fd = fd;
cmd->userptr = netcmd;
with no NULL check between the two callocs and the assignments - an
OOM on either calloc would segfault on the very next line, never
reaching the error label.
The sibling command_uds_new already uses the correct sequential
pattern: allocate, check, allocate, check-with-cleanup-of-first.
Apply that pattern to the other three.
command_network_new also gets one unrelated but same-function fix:
the 'error:' label's teardown did not call socket_close() on fd,
so the socket_nonblock / socket_bind failure paths (which run AFTER
fd was opened and assigned into netcmd->net_fd) leaked the socket
file descriptor. Moved fd into a local initialised to -1 so the
single 'if (fd >= 0) socket_close(fd)' branch in the error handler
covers every exit path that had a real fd, without relying on
netcmd being non-NULL.
Also moves the 'socket_init' call after the two callocs rather than
interleaved with them. This has no behavioural consequence -
socket_init's side effects (network_init, getaddrinfo) are
idempotent / reversible via freeaddrinfo_retro - but keeps the
allocate-check-allocate-check block together, which is what the
whole refactor is aiming for in the first place.
No scope for OOM-on-small-calloc to actually hit these paths in
current practice (all three functions run once at RetroArch startup,
not per-command-message), so this is correctness-only rather than a
user-visible fix. Still worth doing - the NULL-deref in particular
is a real bug waiting for the right-shaped OOM.1 parent a36d996 commit 747a8ab
1 file changed
Lines changed: 42 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
243 | 258 | | |
244 | 259 | | |
245 | 260 | | |
| |||
271 | 286 | | |
272 | 287 | | |
273 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
274 | 295 | | |
275 | 296 | | |
276 | 297 | | |
| |||
351 | 372 | | |
352 | 373 | | |
353 | 374 | | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
358 | 381 | | |
359 | | - | |
| 382 | + | |
| 383 | + | |
360 | 384 | | |
361 | 385 | | |
362 | 386 | | |
363 | 387 | | |
| 388 | + | |
364 | 389 | | |
365 | 390 | | |
366 | 391 | | |
| |||
402 | 427 | | |
403 | 428 | | |
404 | 429 | | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
409 | 435 | | |
410 | | - | |
| 436 | + | |
| 437 | + | |
411 | 438 | | |
412 | 439 | | |
413 | 440 | | |
414 | 441 | | |
| 442 | + | |
415 | 443 | | |
416 | 444 | | |
417 | 445 | | |
| |||
0 commit comments