Fixing fs#317

This should fix the issue of fs#317, avoiding to access memory out of the allocated area and thus avoiding the crash.