Commit 9faa654
committed
network/netplay/netplay_room_parse: NULL-check inner per-room callocs
The two per-room struct netplay_room callocs inside the
netplay_json_start_object rjson callback were unchecked; on
OOM subsequent code in the same function (->connectable = true,
->is_retroarch = true) and every field-handler callback below
(object_member, string, number, boolean) would NULL-deref
net_st->rooms_data->cur or NULL-deref the wrong field via
'&net_st->rooms_data->cur->port' style address-of expressions.
This finishes off a deferred item called out in the commit
message for '3785495 scattered OOM fixes: core_backup entries
leak, netplay core_netpacket_interface and rooms_data' (this
series), which added the outer rooms_data NULL-check but left
the inner per-room allocations noted as out-of-scope.
=== The original code ===
static bool netplay_json_start_object(void* ctx)
{
if (p_ctx->state == STATE_FIELDS_START)
{
p_ctx->state = STATE_FIELDS_OBJECT_START;
if (!net_st->rooms_data->head)
{
net_st->rooms_data->head = calloc(1, ...); /* A */
net_st->rooms_data->cur = net_st->rooms_data->head;
}
else if (!net_st->rooms_data->cur->next)
{
net_st->rooms_data->cur->next = calloc(1, ...); /* B */
net_st->rooms_data->cur = net_st->rooms_data->cur->next;
}
net_st->rooms_data->cur->connectable = true; /* NULL-deref */
net_st->rooms_data->cur->is_retroarch = true; /* NULL-deref */
}
...
}
Two ways this goes wrong on OOM:
1. calloc A or B returns NULL -> cur becomes NULL -> the two
immediate field writes at the bottom of this if-block
NULL-deref.
2. If a *previous* room's calloc failed, cur is NULL coming into
this call. The 'else if (!cur->next)' branch NULL-derefs
cur->next before we even get to the calloc.
Every downstream callback (object_member, string, number,
boolean) then accesses 'net_st->rooms_data->cur->fieldname'
unconditionally and NULL-derefs too.
=== The fix ===
1. Add a new parser state STATE_SKIP_OBJECT to the
netplay_parse_state enum. When the inner calloc fails or
when cur is NULL from a prior failure, transition into
SKIP_OBJECT instead of STATE_FIELDS_OBJECT_START.
2. Every field-handler callback (boolean, string, number,
object_member) already gates on state ==
STATE_FIELDS_OBJECT_START, so they silently skip when state
is SKIP_OBJECT. No changes needed in those handlers.
3. netplay_json_end_object transitions both
STATE_FIELDS_OBJECT_START and STATE_SKIP_OBJECT back to
STATE_ARRAY_START, so the next JSON object in the rooms
array can be attempted.
4. Restructure start_object to use a local 'struct netplay_room
*new_room' temporary so the calloc return can be NULL-checked
before wiring it into head/cur. Guard the 'else if' branch
on cur being non-NULL.
=== Self-healing behaviour ===
The fix lets the parser self-heal across rooms:
* If the first room's calloc fails: head stays NULL. The
next iteration of start_object sees !head true and
retries calloc. If it succeeds, the new room becomes head
as normal.
* If a later room's calloc fails: cur points to the last
successfully-allocated room, which still has next==NULL.
The next iteration re-enters the 'else if (cur &&
!cur->next)' branch, retries calloc, and links the new
room in as if the failed room never existed. No holes
in the linked list; only the failed room is dropped.
=== netplay_rooms_free robustness ===
Walks the linked list from head, freeing each room. Handles
head==NULL (no rooms were allocatable) and partial lists (some
rooms succeeded, some failed) correctly - it just walks what
was successfully linked in. No changes needed.
=== Thread-safety ===
netplay_rooms_parse is called on the main thread (from task
callbacks in menu_cbs_ok.c after an HTTP fetch completes).
The rjson parser runs synchronously on the same thread. No
new locking required.
=== Reachability ===
Every netplay lobby browse triggers this parser with the
server response. OOM here is plausible on memory-pressured
handheld targets (Retroid, Anbernic, etc) where users are
most likely to be browsing for netplay opponents. The pre-
patch behaviour was a segfault on the first unchecked field
write; the post-patch behaviour is a partially-populated
rooms list (with the failing rooms silently dropped).
=== Verification ===
Compile-tested with:
gcc -c -I. -Iinclude -Ilibretro-common/include \
-DHAVE_CONFIG_H -DRARCH_INTERNAL -DHAVE_NETWORKING \
network/netplay/netplay_room_parse.c
No warnings or errors. State machine walked through by hand
for: empty JSON, one-room success, two-room success, first-
room OOM, second-room OOM followed by successful third room.1 parent 2f048a4 commit 9faa654
1 file changed
Lines changed: 61 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
36 | 44 | | |
37 | 45 | | |
38 | 46 | | |
| |||
98 | 106 | | |
99 | 107 | | |
100 | 108 | | |
101 | | - | |
| 109 | + | |
102 | 110 | | |
103 | 111 | | |
104 | 112 | | |
105 | | - | |
106 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
107 | 135 | | |
108 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
109 | 146 | | |
110 | | - | |
111 | | - | |
| 147 | + | |
| 148 | + | |
112 | 149 | | |
113 | 150 | | |
| 151 | + | |
114 | 152 | | |
115 | 153 | | |
116 | 154 | | |
| |||
124 | 162 | | |
125 | 163 | | |
126 | 164 | | |
127 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
128 | 170 | | |
129 | 171 | | |
130 | 172 | | |
| |||
288 | 330 | | |
289 | 331 | | |
290 | 332 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
299 | 341 | | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
305 | 346 | | |
306 | 347 | | |
307 | 348 | | |
| |||
0 commit comments