diff options
author | Pierre Joye <pierre.php@gmail.com> | 2021-08-26 22:40:15 +0700 |
---|---|---|
committer | Pierre Joye <pierre.php@gmail.com> | 2021-08-26 22:40:15 +0700 |
commit | 13d3b67822f9e8b2f6cfc96933abeef054d37085 (patch) | |
tree | ec7383b9e604691603727ea5b786dfb2bbba9d58 | |
parent | 63070b49e2f7380b11bf8d2f6f8cdf49cbe8076d (diff) | |
download | libgd-13d3b67822f9e8b2f6cfc96933abeef054d37085.tar.gz |
Fix #402, negative determinant fails, only 0 or not finite should fail
-rw-r--r-- | src/gd_matrix.c | 5 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/gdmatrix/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tests/gdmatrix/bug00402.c | 30 |
4 files changed, 40 insertions, 1 deletions
diff --git a/src/gd_matrix.c b/src/gd_matrix.c index def3f3c..7ee2e6f 100644 --- a/src/gd_matrix.c +++ b/src/gd_matrix.c @@ -84,7 +84,10 @@ BGD_DECLARE(int) gdAffineInvert (double dst[6], const double src[6]) { double r_det = (src[0] * src[3] - src[1] * src[2]); - if (fabs(r_det) <= 0.0) { + if (!isfinite(r_det)) { + return GD_FALSE; + } + if (r_det == 0) { return GD_FALSE; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 51f09c4..25a19ba 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -78,6 +78,7 @@ if (BUILD_TEST) gdimagestringup16 gdimagetruecolortopalette gdinterpolatedscale + gdmatrix gdnewfilectx gdtest gdtiled diff --git a/tests/gdmatrix/CMakeLists.txt b/tests/gdmatrix/CMakeLists.txt new file mode 100644 index 0000000..1035246 --- /dev/null +++ b/tests/gdmatrix/CMakeLists.txt @@ -0,0 +1,5 @@ +LIST(APPEND TESTS_FILES + bug00402 +) + +ADD_GD_TESTS() diff --git a/tests/gdmatrix/bug00402.c b/tests/gdmatrix/bug00402.c new file mode 100644 index 0000000..15529b6 --- /dev/null +++ b/tests/gdmatrix/bug00402.c @@ -0,0 +1,30 @@ +#include "gd.h" +#include <stdio.h> +#include "gdtest.h" +#include <math.h> +int main() +{ + double matrix[6] = { + 0.000000, 0.150000, + 0.150000, -0.000000, + 0.000000, 0.000000 + }; + double matrix_inv[6]; + double matrix_inv_exp[6] = { + 0.0000000000, 6.6666666667, + 6.6666666667, -0.0000000000, + -0.0000000000, 0.0000000000 + }; + + int res = gdAffineInvert(matrix_inv, matrix); + + for (int i=0; i < 6; i++) { + double rounded_res = round(matrix_inv[i] * 10); + double rounded_exp = round(matrix_inv_exp[i] * 10); + if (rounded_res != rounded_exp) { + printf("%i failed %f exp %f", i, matrix_inv[i], matrix_inv_exp[i]); + return -1; + } + } + return res; +} |