Some engine fixes

Fix initializing return buffer.
Fix possible array overrun.
Remove unneeded double-initialization of the vars.

This is #834 retargeted to `development-3.6`.