Found by static analysis with PVS-Studio. Original check wasn't actually checking for OOB and would segfault in case of it.