"if ((size_t)p - (size_t)kern_buf >= kern_mmap_size) { " should be if ((size_t)p + sizeof(*p) - (size_t)kern_buf >= kern_mmap_size) {