Use _cairo_strndup where appropriate

This commit is contained in:
Adrian Johnson 2023-01-03 16:01:51 +10:30
parent 6d03a18649
commit 3d102f25c9
6 changed files with 30 additions and 46 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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;
}