
< while (buf[len - 1] == 0x20) len--;
---
> while (len > 0 && buf[len - 1] == 0x20) len--;
