diff
- while (i < sizeof(buf) && (pg - page < len) &&
+ while (i < bufsize - 1 && (pg - page < len) &&
