Fixes issue 3258 by changing certain calculations made with int types to ptrdiff_t whenever appropriate.