dash-infinite-loop.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright © 2009 M Joonas Pihlaja
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
  25. */
  26. #include "cairo-test.h"
  27. /* When faced with very small dash lengths the stroker is liable to
  28. * get stuck in an infinite loop when advancing the dash offset. This
  29. * test attempts to hit each of the locations in the stroker code
  30. * where the dash offset is advanced in a loop.
  31. *
  32. * Reported to the cairo mailing list by Hans Breuer.
  33. * http://lists.cairographics.org/archives/cairo/2009-June/017506.html
  34. */
  35. #define EPS 1e-30
  36. /* This should be comfortably smaller than the unit epsilon of the
  37. * floating point type used to advance the dashing, yet not small
  38. * enough that it underflows to zero. 1e-30 works to foil up to 80
  39. * bit extended precision arithmetic. We want to avoid zero dash
  40. * lengths because those trigger special processing in the stroker. */
  41. static void
  42. do_dash (cairo_t *cr, double dx, double dy, double offset)
  43. {
  44. /* Set the dash pattern to be predominantly ON so that we can
  45. * create a reference image by just ignoring the dashing. */
  46. static double dash[] = { EPS, EPS/512 };
  47. cairo_set_dash (cr, dash, 2, offset);
  48. cairo_move_to (cr, 10, 10);
  49. cairo_rel_line_to (cr, dx, dy);
  50. cairo_stroke (cr);
  51. cairo_translate (cr, dx, dy);
  52. }
  53. static cairo_test_status_t
  54. draw (cairo_t *cr, int width, int height)
  55. {
  56. (void)width; (void)height;
  57. cairo_set_source_rgb (cr, 1,1,1);
  58. cairo_paint (cr);
  59. cairo_set_source_rgb (cr, 0,0,0);
  60. cairo_set_line_width (cr, 10);
  61. /* The following calls will wedge in various places that try
  62. * to advance the dashing in a loop inside the stroker. */
  63. do_dash (cr, 30, 30, 0); /* _cairo_stroker_line_to_dashed */
  64. do_dash (cr, 30, 0, 0); /* _cairo_rectilinear_stroker_line_to_dashed */
  65. do_dash (cr, 30, 30, 1); /* _cairo_stroker_dash_start */
  66. return CAIRO_TEST_SUCCESS;
  67. }
  68. CAIRO_TEST (dash_infinite_loop,
  69. "Test dashing with extremely small dash lengths.",
  70. "dash",
  71. NULL,
  72. 100, 100,
  73. NULL, draw);