Fix CVE-2014-3483 and protect against CVE-2014-3482

This is literally translated patch from http://seclists.org/oss-sec/2014/q3/5 and http://www.openwall.com/lists/oss-security/2014/07/02/10