Added check for out of bound access to valgrind

Signed-off-by: Shahbaz Youssefi <ShabbyX@gmail.com>