Files
Bubberstation/code/controllers/subsystem/lighting.dm
SkyratBot 93a02f7e21 [MIRROR] Fix out of bounds in lighting subsystem [MDB IGNORE] (#21413)
* Fix out of bounds in lighting subsystem (#75018)

## About The Pull Request
Fixes #74697

Look at this for loop

bfba2c5934/code/controllers/subsystem/lighting.dm (L34-L39)

Now look at update corners

bfba2c5934/code/modules/lighting/lighting_source.dm (L428-L430)

Now look at refresh values. this proc has a chance to delete itself here

bfba2c5934/code/modules/lighting/lighting_source.dm (L315-L318)
And here

bfba2c5934/code/modules/lighting/lighting_source.dm (L331-L334)

Now look at the Destroy proc, specifically focus on the needs_update
condition

bfba2c5934/code/modules/lighting/lighting_source.dm (L66-L67)

We are removing the light from the subsystem source queue while the
subsystem is still iterating through them causing big problems for this
for loop

bfba2c5934/code/controllers/subsystem/lighting.dm (L33-L37)

which causes the out of bounds exception because loop variable `i` is
not updated accordingly when this list size is reduced mid iteration.

The solution? we have to move the loop variable `i` back whenever the
source is deleted i.e. removed from the list so we don't overflow

## Changelog

🆑
fix: out of bounds when updating lights in lighting subsystem
/🆑

---------

Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@ users.noreply.github.com>

* Fix out of bounds in lighting subsystem

---------

Co-authored-by: SyncIt21 <110812394+SyncIt21@users.noreply.github.com>
Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@ users.noreply.github.com>
2023-05-25 17:22:43 -07:00

116 lines
2.9 KiB
Plaintext

SUBSYSTEM_DEF(lighting)
name = "Lighting"
wait = 2
init_order = INIT_ORDER_LIGHTING
flags = SS_TICKER
var/static/list/sources_queue = list() // List of lighting sources queued for update.
var/static/list/corners_queue = list() // List of lighting corners queued for update.
var/static/list/objects_queue = list() // List of lighting objects queued for update.
#ifdef VISUALIZE_LIGHT_UPDATES
var/allow_duped_values = FALSE
var/allow_duped_corners = FALSE
#endif
/datum/controller/subsystem/lighting/stat_entry(msg)
msg = "L:[length(sources_queue)]|C:[length(corners_queue)]|O:[length(objects_queue)]"
return ..()
/datum/controller/subsystem/lighting/Initialize()
if(!initialized)
create_all_lighting_objects()
initialized = TRUE
fire(FALSE, TRUE)
return SS_INIT_SUCCESS
/datum/controller/subsystem/lighting/fire(resumed, init_tick_checks)
MC_SPLIT_TICK_INIT(3)
if(!init_tick_checks)
MC_SPLIT_TICK
var/list/queue
var/i = 0
// UPDATE SOURCE QUEUE
queue = sources_queue
while(i < length(queue)) //we don't use for loop here because i cannot be changed during an iteration
i += 1
var/datum/light_source/L = queue[i]
L.update_corners()
if(!QDELETED(L))
L.needs_update = LIGHTING_NO_UPDATE
else
i -= 1 // update_corners() has removed L from the list, move back so we don't overflow or skip the next element
// We unroll TICK_CHECK here so we can clear out the queue to ensure any removals/additions when sleeping don't fuck us
if(init_tick_checks)
if(!TICK_CHECK)
continue
queue.Cut(1, i + 1)
i = 0
stoplag()
else if(MC_TICK_CHECK)
break
if(i)
queue.Cut(1, i + 1)
i = 0
if(!init_tick_checks)
MC_SPLIT_TICK
// UPDATE CORNERS QUEUE
queue = corners_queue
while(i < length(queue)) //we don't use for loop here because i cannot be changed during an iteration
i += 1
var/datum/lighting_corner/C = queue[i]
C.needs_update = FALSE //update_objects() can call qdel if the corner is storing no data
C.update_objects()
// We unroll TICK_CHECK here so we can clear out the queue to ensure any removals/additions when sleeping don't fuck us
if(init_tick_checks)
if(!TICK_CHECK)
continue
queue.Cut(1, i + 1)
i = 0
stoplag()
else if(MC_TICK_CHECK)
break
if(i)
queue.Cut(1, i+1)
i = 0
if(!init_tick_checks)
MC_SPLIT_TICK
// UPDATE OBJECTS QUEUE
queue = objects_queue
while(i < length(queue)) //we don't use for loop here because i cannot be changed during an iteration
i += 1
var/datum/lighting_object/O = queue[i]
if(QDELETED(O))
continue
O.update()
O.needs_update = FALSE
// We unroll TICK_CHECK here so we can clear out the queue to ensure any removals/additions when sleeping don't fuck us
if(init_tick_checks)
if(!TICK_CHECK)
continue
queue.Cut(1, i + 1)
i = 0
stoplag()
else if(MC_TICK_CHECK)
break
if(i)
queue.Cut(1, i + 1)
/datum/controller/subsystem/lighting/Recover()
initialized = SSlighting.initialized
..()