Commit 9fdc3fd8 authored by Zebediah Figura's avatar Zebediah Figura Committed by Alexandre Julliard

wined3d: Introduce a wined3d_bound_range() helper.

parent b2e70ed0
...@@ -3071,7 +3071,7 @@ static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *ifac ...@@ -3071,7 +3071,7 @@ static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *ifac
if (!constants) if (!constants)
return D3DERR_INVALIDCALL; return D3DERR_INVALIDCALL;
if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx) if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
{ {
WARN("Trying to access %u constants, but d3d8 only supports %u.\n", WARN("Trying to access %u constants, but d3d8 only supports %u.\n",
start_idx + count, device->vs_uniform_count); start_idx + count, device->vs_uniform_count);
...@@ -3375,8 +3375,7 @@ static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface ...@@ -3375,8 +3375,7 @@ static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface
TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count); TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
if (!constants || start_idx >= D3D8_MAX_PIXEL_SHADER_CONSTANTF if (!constants || !wined3d_bound_range(start_idx, count, D3D8_MAX_PIXEL_SHADER_CONSTANTF))
|| count > D3D8_MAX_PIXEL_SHADER_CONSTANTF - start_idx)
return WINED3DERR_INVALIDCALL; return WINED3DERR_INVALIDCALL;
wined3d_mutex_lock(); wined3d_mutex_lock();
......
...@@ -3684,7 +3684,7 @@ static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *i ...@@ -3684,7 +3684,7 @@ static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *i
if (!constants) if (!constants)
return D3DERR_INVALIDCALL; return D3DERR_INVALIDCALL;
if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx) if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
{ {
WARN("Trying to access %u constants, but d3d9 only supports %u\n", WARN("Trying to access %u constants, but d3d9 only supports %u\n",
start_idx + count, device->vs_uniform_count); start_idx + count, device->vs_uniform_count);
...@@ -4033,7 +4033,7 @@ static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *if ...@@ -4033,7 +4033,7 @@ static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *if
TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count); TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
if (!constants || start_idx >= WINED3D_MAX_PS_CONSTS_F || count > WINED3D_MAX_PS_CONSTS_F - start_idx) if (!constants || !wined3d_bound_range(start_idx, count, WINED3D_MAX_PS_CONSTS_F))
return WINED3DERR_INVALIDCALL; return WINED3DERR_INVALIDCALL;
wined3d_mutex_lock(); wined3d_mutex_lock();
......
...@@ -5373,8 +5373,8 @@ static HRESULT d3d_device7_SetViewport(IDirect3DDevice7 *iface, D3DVIEWPORT7 *vi ...@@ -5373,8 +5373,8 @@ static HRESULT d3d_device7_SetViewport(IDirect3DDevice7 *iface, D3DVIEWPORT7 *vi
surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv); surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc); wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
if (viewport->dwX > rt_desc.width || viewport->dwWidth > rt_desc.width - viewport->dwX if (!wined3d_bound_range(viewport->dwX, viewport->dwWidth, rt_desc.width)
|| viewport->dwY > rt_desc.height || viewport->dwHeight > rt_desc.height - viewport->dwY) || !wined3d_bound_range(viewport->dwY, viewport->dwHeight, rt_desc.height))
{ {
WARN("Invalid viewport, returning E_INVALIDARG.\n"); WARN("Invalid viewport, returning E_INVALIDARG.\n");
wined3d_mutex_unlock(); wined3d_mutex_unlock();
......
...@@ -414,8 +414,8 @@ static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIE ...@@ -414,8 +414,8 @@ static HRESULT WINAPI d3d_viewport_SetViewport(IDirect3DViewport3 *iface, D3DVIE
surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv); surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc); wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
if (vp->dwX > rt_desc.width || vp->dwWidth > rt_desc.width - vp->dwX if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
|| vp->dwY > rt_desc.height || vp->dwHeight > rt_desc.height - vp->dwY) || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
{ {
WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n"); WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
wined3d_mutex_unlock(); wined3d_mutex_unlock();
...@@ -1043,8 +1043,8 @@ static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVI ...@@ -1043,8 +1043,8 @@ static HRESULT WINAPI d3d_viewport_SetViewport2(IDirect3DViewport3 *iface, D3DVI
surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv); surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc); wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
if (vp->dwX > rt_desc.width || vp->dwWidth > rt_desc.width - vp->dwX if (!wined3d_bound_range(vp->dwX, vp->dwWidth, rt_desc.width)
|| vp->dwY > rt_desc.height || vp->dwHeight > rt_desc.height - vp->dwY) || !wined3d_bound_range(vp->dwY, vp->dwHeight, rt_desc.height))
{ {
WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n"); WARN("Invalid viewport, returning DDERR_INVALIDPARAMS.\n");
wined3d_mutex_unlock(); wined3d_mutex_unlock();
......
...@@ -1837,7 +1837,7 @@ void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_con ...@@ -1837,7 +1837,7 @@ void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_con
TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers); TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
if (start_idx >= MAX_CONSTANT_BUFFERS || count > MAX_CONSTANT_BUFFERS - start_idx) if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
{ {
WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count); WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
return; return;
...@@ -1988,7 +1988,7 @@ void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_devic ...@@ -1988,7 +1988,7 @@ void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_devic
TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views); TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
if (start_idx >= MAX_SHADER_RESOURCE_VIEWS || count > MAX_SHADER_RESOURCE_VIEWS - start_idx) if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
{ {
WARN("Invalid view index %u, count %u.\n", start_idx, count); WARN("Invalid view index %u, count %u.\n", start_idx, count);
return; return;
...@@ -2040,7 +2040,7 @@ void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *co ...@@ -2040,7 +2040,7 @@ void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *co
TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers); TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
if (start_idx >= MAX_SAMPLER_OBJECTS || count > MAX_SAMPLER_OBJECTS - start_idx) if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
{ {
WARN("Invalid sampler index %u, count %u.\n", start_idx, count); WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
return; return;
......
...@@ -61,7 +61,7 @@ HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette, ...@@ -61,7 +61,7 @@ HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette,
if (flags) if (flags)
return WINED3DERR_INVALIDCALL; /* unchecked */ return WINED3DERR_INVALIDCALL; /* unchecked */
if (start > palette->size || count > palette->size - start) if (!wined3d_bound_range(start, count, palette->size))
return WINED3DERR_INVALIDCALL; return WINED3DERR_INVALIDCALL;
if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES) if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES)
......
...@@ -1204,8 +1204,7 @@ HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stat ...@@ -1204,8 +1204,7 @@ HRESULT CDECL wined3d_stateblock_set_vs_consts_f(struct wined3d_stateblock *stat
TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
stateblock, start_idx, count, constants); stateblock, start_idx, count, constants);
if (!constants || start_idx >= d3d_info->limits.vs_uniform_count if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.vs_uniform_count))
|| count > d3d_info->limits.vs_uniform_count - start_idx)
return WINED3DERR_INVALIDCALL; return WINED3DERR_INVALIDCALL;
memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants)); memcpy(&stateblock->stateblock_state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
...@@ -1273,8 +1272,7 @@ HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stat ...@@ -1273,8 +1272,7 @@ HRESULT CDECL wined3d_stateblock_set_ps_consts_f(struct wined3d_stateblock *stat
TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n", TRACE("stateblock %p, start_idx %u, count %u, constants %p.\n",
stateblock, start_idx, count, constants); stateblock, start_idx, count, constants);
if (!constants || start_idx >= d3d_info->limits.ps_uniform_count if (!constants || !wined3d_bound_range(start_idx, count, d3d_info->limits.ps_uniform_count))
|| count > d3d_info->limits.ps_uniform_count - start_idx)
return WINED3DERR_INVALIDCALL; return WINED3DERR_INVALIDCALL;
memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants)); memcpy(&stateblock->stateblock_state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
......
...@@ -132,8 +132,7 @@ static const struct wined3d_format *validate_resource_view(const struct wined3d_ ...@@ -132,8 +132,7 @@ static const struct wined3d_format *validate_resource_view(const struct wined3d_
return NULL; return NULL;
buffer_size = buffer->resource.size / element_size; buffer_size = buffer->resource.size / element_size;
if (desc->u.buffer.start_idx >= buffer_size if (!wined3d_bound_range(desc->u.buffer.start_idx, desc->u.buffer.count, buffer_size))
|| desc->u.buffer.count > buffer_size - desc->u.buffer.start_idx)
return NULL; return NULL;
} }
else else
...@@ -156,11 +155,9 @@ static const struct wined3d_format *validate_resource_view(const struct wined3d_ ...@@ -156,11 +155,9 @@ static const struct wined3d_format *validate_resource_view(const struct wined3d_
if (!desc->u.texture.level_count if (!desc->u.texture.level_count
|| (mip_slice && desc->u.texture.level_count != 1) || (mip_slice && desc->u.texture.level_count != 1)
|| desc->u.texture.level_idx >= texture->level_count || !wined3d_bound_range(desc->u.texture.level_idx, desc->u.texture.level_count, texture->level_count)
|| desc->u.texture.level_count > texture->level_count - desc->u.texture.level_idx
|| !desc->u.texture.layer_count || !desc->u.texture.layer_count
|| desc->u.texture.layer_idx >= depth_or_layer_count || !wined3d_bound_range(desc->u.texture.layer_idx, desc->u.texture.layer_count, depth_or_layer_count))
|| desc->u.texture.layer_count > depth_or_layer_count - desc->u.texture.layer_idx)
return NULL; return NULL;
} }
......
...@@ -2927,6 +2927,17 @@ static inline int wined3d_bit_scan(unsigned int *x) ...@@ -2927,6 +2927,17 @@ static inline int wined3d_bit_scan(unsigned int *x)
return bit_offset; return bit_offset;
} }
static inline bool wined3d_bound_range(unsigned int start, unsigned int count, unsigned int limit)
{
#if defined(__GNUC__) && __GNUC__ >= 5
unsigned int sum;
return !__builtin_add_overflow(start, count, &sum) && sum <= limit;
#else
return start <= limit && count <= limit - start;
#endif
}
static inline void wined3d_box_set(struct wined3d_box *box, unsigned int left, unsigned int top, static inline void wined3d_box_set(struct wined3d_box *box, unsigned int left, unsigned int top,
unsigned int right, unsigned int bottom, unsigned int front, unsigned int back) unsigned int right, unsigned int bottom, unsigned int front, unsigned int back)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment