Safeguard against bad memory access

Change-Id: I3cac83082fe88b90aaf11a0c5c52232a4ad2aeec