[cairo] [PATCH] Ensure null-terminated result from strncpy()

Bill Spitzak spitzak at gmail.com
Sat Aug 29 16:37:11 PDT 2015


On 08/28/2015 01:25 PM, Bryce Harrington wrote:
> Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
> ---
>   src/cairo-scaled-font-subsets.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/src/cairo-scaled-font-subsets.c b/src/cairo-scaled-font-subsets.c
> index 2121761..196fa99 100644
> --- a/src/cairo-scaled-font-subsets.c
> +++ b/src/cairo-scaled-font-subsets.c
> @@ -1206,10 +1206,12 @@ _cairo_scaled_font_subset_create_glyph_names (cairo_scaled_font_subset_t *subset
>
>   	if (utf16_len == 1) {
>   	    int ch = _cairo_unicode_to_winansi (utf16[0]);
> -	    if (ch > 0 && _cairo_winansi_to_glyphname (ch))
> +	    if (ch > 0 && _cairo_winansi_to_glyphname (ch)) {
>   		strncpy (buf, _cairo_winansi_to_glyphname (ch), sizeof (buf));
> -	    else
> +		buf[sizeof (buf)-1] = '\0';
> +	    } else {
>   		snprintf (buf, sizeof (buf), "uni%04X", (int) utf16[0]);
> +	    }

snprintf(buf, sizeof(buf), "%s", _cairo_winansi_to_glyphname(ch)) will 
do what you want, it will truncated with a nul byte at the correct 
point, and not waste time filling the rest of the buffer with nul.

Of course you could also use strlcpy...




More information about the cairo mailing list