This patch should diminish the likelihood of pointer arithmetic overflow
bugs, like the one fixed by
b69c7c5dac.
Change the type of parameter 'out_stride' from int to ptrdiff_t. The
logic is that if you call intel_miptree_map() and use the value of
'out_stride', then you must be doing pointer arithmetic on 'out_ptr'.
Using ptrdiff_t instead of int should make a little bit harder to hit
overflow bugs.
As a side-effect, some function-scope variables needed to be retyped to
avoid compilation errors.
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Signed-off-by: Chad Versace <chad.versace@linux.intel.com>
{
bool same_slice;
void *mapped, *src_mapped, *dst_mapped;
- int src_stride, dst_stride, i, cpp;
+ ptrdiff_t src_stride, dst_stride, cpp;
int map_x1, map_y1, map_x2, map_y2;
GLuint src_bw, src_bh;
src_width /= (int)src_bw;
src_height /= (int)src_bh;
- for (i = 0; i < src_height; ++i) {
+ for (int i = 0; i < src_height; ++i) {
memcpy(dst_mapped, src_mapped, src_width * cpp);
src_mapped += src_stride;
dst_mapped += dst_stride;
struct intel_renderbuffer *irb = intel_renderbuffer(rb);
struct intel_mipmap_tree *mt;
void *map;
- int stride;
+ ptrdiff_t stride;
if (srb->Buffer) {
/* this is a malloc'd renderbuffer (accum buffer), not an irb */
stride = -stride;
}
- DBG("%s: rb %d (%s) mt mapped: (%d, %d) (%dx%d) -> %p/%d\n",
+ DBG("%s: rb %d (%s) mt mapped: (%d, %d) (%dx%d) -> %p/%"PRIdPTR"\n",
__FUNCTION__, rb->Name, _mesa_get_format_name(rb->Format),
x, y, w, h, map, stride);
int height)
{
void *src, *dst;
- int src_stride, dst_stride;
+ ptrdiff_t src_stride, dst_stride;
int cpp = dst_mt->cpp;
intel_miptree_map(brw, src_mt,
BRW_MAP_DIRECT_BIT,
&dst, &dst_stride);
- DBG("sw blit %s mt %p %p/%d -> %s mt %p %p/%d (%dx%d)\n",
+ DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
_mesa_get_format_name(src_mt->format),
src_mt, src, src_stride,
_mesa_get_format_name(dst_mt->format),
return true;
}
+/**
+ * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
+ * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
+ * arithmetic overflow.
+ *
+ * If you call this function and use \a out_stride, then you're doing pointer
+ * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
+ * bugs. The caller must still take care to avoid 32-bit overflow errors in
+ * all arithmetic expressions that contain buffer offsets and pixel sizes,
+ * which usually have type uint32_t or GLuint.
+ */
void
intel_miptree_map(struct brw_context *brw,
struct intel_mipmap_tree *mt,
unsigned int h,
GLbitfield mode,
void **out_ptr,
- int *out_stride)
+ ptrdiff_t *out_stride)
{
struct intel_miptree_map *map;
unsigned int h,
GLbitfield mode,
void **out_ptr,
- int *out_stride);
+ ptrdiff_t *out_stride);
void
intel_miptree_unmap(struct brw_context *brw,
GLuint x, GLuint y, GLuint w, GLuint h,
GLbitfield mode,
GLubyte **map,
- GLint *stride)
+ GLint *out_stride)
{
struct brw_context *brw = brw_context(ctx);
struct intel_texture_image *intel_image = intel_texture_image(tex_image);
struct intel_mipmap_tree *mt = intel_image->mt;
+ ptrdiff_t stride;
/* Our texture data is always stored in a miptree. */
assert(mt);
tex_image->Level + tex_image->TexObject->MinLevel,
slice + tex_image->TexObject->MinLayer,
x, y, w, h, mode,
- (void **)map, stride);
+ (void **)map, &stride);
+
+ *out_stride = stride;
}
static void