--- samba-3.2.6-orig/source/lib/xfile.c 2008-12-10 03:12:23.000000000 -0800 +++ samba-3.2.6/source/lib/xfile.c 2008-12-13 13:07:38.000000000 -0800 @@ -356,10 +356,21 @@ size_t x_fread(void *p, size_t size, siz { size_t total = 0; while (total < size*nmemb) { - int c = x_fgetc(f); - if (c == EOF) break; - (total+(char *)p)[0] = (char)c; - total++; + if (f->flags & (X_FLAG_EOF | X_FLAG_ERROR)) break; + + if (f->bufused == 0) x_fillbuf(f); + + if (f->bufused == 0) { + f->flags |= X_FLAG_EOF; + break; + } + + size_t nbytes = MIN(size*nmemb - total, f->bufused); + memcpy(p, f->next, nbytes); + f->next += nbytes; + f->bufused -= nbytes; + total += nbytes; + p += nbytes; } return total/size; }