[Mesa-dev] [PATCH 06/10] mesa: Prevent negative indexing on noise2, noise3 and noise4

Ian Romanick idr at freedesktop.org
Mon Feb 17 13:16:11 PST 2014


On 02/17/2014 08:21 AM, Juha-Pekka Heikkila wrote:
> % operator could return negative value which would cause
> indexing before perm table.
> 
> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila at gmail.com>
> ---
>  src/mesa/program/prog_noise.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/src/mesa/program/prog_noise.c b/src/mesa/program/prog_noise.c
> index c258c5e..12bb4d9 100644
> --- a/src/mesa/program/prog_noise.c
> +++ b/src/mesa/program/prog_noise.c
> @@ -282,8 +282,8 @@ _mesa_noise2(GLfloat x, GLfloat y)
>     y2 = y0 - 1.0f + 2.0f * G2;
>  
>     /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
> -   ii = i % 256;
> -   jj = j % 256;
> +   ii = abs(i % 256);
> +   jj = abs(j % 256);

It seems like

   ii = i & 0x0ff;

is a better way to do this.  Perhaps add a comment about 'int % int'
possibly returning NULL so that someone doesn't come a long and change
it back. :)

At that point, ii, jj, i1, and j1 should probably be unsigned.  I think
only i and j need to be int.

>     /* Calculate the contribution from the three corners */
>     t0 = 0.5f - x0 * x0 - y0 * y0;
> @@ -423,9 +423,9 @@ _mesa_noise3(GLfloat x, GLfloat y, GLfloat z)
>     z3 = z0 - 1.0f + 3.0f * G3;
>  
>     /* Wrap the integer indices at 256 to avoid indexing perm[] out of bounds */
> -   ii = i % 256;
> -   jj = j % 256;
> -   kk = k % 256;
> +   ii = abs(i % 256);
> +   jj = abs(j % 256);
> +   kk = abs(k % 256);
>  
>     /* Calculate the contribution from the four corners */
>     t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
> @@ -573,10 +573,10 @@ _mesa_noise4(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
>     w4 = w0 - 1.0f + 4.0f * G4;
>  
>     /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
> -   ii = i % 256;
> -   jj = j % 256;
> -   kk = k % 256;
> -   ll = l % 256;
> +   ii = abs(i % 256);
> +   jj = abs(j % 256);
> +   kk = abs(k % 256);
> +   ll = abs(l % 256);
>  
>     /* Calculate the contribution from the five corners */
>     t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
> 



More information about the mesa-dev mailing list