[Spice-devel] [PATCH spice-gtk] spicy: make codec to string a bit safer

Frediano Ziglio fziglio at redhat.com
Mon Mar 13 11:05:08 UTC 2017


> 
> From: Marc-André Lureau <marcandre.lureau at redhat.com>
> 
> Handle unknown values instead of out-of-array access.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau at redhat.com>
> ---
>  tools/spicy.c | 28 +++++++++++++++++++---------
>  1 file changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/spicy.c b/tools/spicy.c
> index a41a1a3..2f6be4e 100644
> --- a/tools/spicy.c
> +++ b/tools/spicy.c
> @@ -181,14 +181,24 @@ static int ask_user(GtkWidget *parent, char *title,
> char *message,
>      return retval;
>  }
>  
> -static const gchar *video_codec_enum_to_str[] = {
> -    [0] = "none",
> -    [SPICE_VIDEO_CODEC_TYPE_MJPEG] = "mjpeg",
> -    [SPICE_VIDEO_CODEC_TYPE_VP8] = "vp8",
> -    [SPICE_VIDEO_CODEC_TYPE_H264] = "h264",
> -    [SPICE_VIDEO_CODEC_TYPE_VP9] = "vp9",
> -    [SPICE_VIDEO_CODEC_TYPE_ENUM_END] = "error",
> -};
> +static const gchar *
> +video_codec_to_string(SpiceVideoCodecType type)
> +{
> +    const char *str = NULL;
> +    static const gchar *to_string[] = {

this can be also const (not a regression by the way).

> +        NULL,

This should be automatic and before was "none" instead but for
me is fine.
For SPICE_VIDEO_CODEC_TYPE_ENUM_END before an "error" was print,
now "unknown codec". Again, for me is fine.

> +        [SPICE_VIDEO_CODEC_TYPE_MJPEG] = "mjpeg",
> +        [SPICE_VIDEO_CODEC_TYPE_VP8] = "vp8",
> +        [SPICE_VIDEO_CODEC_TYPE_H264] = "h264",
> +        [SPICE_VIDEO_CODEC_TYPE_VP9] = "vp9",
> +    };
> +
> +    if (type >= 0 && type < G_N_ELEMENTS(to_string)) {
> +        str = to_string[type];
> +    }
> +
> +    return str ? str : "unknown codec";
> +}
>  
>  static void update_status_window(SpiceWindow *win)
>  {
> @@ -201,7 +211,7 @@ static void update_status_window(SpiceWindow *win)
>      g_string_printf(status, "mouse: %6s, agent: %3s, streaming: %5s",
>                      win->conn->mouse_state,
>                      win->conn->agent_state,
> -                    video_codec_enum_to_str[win->video_codec]);
> +                    video_codec_to_string(win->video_codec));
>  
>      if (win->mouse_grabbed) {
>          SpiceGrabSequence *sequence =
>          spice_display_get_grab_keys(SPICE_DISPLAY(win->spice));

Reviewed-by: Frediano Ziglio <fziglio at redhat.com>

Frediano


More information about the Spice-devel mailing list