peres: Fix an off-by-one heap overflow in saveResource.

- Fix an off-by-one heap overflow in saveResource.
- Add missing checkings for NULL pointer.