[cairo-commit] 2 commits - src/cairo-scaled-font.c test/.gitignore test/Makefile.am test/text-zero-len.c test/text-zero-len-ref.png

Behdad Esfahbod behdad at kemper.freedesktop.org
Thu Dec 28 17:01:22 PST 2006


 src/cairo-scaled-font.c    |    6 +++
 test/.gitignore            |    1 
 test/Makefile.am           |    2 +
 test/text-zero-len-ref.png |binary
 test/text-zero-len.c       |   88 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+)

New commits:
diff-tree eafd6e454a85c807f2addf7768046fea58aea3f9 (from ab29396ef9f68f5534f927b2b5bb0a7ffac86509)
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Thu Dec 28 20:01:18 2006 -0500

    In _cairo_scaled_font_text_to_glyphs, bail if text is zero-length (#5177)

diff --git a/src/cairo-scaled-font.c b/src/cairo-scaled-font.c
index 73afb5f..c63f774 100755
--- a/src/cairo-scaled-font.c
+++ b/src/cairo-scaled-font.c
@@ -790,6 +790,12 @@ _cairo_scaled_font_text_to_glyphs (cairo
     cairo_status_t status = CAIRO_STATUS_SUCCESS;
     cairo_scaled_glyph_t *scaled_glyph;
 
+    if (utf8[0] == '\0') {
+	*num_glyphs = 0;
+	*glyphs = NULL;
+	return CAIRO_STATUS_SUCCESS;
+    }
+
     if (scaled_font->backend->text_to_glyphs) {
 	status = scaled_font->backend->text_to_glyphs (scaled_font,
 						       x, y, utf8,
diff-tree ab29396ef9f68f5534f927b2b5bb0a7ffac86509 (from 2d30161e8922e66b276341810c21ce1b740f6699)
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Thu Dec 28 19:51:20 2006 -0500

    [test] Add new test text-zero-len (#5177)
    
    The test passes an empty string to cairo_show_text, cairo_text_path, and
    cairo_text_extents, and NULL and an invalid pointer, with zero num_glyphs to
    cairo_show_glyphs, cairo_glyph_path, and cairo_glyph_extents.

diff --git a/test/.gitignore b/test/.gitignore
index c6e2c40..75fa10c 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -126,6 +126,7 @@ text-antialias-subpixel
 text-cache-crash
 text-pattern
 text-rotate
+text-zero-len
 transforms
 translate-show-surface
 trap-clip
diff --git a/test/Makefile.am b/test/Makefile.am
index 16237dc..efdb162 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -90,6 +90,7 @@ text-antialias-subpixel		\
 text-cache-crash		\
 text-pattern			\
 text-rotate			\
+text-zero-len			\
 transforms			\
 translate-show-surface		\
 trap-clip			\
@@ -320,6 +321,7 @@ text-pattern-svg-argb32-ref.png				\
 text-pattern-svg-rgb24-ref.png				\
 text-rotate-ref.png					\
 text-rotate-rgb24-ref.png				\
+text-zero-len-ref.png					\
 transforms-ref.png					\
 translate-show-surface-ref.png				\
 trap-clip-ref.png					\
diff --git a/test/text-zero-len-ref.png b/test/text-zero-len-ref.png
new file mode 100644
index 0000000..4bd2d04
Binary files /dev/null and b/test/text-zero-len-ref.png differ
diff --git a/test/text-zero-len.c b/test/text-zero-len.c
new file mode 100644
index 0000000..57c36fd
--- /dev/null
+++ b/test/text-zero-len.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Red Hat, Inc. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Red Hat, Inc. makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Behdad Esfahbod <behdad at behdad.org>
+ */
+
+/* Related bug 5177
+ *
+ * In short:
+ *
+ * _cairo_atsui_font_text_to_glyph with a zero-sized string crashes.
+ *
+ * Moreover, the fallback path in cairo_scaled_font_text_to_glyphs()
+ * when handling a zero-sized string, allocates a zero-sized glyph array
+ * and when NULL is returned by malloc, recognizes that as an out-of-memory
+ * error.  The glibc implementation of malloc() does not return NULL from
+ * malloc(0), but I don't think it's a safe assumption.
+ *
+ * By just bailing out on zero-sized text, we fix both issues.
+ */
+
+#include "cairo-test.h"
+
+#define WIDTH  60
+#define HEIGHT 60
+#define NUM_TEXT 20
+#define TEXT_SIZE 12
+
+static cairo_test_draw_function_t draw;
+
+cairo_test_t test = {
+    "text-zero-len",
+    "Tests show_text and text_path with a zero-sized string",
+    WIDTH, HEIGHT,
+    draw
+};
+
+/* Draw the word cairo at NUM_TEXT different angles */
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    cairo_text_extents_t extents;
+
+    cairo_select_font_face (cr, "Bitstream Vera Sans",
+			    CAIRO_FONT_SLANT_NORMAL,
+			    CAIRO_FONT_WEIGHT_NORMAL);
+    cairo_set_font_size (cr, 16);
+
+    cairo_move_to (cr, 10, 25);
+    cairo_show_text (cr, "");
+    cairo_show_glyphs (cr, NULL, 0);
+    cairo_show_glyphs (cr, (void*)8, 0);
+
+    cairo_move_to (cr, 10, 55);
+    cairo_text_path (cr, "");
+    cairo_glyph_path (cr, (void*)8, 0);
+    cairo_fill (cr);
+
+    cairo_text_extents (cr, "", &extents);
+    cairo_glyph_extents (cr, (void*)8, 0, &extents);
+
+    return CAIRO_TEST_SUCCESS;
+}
+
+int
+main (void)
+{
+    return cairo_test (&test);
+}


More information about the cairo-commit mailing list