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

Bill Spitzak <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
On 08/28/2015 01:25 PM, Bryce Harrington wrote:
> Signed-off-by: Bryce Harrington <[email protected]>
> ---
>   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...


-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.