[virt-tools-list] [PATCH 2/2] entity: Fix osinfo_entity_get_param_value_int64_with_default

Christophe Fergeau cfergeau at redhat.com
Thu Nov 22 14:32:24 UTC 2012


The way it's currently implemented,
osinfo_entity_get_param_value_int64_with_default will return the
default value if the value which is being parsed is negative.
This happens because it tries to reuse osinfo_entity_get_param_value_int64
to do the parsing, which returns -1 when the value could not be found.
However, we have no way of knowing if the -1 means that the value could
not be found, or if this means the value was found and its value is -1.
This is made worse by the fact that we return the default value as
soon as osinfo_entity_get_param_value_int64 returns a negative value,
not just -1.

By implementing osinfo_entity_get_param_value_int64 by calling
osinfo_entity_get_param_value_int64_with_default rather than doing the
contrary, we can avoid this issue.
---
 osinfo/osinfo_entity.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/osinfo/osinfo_entity.c b/osinfo/osinfo_entity.c
index f86b142..0e4b2a4 100644
--- a/osinfo/osinfo_entity.c
+++ b/osinfo/osinfo_entity.c
@@ -364,28 +364,21 @@ gboolean osinfo_entity_get_param_value_boolean_with_default(OsinfoEntity *entity
 gint64 osinfo_entity_get_param_value_int64(OsinfoEntity *entity,
                                            const gchar *key)
 {
-    const gchar *str;
-
-    str = osinfo_entity_get_param_value(entity, key);
-
-    if (str == NULL)
-        return -1;
-
-    return (gint64) g_ascii_strtoll(str, NULL);
+    return osinfo_entity_get_param_value_int64_with_default(entity, key, -1);
 }
 
 gint64 osinfo_entity_get_param_value_int64_with_default(OsinfoEntity *entity,
                                                         const gchar *key,
                                                         gint64 default_value)
 {
-    gint64 value;
+    const gchar *str;
 
-    value = osinfo_entity_get_param_value_int64(entity, key);
+    str = osinfo_entity_get_param_value(entity, key);
 
-    if (value < 0)
+    if (str == NULL)
         return default_value;
 
-    return value;
+    return g_ascii_strtoll(str, NULL, 0);
 }
 
 gint osinfo_entity_get_param_value_enum(OsinfoEntity *entity,
-- 
1.8.0




More information about the virt-tools-list mailing list