[cairo-commit] 6 commits - src/cairo-cff-subset.c src/cairo-ft-font.c src/cairoint.h src/cairo-misc.c src/cairo-pdf-surface.c src/cairo-ps-surface.c src/cairo-scaled-font-subsets.c src/cairo-svg-glyph-render.c src/cairo-tag-attributes.c src/cairo-truetype-subset.c util/cairo-missing

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jan 3 19:03:52 UTC 2023


 src/cairo-cff-subset.c          |    8 +++-----
 src/cairo-ft-font.c             |    3 ++-
 src/cairo-misc.c                |   27 +++++++++++++++++++++++++++
 src/cairo-pdf-surface.c         |    7 ++++---
 src/cairo-ps-surface.c          |   14 ++++++++------
 src/cairo-scaled-font-subsets.c |   27 +++++++++++----------------
 src/cairo-svg-glyph-render.c    |   36 ++++++++++++++++++------------------
 src/cairo-tag-attributes.c      |    5 +----
 src/cairo-truetype-subset.c     |   15 +++------------
 src/cairoint.h                  |    7 +++++++
 util/cairo-missing/strndup.c    |    8 ++++++--
 11 files changed, 90 insertions(+), 67 deletions(-)

New commits:
commit 13a0474641232cccb15d24fa31d8c2557731665b
Merge: 4e51d1536 3d102f25c
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 19:03:50 2023 +0000

    Merge branch 'mingw-fixes' into 'master'
    
    Fix mingw build failure when SVG fonts enabled
    
    See merge request cairo/cairo!389

commit 3d102f25c924936d660fb771b695f753debf8d44
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 16:01:51 2023 +1030

    Use _cairo_strndup where appropriate

diff --git a/src/cairo-cff-subset.c b/src/cairo-cff-subset.c
index be4766440..c7aaec4a8 100644
--- a/src/cairo-cff-subset.c
+++ b/src/cairo-cff-subset.c
@@ -890,12 +890,10 @@ cairo_cff_font_read_name (cairo_cff_font_t *font)
 		len -= 7;
 	    }
 	}
-        font->ps_name = _cairo_malloc (len + 1);
-        if (unlikely (font->ps_name == NULL))
-            return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-        memcpy (font->ps_name, p, len);
-        font->ps_name[len] = 0;
+	font->ps_name = _cairo_strndup ((char*)p, len);
+	if (unlikely (font->ps_name == NULL))
+	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
         status = _cairo_escape_ps_name (&font->ps_name);
     }
diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index 86ee48cdc..20720e2cb 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -3535,9 +3535,10 @@ _cairo_pdf_surface_emit_ccitt_image (cairo_pdf_surface_t              *surface,
 	return CAIRO_INT_STATUS_UNSUPPORTED;
 
     /* ensure params_string is null terminated */
-    params = malloc (ccitt_params_string_len + 1);
-    memcpy (params, ccitt_params_string, ccitt_params_string_len);
-    params[ccitt_params_string_len] = 0;
+    params = _cairo_strndup ((const char *)ccitt_params_string, ccitt_params_string_len);
+    if (unlikely (params == NULL))
+	return _cairo_surface_set_error (&surface->base, CAIRO_STATUS_NO_MEMORY);
+
     status = _cairo_tag_parse_ccitt_params (params, &ccitt_params);
     if (unlikely(status))
 	return source->status;
diff --git a/src/cairo-ps-surface.c b/src/cairo-ps-surface.c
index 343a5ea05..30e9a064b 100644
--- a/src/cairo-ps-surface.c
+++ b/src/cairo-ps-surface.c
@@ -3095,9 +3095,10 @@ _cairo_ps_surface_emit_ccitt_image (cairo_ps_surface_t          *surface,
 	return CAIRO_INT_STATUS_UNSUPPORTED;
 
     /* ensure params_string is null terminated */
-    ccitt_params_string = malloc (ccitt_params_data_len + 1);
-    memcpy (ccitt_params_string, ccitt_params_data, ccitt_params_data_len);
-    ccitt_params_string[ccitt_params_data_len] = 0;
+    ccitt_params_string = _cairo_strndup ((const char *)ccitt_params_data, ccitt_params_data_len);
+    if (unlikely (ccitt_params_string == NULL))
+	return _cairo_surface_set_error (&surface->base, CAIRO_STATUS_NO_MEMORY);
+
     status = _cairo_tag_parse_ccitt_params (ccitt_params_string, &ccitt_params);
     if (unlikely(status))
 	return status;
@@ -3280,9 +3281,10 @@ _cairo_ps_surface_emit_eps (cairo_ps_surface_t          *surface,
 	return CAIRO_INT_STATUS_UNSUPPORTED;
 
     /* ensure params_string is null terminated */
-    params_string = malloc (eps_params_string_len + 1);
-    memcpy (params_string, eps_params_string, eps_params_string_len);
-    params_string[eps_params_string_len] = 0;
+    params_string = _cairo_strndup ((const char *)eps_params_string, eps_params_string_len);
+    if (unlikely (params_string == NULL))
+	return _cairo_surface_set_error (&surface->base, CAIRO_STATUS_NO_MEMORY);
+
     status = _cairo_tag_parse_eps_params (params_string, &eps_params);
     if (unlikely(status))
 	return status;
diff --git a/src/cairo-scaled-font-subsets.c b/src/cairo-scaled-font-subsets.c
index 94a7aae26..c5ba12665 100644
--- a/src/cairo-scaled-font-subsets.c
+++ b/src/cairo-scaled-font-subsets.c
@@ -1,3 +1,4 @@
+/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
 /* cairo - a vector graphics library with display and print output
  *
  * Copyright © 2003 University of Southern California
@@ -404,12 +405,10 @@ _cairo_sub_font_glyph_lookup_unicode (cairo_scaled_font_t    *scaled_font,
     if (unicode != (uint32_t) -1) {
 	len = _cairo_ucs4_to_utf8 (unicode, buf);
 	if (len > 0) {
-	    *utf8_out = _cairo_malloc (len + 1);
-	    if (unlikely (*utf8_out == NULL))
-		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+            *utf8_out = _cairo_strndup (buf, len);
+            if (unlikely (*utf8_out == NULL))
+                return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-	    memcpy (*utf8_out, buf, len);
-	    (*utf8_out)[len] = 0;
 	    *utf8_len_out = len;
 	}
     }
@@ -441,12 +440,10 @@ _cairo_sub_font_glyph_map_to_unicode (cairo_sub_font_glyph_t *sub_font_glyph,
 	    }
 	} else {
 	    /* No existing mapping. Use the requested mapping */
-	    sub_font_glyph->utf8 = _cairo_malloc (utf8_len + 1);
-	    if (unlikely (sub_font_glyph->utf8 == NULL))
-		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+            sub_font_glyph->utf8 = _cairo_strndup (utf8, utf8_len);
+            if (unlikely (sub_font_glyph->utf8 == NULL))
+                return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-	    memcpy (sub_font_glyph->utf8, utf8, utf8_len);
-	    sub_font_glyph->utf8[utf8_len] = 0;
 	    sub_font_glyph->utf8_len = utf8_len;
 	    *is_mapped = TRUE;
 	}
@@ -612,13 +609,11 @@ _cairo_sub_font_map_glyph (cairo_sub_font_t	*sub_font,
 		if (ucs4_len == 1) {
 		    font_unicode = ucs4[0];
 		    free (font_utf8);
-		    font_utf8 = _cairo_malloc (text_utf8_len + 1);
-		    if (font_utf8 == NULL) {
-			free (ucs4);
-			return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+                    font_utf8 = _cairo_strndup (text_utf8, text_utf8_len);
+                    if (font_utf8 == NULL) {
+                        free (ucs4);
+                        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 		    }
-		    memcpy (font_utf8, text_utf8, text_utf8_len);
-		    font_utf8[text_utf8_len] = 0;
 		    font_utf8_len = text_utf8_len;
 		}
 		free (ucs4);
diff --git a/src/cairo-tag-attributes.c b/src/cairo-tag-attributes.c
index 6d0f63f11..85467ad73 100644
--- a/src/cairo-tag-attributes.c
+++ b/src/cairo-tag-attributes.c
@@ -366,12 +366,9 @@ parse_name (const char *attributes, const char *p, const char **end, char **s)
 	p2++;
 
     len = p2 - p;
-    name = _cairo_malloc (len + 1);
+    name = _cairo_strndup (p, len);
     if (unlikely (name == NULL))
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
-
-    memcpy (name, p, len);
-    name[len] = 0;
     *s = name;
     *end = p2;
 
diff --git a/src/cairo-truetype-subset.c b/src/cairo-truetype-subset.c
index c58b96665..91ee0e90b 100644
--- a/src/cairo-truetype-subset.c
+++ b/src/cairo-truetype-subset.c
@@ -1,3 +1,4 @@
+/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
 /* cairo - a vector graphics library with display and print output
  *
  * Copyright © 2004 Red Hat, Inc
@@ -1470,14 +1471,10 @@ find_name (tt_name_t *name, unsigned long size, int name_id, int platform, int e
 	    if (offset + len > size)
 		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-	    str = _cairo_malloc (len + 1);
+	    str = _cairo_strndup (((char*)name) + offset, len);
 	    if (str == NULL)
 		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-	    memcpy (str,
-		    ((char*)name) + offset,
-		    len);
-	    str[len] = 0;
 	    break;
 	}
     }
@@ -1531,13 +1528,7 @@ find_name (tt_name_t *name, unsigned long size, int name_id, int platform, int e
 	}
     }
     if (has_tag) {
-	p = _cairo_malloc (len - 6);
-	if (unlikely (p == NULL)) {
-	    status =_cairo_error (CAIRO_STATUS_NO_MEMORY);
-	    goto fail;
-	}
-	memcpy (p, str + 7, len - 7);
-	p[len-7] = 0;
+	p = _cairo_strndup (str + 7, len - 7);
 	free (str);
 	str = p;
     }
commit 6d03a186491b34745ae13a6a589799ab40fa699b
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 15:28:02 2023 +1030

    Fix leak

diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c
index 674e18196..94bae79c1 100644
--- a/src/cairo-ft-font.c
+++ b/src/cairo-ft-font.c
@@ -2768,6 +2768,7 @@ _cairo_ft_scaled_glyph_init_record_colr_v0_glyph (cairo_ft_scaled_font_t *scaled
 	path = _cairo_path_create (path_fixed, cr);
 	_cairo_path_fixed_destroy (path_fixed);
 	cairo_append_path(cr, path);
+	cairo_path_destroy (path);
 	cairo_fill (cr);
     }
 
commit dcc6c2c806065531d227cd316102562d76eb9b48
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 15:24:38 2023 +1030

    Fix mingw warning: "ERROR" redefined

diff --git a/src/cairo-svg-glyph-render.c b/src/cairo-svg-glyph-render.c
index f4fe4ffce..6ed3b8cc8 100644
--- a/src/cairo-svg-glyph-render.c
+++ b/src/cairo-svg-glyph-render.c
@@ -311,13 +311,13 @@ typedef struct _cairo_svg_glyph_render {
 } cairo_svg_glyph_render_t;
 
 
-#define ERROR 1
-#define WARNING 2
-#define INFO 3
+#define SVG_RENDER_ERROR 1
+#define SVG_RENDER_WARNING 2
+#define SVG_RENDER_INFO 3
 
-#define print_error(render, ...) cairo_svg_glyph_render_printf(render, ERROR, ##__VA_ARGS__)
-#define print_warning(render, ...) cairo_svg_glyph_render_printf(render, WARNING, ##__VA_ARGS__)
-#define print_info(render, ...) cairo_svg_glyph_render_printf(render, INFO, ##__VA_ARGS__)
+#define print_error(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_ERROR, ##__VA_ARGS__)
+#define print_warning(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_WARNING, ##__VA_ARGS__)
+#define print_info(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_INFO, ##__VA_ARGS__)
 
 static void
 cairo_svg_glyph_render_printf (cairo_svg_glyph_render_t *svg_render,
@@ -333,10 +333,10 @@ cairo_svg_glyph_render_printf (cairo_svg_glyph_render_t *svg_render,
 
     if (svg_render->debug >= level ) {
         switch (level) {
-            case ERROR:
+            case SVG_RENDER_ERROR:
                 printf("ERROR: ");
                 break;
-            case WARNING:
+            case SVG_RENDER_WARNING:
                 printf("WARNING: ");
                 break;
         }
@@ -920,7 +920,7 @@ parse_error (cairo_svg_glyph_render_t *svg_render,
     const char *start;
     const char *end;
 
-    if (svg_render->debug >= ERROR) {
+    if (svg_render->debug >= SVG_RENDER_ERROR) {
         printf("ERROR: ");
 	va_start (ap, fmt);
 	vprintf (fmt, ap);
@@ -3115,7 +3115,7 @@ _cairo_render_svg_glyph (const char           *svg_document,
         if (strlen (s) > 0)
             svg_render->debug = atoi (s);
         else
-            svg_render->debug = ERROR;
+            svg_render->debug = SVG_RENDER_ERROR;
     }
 
     svg_render->cr = cr;
commit 82aa3fb80eed9eb7f5b0225a89d33a08b269dd9f
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 15:08:48 2023 +1030

    Fix bug in strndup implementation
    
    The strlen() could go past the n bytes and into a memory address we
    don't have read access to.

diff --git a/util/cairo-missing/strndup.c b/util/cairo-missing/strndup.c
index 280ea3017..049802b76 100644
--- a/util/cairo-missing/strndup.c
+++ b/util/cairo-missing/strndup.c
@@ -37,15 +37,19 @@ char *
 strndup (const char *s,
 	 size_t      n)
 {
+    const char *end;
     size_t len;
     char *sdup;
 
     if (s == NULL)
 	return NULL;
 
-    len = strlen (s);
-    if (len > n)
+    end = memchr (s, 0, n);
+    if (end)
+	len = end - s;
+    else
 	len = n;
+
     sdup = (char *) _cairo_malloc (len + 1);
     if (sdup != NULL) {
 	memcpy (sdup, s, len);
commit 5e0e40e3c53d253bb767606f847182f95a443547
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Jan 3 15:05:51 2023 +1030

    Fix mingw build failure when SVG fonts enabled
    
    Add missing strndup() function. Copied the strndup() implementation
    from util/cairo-missing/strndup.c plus a bug fix.

diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c
index 100f839d7..674e18196 100644
--- a/src/cairo-ft-font.c
+++ b/src/cairo-ft-font.c
@@ -2803,7 +2803,7 @@ _cairo_ft_scaled_glyph_init_record_svg_glyph (cairo_ft_scaled_font_t *scaled_fon
     unsigned int num_palette_entries;
 
     /* Create NULL terminated SVG document */
-    svg_document = strndup((const char*)svg_doc->svg_document, svg_doc->svg_document_length);
+    svg_document = _cairo_strndup ((const char*)svg_doc->svg_document, svg_doc->svg_document_length);
 
     recording_surface =
 	cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
diff --git a/src/cairo-misc.c b/src/cairo-misc.c
index bf8a62730..edbd98003 100644
--- a/src/cairo-misc.c
+++ b/src/cairo-misc.c
@@ -888,6 +888,33 @@ _cairo_strtod (const char *nptr, char **endptr)
 }
 #endif
 
+#ifndef HAVE_STRNDUP
+char *
+_cairo_strndup (const char *s, size_t n)
+{
+    const char *end;
+    size_t len;
+    char *sdup;
+
+    if (s == NULL)
+	return NULL;
+
+    end = memchr (s, 0, n);
+    if (end)
+	len = end - s;
+    else
+	len = n;
+
+    sdup = (char *) _cairo_malloc (len + 1);
+    if (sdup != NULL) {
+	memcpy (sdup, s, len);
+	sdup[len] = '\0';
+    }
+
+    return sdup;
+}
+#endif
+
 /**
  * _cairo_fopen:
  * @filename: filename to open
diff --git a/src/cairo-svg-glyph-render.c b/src/cairo-svg-glyph-render.c
index 738d4f8ff..f4fe4ffce 100644
--- a/src/cairo-svg-glyph-render.c
+++ b/src/cairo-svg-glyph-render.c
@@ -453,7 +453,7 @@ lookup_url_element (cairo_svg_glyph_render_t *svg_render, const char *url)
 
         const char *end = strpbrk(p, WHITE_SPACE_CHARS ")");
         if (end) {
-            char *id = strndup (p, end - p);
+            char *id = _cairo_strndup (p, end - p);
             element = lookup_element (svg_render, id);
             free (id);
         }
@@ -812,7 +812,7 @@ get_color (cairo_svg_glyph_render_t *svg_render,
             if (!end || end == s)
             return FALSE;
 
-            char *fallback = strndup (s, end - s);
+            char *fallback = _cairo_strndup (s, end - s);
             cairo_bool_t success = get_color (svg_render, fallback, color);
             free (fallback);
             return success;
@@ -965,7 +965,7 @@ append_attribute (cairo_svg_element_t *element, svg_attribute_t *attribute)
             end = strchr (p, ':');
             if (!end || end == p)
                 break;
-            attr.name = strndup (p, end - p);
+            attr.name = _cairo_strndup (p, end - p);
             p = end + 1;
             p = skip_space(p);
             end = strchr (p, ';');
@@ -974,7 +974,7 @@ append_attribute (cairo_svg_element_t *element, svg_attribute_t *attribute)
             if (end == p)
                 goto split_style_fail;
 
-            attr.value = strndup (p, end - p);
+            attr.value = _cairo_strndup (p, end - p);
             if (*end)
                 p = end + 1;
 
@@ -1067,7 +1067,7 @@ parse_attributes (cairo_svg_glyph_render_t *svg_render,
             goto fail;
         }
 
-        attr.name = strndup (p, end - p);
+        attr.name = _cairo_strndup (p, end - p);
         p = end;
 
         p = skip_space (p);
@@ -1092,7 +1092,7 @@ parse_attributes (cairo_svg_glyph_render_t *svg_render,
             goto fail;
         }
 
-        attr.value = strndup (p, end - p);
+        attr.value = _cairo_strndup (p, end - p);
         p = end + 1;
 
         if (!append_attribute (element, &attr))
@@ -1229,7 +1229,7 @@ parse_svg (cairo_svg_glyph_render_t *svg_render,
                 goto fail;
             }
 
-            name = strndup (p, end - p);
+            name = _cairo_strndup (p, end - p);
             p = end;
             p = skip_space (p);
             if (*p != '>') {
@@ -1275,7 +1275,7 @@ parse_svg (cairo_svg_glyph_render_t *svg_render,
             goto fail;
         }
 
-        name = strndup (p, end - p);
+        name = _cairo_strndup (p, end - p);
         p = end;
 
         new_elem = create_element (CONTAINER_ELEMENT, name);
diff --git a/src/cairoint.h b/src/cairoint.h
index af0f0fadc..976162e6f 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -943,6 +943,13 @@ _cairo_get_locale_decimal_point (void);
 cairo_private double
 _cairo_strtod (const char *nptr, char **endptr);
 
+#ifdef HAVE_STRNDUP
+#define _cairo_strndup strndup
+#else
+cairo_private char *
+_cairo_strndup (const char *s, size_t n);
+#endif
+
 /* cairo-path-fixed.c */
 cairo_private cairo_path_fixed_t *
 _cairo_path_fixed_create (void);


More information about the cairo-commit mailing list