fix bug leading to losing of hash entries in hash_iter_next

Hello,

There is a bug in `hash_iter_next` function, which can lead to skipping of hash elements due to the index `i` becoming invalid under certain conditions. Please review the fix addressing the bug in this pull request. Please comment if you require further information.