The issue seems to be caused by the integer overflow. It's actually still needed to investigate why exactly buffer contained such a huge value, but the patch is still legit and seems to be solving the issue just nicely.