[PATCH 2/2] Ensure strncpy results are null terminated.

Bill Spitzak spitzak at gmail.com
Sat Aug 29 16:39:13 PDT 2015


On 08/28/2015 01:04 PM, Bryce Harrington wrote:
> Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
> ---
>   clients/window.c       | 1 +
>   src/compositor-fbdev.c | 1 +
>   2 files changed, 2 insertions(+)
>
> diff --git a/clients/window.c b/clients/window.c
> index 0e73f5b..614b320 100644
> --- a/clients/window.c
> +++ b/clients/window.c
> @@ -5886,6 +5886,7 @@ keysym_modifiers_add(struct wl_array *modifiers_map,
>   		return;
>
>   	strncpy(p, name, len);
> +	p[len-1] = '\0';
>   }
>
>   static xkb_mod_index_t
> diff --git a/src/compositor-fbdev.c b/src/compositor-fbdev.c
> index a5c7947..b90975c 100644
> --- a/src/compositor-fbdev.c
> +++ b/src/compositor-fbdev.c
> @@ -338,6 +338,7 @@ fbdev_query_screen_info(struct fbdev_output *output, int fd,
>   	info->buffer_length = fixinfo.smem_len;
>   	info->line_length = fixinfo.line_length;
>   	strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
> +	info->id[sizeof(info->id)-1] = '\0';
>
>   	info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
>   	info->refresh_rate = calculate_refresh_rate(&varinfo);

Lacking strlcpy, a replacement that works is snprintf(dest, len, "%s", 
source). Generally strncpy is never what you want to do.




More information about the wayland-devel mailing list