From e7c1f058d18f62aa4871aec623f994d7b68cb8c1 Mon Sep 17 00:00:00 2001 From: Chad Versace Date: Tue, 1 Feb 2011 10:14:28 -0800 Subject: [PATCH] glsl: Avoid division-by-zero during constant-folding Avoid division-by-zero when constant-folding the following expression types: ir_unop_rsq ir_binop_div ir_binop_mod Fixes bugs: https://bugs.freedesktop.org//show_bug.cgi?id=33306 https://bugs.freedesktop.org//show_bug.cgi?id=33508 Fixes Piglit tests: glslparsertest/glsl2/div-by-zero-01.frag glslparsertest/glsl2/div-by-zero-02.frag glslparsertest/glsl2/div-by-zero-03.frag glslparsertest/glsl2/modulus-zero-01.frag glslparsertest/glsl2/modulus-zero-02.frag NOTE: This is a candidate for the 7.9 and 7.10 branches. --- src/glsl/ir_constant_expression.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp index 321374f..2841fb3 100644 --- a/src/glsl/ir_constant_expression.cpp +++ b/src/glsl/ir_constant_expression.cpp @@ -314,9 +314,13 @@ ir_expression::constant_expression_value() break; case ir_unop_rsq: + /* FINISHME: Emit warning when division-by-zero is detected. */ assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); for (unsigned c = 0; c < op[0]->type->components(); c++) { - data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]); + float s = sqrtf(op[0]->value.f[c]); + if (s == 0) + return NULL; + data.f[c] = 1.0F / s; } break; @@ -511,6 +515,7 @@ ir_expression::constant_expression_value() break; case ir_binop_div: + /* FINISHME: Emit warning when division-by-zero is detected. */ assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); for (unsigned c = 0, c0 = 0, c1 = 0; c < components; @@ -518,12 +523,18 @@ ir_expression::constant_expression_value() switch (op[0]->type->base_type) { case GLSL_TYPE_UINT: + if (op[1]->value.u[c1] == 0) + return NULL; data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; break; case GLSL_TYPE_INT: + if (op[1]->value.i[c1] == 0) + return NULL; data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; break; case GLSL_TYPE_FLOAT: + if (op[1]->value.f[c1] == 0) + return NULL; data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; break; default: @@ -533,6 +544,7 @@ ir_expression::constant_expression_value() break; case ir_binop_mod: + /* FINISHME: Emit warning when division-by-zero is detected. */ assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); for (unsigned c = 0, c0 = 0, c1 = 0; c < components; @@ -540,12 +552,18 @@ ir_expression::constant_expression_value() switch (op[0]->type->base_type) { case GLSL_TYPE_UINT: + if (op[1]->value.u[c1] == 0) + return NULL; data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1]; break; case GLSL_TYPE_INT: + if (op[1]->value.i[c1] == 0) + return NULL; data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1]; break; case GLSL_TYPE_FLOAT: + if (op[1]->value.f[c1] == 0) + return NULL; /* We don't use fmod because it rounds toward zero; GLSL specifies * the use of floor. */ -- 2.7.4