diff options
author | Philip Withnall <withnall@endlessm.com> | 2017-02-14 11:12:51 +0000 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2017-02-14 11:16:05 +0000 |
commit | e4ea2d2e5118b44bf1c3e5fa998356696e073aa8 (patch) | |
tree | 90682d671f6b83bd4d274653fe46565eac75bd34 | |
parent | 318fd546a0ab8ad68f65f5f81b0a6fb845141a9f (diff) | |
download | pango-e4ea2d2e5118b44bf1c3e5fa998356696e073aa8.tar.gz |
Add assertions to guard against division by zero on sample text paths
There are a few code paths where pango_utf8_strwidth() is called on
language-specific sample text. The sample text should have been chosen
to never have a zero width, but we should add some assertions to ensure
that’s the case. This guides static analysers into the right analysis.
Coverity IDs: 1391697, 1391698, 1391699
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://bugzilla.gnome.org/show_bug.cgi?id=778602
-rw-r--r-- | pango/pango-context.c | 8 | ||||
-rw-r--r-- | pango/pangocairo-font.c | 5 | ||||
-rw-r--r-- | pango/pangofc-font.c | 5 |
3 files changed, 15 insertions, 3 deletions
diff --git a/pango/pango-context.c b/pango/pango-context.c index f91d0fe6..f0cea733 100644 --- a/pango/pango-context.c +++ b/pango/pango-context.c @@ -1635,6 +1635,10 @@ update_metrics_from_items (PangoFontMetrics *metrics, GHashTable *fonts_seen = g_hash_table_new (NULL, NULL); PangoGlyphString *glyphs = pango_glyph_string_new (); GList *l; + glong text_width; + + /* This should typically be called with a sample text string. */ + g_return_if_fail (text_len > 0); metrics->approximate_char_width = 0; @@ -1663,7 +1667,9 @@ update_metrics_from_items (PangoFontMetrics *metrics, pango_glyph_string_free (glyphs); g_hash_table_destroy (fonts_seen); - metrics->approximate_char_width /= pango_utf8_strwidth (text); + text_width = pango_utf8_strwidth (text); + g_assert (text_width > 0); + metrics->approximate_char_width /= text_width; } /** diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c index 97df00c5..b86f85b7 100644 --- a/pango/pangocairo-font.c +++ b/pango/pangocairo-font.c @@ -249,6 +249,7 @@ _pango_cairo_font_get_metrics (PangoFont *font, cairo_matrix_t cairo_matrix; PangoMatrix pango_matrix; PangoMatrix identity = PANGO_MATRIX_INIT; + glong sample_str_width; int height, shift; @@ -311,7 +312,9 @@ _pango_cairo_font_get_metrics (PangoFont *font, pango_layout_set_text (layout, sample_str, -1); pango_layout_get_extents (layout, NULL, &extents); - info->metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str); + sample_str_width = pango_utf8_strwidth (sample_str); + g_assert (sample_str_width > 0); + info->metrics->approximate_char_width = extents.width / sample_str_width; pango_layout_set_text (layout, "0123456789", -1); info->metrics->approximate_digit_width = max_glyph_width (layout); diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index b463e90c..c35b77ee 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -588,6 +588,7 @@ pango_fc_font_get_metrics (PangoFont *font, PangoRectangle extents; const char *sample_str = pango_language_get_sample_string (language); PangoFontDescription *desc = pango_font_describe_with_absolute_size (font); + gulong sample_str_width; layout = pango_layout_new (context); pango_layout_set_font_description (layout, desc); @@ -596,7 +597,9 @@ pango_fc_font_get_metrics (PangoFont *font, pango_layout_set_text (layout, sample_str, -1); pango_layout_get_extents (layout, NULL, &extents); - info->metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str); + sample_str_width = pango_utf8_strwidth (sample_str); + g_assert (sample_str_width > 0); + info->metrics->approximate_char_width = extents.width / sample_str_width; pango_layout_set_text (layout, "0123456789", -1); info->metrics->approximate_digit_width = max_glyph_width (layout); |