Fix keys replacing.

Suggested by:	Bruno Lauze
Reserve space for NULL character.
Check boundaries when processing comments.
Fix memory leak in expanding variables.
Fix memory leak in keys replacing.
Update to 0.5.1