[virt-tools-list] [RFC virt-viewer 08/12] util: Improve empty string handling in virt_viewer_compare_buildid

Jonathon Jongsma jjongsma at redhat.com
Wed Jun 3 14:51:26 UTC 2015


On Tue, 2015-06-02 at 16:29 +0200, Christophe Fergeau wrote:
> When getting NULL or "" for one of the buildid components, it will be
> considered as less than the other component, unless the other component
> is also NULL or "". This matches what g_strcmp0 does and what strcmp
> does for "" strings.
> ---
>  src/virt-viewer-util.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
> index e34336a..14e1de8 100644
> --- a/src/virt-viewer-util.c
> +++ b/src/virt-viewer-util.c
> @@ -439,14 +439,24 @@ spice_hotkey_to_gtk_accelerator(const gchar *key)
>      return accel;
>  }
>  
> +static gboolean str_is_empty(const gchar *str)
> +{
> +  return ((str == NULL) || (str[0] == '\0'));
> +}
> +
>  static gint
>  virt_viewer_compare_version(const gchar *s1, const gchar *s2)
>  {
>      gint i, retval = 0;
>      gchar **v1, **v2;
>  
> -    g_return_val_if_fail(s1 != NULL, G_MAXINT);
> -    g_return_val_if_fail(s2 != NULL, G_MAXINT);
> +    if (str_is_empty(s1) && str_is_empty(s2)) {
> +      return 0;
> +    } else if (str_is_empty(s1)) {
> +      return -1;
> +    } else if (str_is_empty(s2)) {
> +      return 1;
> +    }
>  
>      v1 = g_strsplit(s1, ".", -1);
>      v2 = g_strsplit(s2, ".", -1);


Ah, I see here that you change things around so that passing NULL
parameters to this function no longer return G_MAXINT. That makes one of
my comments on patch #6 a bit obsolete perhaps.




More information about the virt-tools-list mailing list