[LIBS] Check remainder before de-referencing

Signed-off-by: Anup Patel <anup@brainfault.org>
[TESTS] Check remainder before de-referencing

Signed-off-by: Anup Patel <anup@brainfault.org>