Skip to contentSkip to conversation

: xl: introduce xlu_cfg_get_(string|long)_default

PATCH In review Gianni Tedesco 3 participants 6 messages Aug 31, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : xl: introduce xlu_cfg_get_(string|long)_default

The function init_dm_info() in xl_cmdimpl.c is used to initialise a libxl_device_model_info structure with some default values. After being called, some of those values are overridden. For the string values which are not overwritten we either end up with a double free or attempt to free a string literal resulting in a segfault. This type of usage model would be complex to fix by adding strdup()'s in the init function and then checking and freeing when over-writing.

My proposed solution is to add default versions of xlu_cfg_get_string and xlu_cfg_get_long. This way both double-free's and leaks are avoided and arguably the intention of the code is clearer.

tools/libxl/libxlu_cfg.c +46 -1
diff -r d323c34ec982 -r aa90461b1d04 tools/libxl/libxlu_cfg.c
@@ -151,7 +151,23 @@ int xlu_cfg_get_string(const XLU_Config 
     *value_r= set->values[0];
     return 0;
 }
-        
+
+int xlu_cfg_get_string_default(const XLU_Config *cfg, const char *n,
+                       const char **value_r, const char *def) {
+    XLU_ConfigSetting *set;
+    int e;
+
+    e= find_atom(cfg,n,&set);
+    if (e) {
+        *value_r = strdup(def);
+        if ( def && NULL == *value_r )
+            return ENOMEM;
+        return 0;
+    }
+    *value_r= set->values[0];
+    return 0;
+}
+
 int xlu_cfg_get_long(const XLU_Config *cfg, const char *n,
                      long *value_r) {
     long l;
@@ -181,6 +197,35 @@ int xlu_cfg_get_long(const XLU_Config *c
     return 0;
 }
         
+int xlu_cfg_get_long_default(const XLU_Config *cfg, const char *n,
+                     long *value_r, long def) {
+    long l;
+    XLU_ConfigSetting *set;
+    int e;
+    char *ep;
+
+    e= find_atom(cfg,n,&set);  if (e) { *value_r = def; return 0; }
+    errno= 0; l= strtol(set->values[0], &ep, 0);
+    e= errno;
+    if (errno) {
+        e= errno;
+        assert(e==EINVAL || e==ERANGE);
+        fprintf(cfg->report,
+                "%s:%d: warning: parameter `%s' could not be parsed"
+                " as a number: %s\n",
+                cfg->filename, set->lineno, n, strerror(e));
+        *value_r = def;
+        return e;
+    }
+    if (*ep || ep==set->values[0]) {
+        fprintf(cfg->report,
+                "%s:%d: warning: parameter `%s' is not a valid number\n",
+                cfg->filename, set->lineno, n);
+        return EINVAL;
+    }
+    *value_r= l;
+    return 0;
+}
 
 int xlu_cfg_get_list(const XLU_Config *cfg, const char *n,
                      XLU_ConfigList **list_r, int *entries_r) {
tools/libxl/libxlutil.h +3 -0
diff -r d323c34ec982 -r aa90461b1d04 tools/libxl/libxlutil.h
@@ -46,7 +46,10 @@ void xlu_cfg_destroy(XLU_Config*);
  */
 
 int xlu_cfg_get_string(const XLU_Config*, const char *n, const char **value_r);
+int xlu_cfg_get_string_default(const XLU_Config *cfg, const char *n,
+                                const char **value_r, const char *def);
 int xlu_cfg_get_long(const XLU_Config*, const char *n, long *value_r);
+int xlu_cfg_get_long_default(const XLU_Config*, const char *n, long *value_r, long def);
 
 int xlu_cfg_get_list(const XLU_Config*, const char *n,
                      XLU_ConfigList **list_r /* may be 0 */,
tools/libxl/xl_cmdimpl.c +16 -37

87 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Gianni Tedesco

The function init_dm_info() in xl_cmdimpl.c is used to initialise a libxl_device_model_info structure with some default values. After being called, some of those values are overridden. For the string values which are not overwritten we either end up with a double free or attempt to free a string literal resulting in a segfault. This type of usage model would be complex to fix by adding strdup()'s in the init function and then checking and freeing when over-writing.

My proposed solution is to add default versions of xlu_cfg_get_string and xlu_cfg_get_long.

I like the idea but is it not possible to implement these as wrappers around the non-default providing versions and therefore avoid duplicating the code? (or maybe the other way round, defining the non-default variants in terms of default==NULL etc).

tools/libxl/xl_cmdimpl.c skip_vfb: · line 993

Quoted 4 lines
         /* then process config related to dm */
tools/libxl/xl_cmdimpl.c +1 -1
-        if (!xlu_cfg_get_string (config, "device_model", &buf))
+        if (!xlu_cfg_get_string_default (config, "device_model", &buf, "qemu-dm"))
             dm_info->device_model = strdup(buf);

Hasn't buf already been strdupped by xlu_cfg_get_string_default if the default ends up being used?

I'm not sure about set->values[0] in the other case but presumably it is not already dupped or we wouldn't already be doing it again. In which case it looks like xlu_cfg_get_string_default should return the literal undup'd default and let the caller take care of dupping it.

Presumably this is the same in the other cases too.

+int xlu_cfg_get_long_default(const XLU_Config *cfg, const char *n,

Quoted 7 lines
tools/libxl/libxlu_cfg.c +7 -0
+                     long *value_r, long def) {
+    long l;
+    XLU_ConfigSetting *set;
+    int e;
+    char *ep;
+
+    e= find_atom(cfg,n,&set);  if (e) { *value_r = def; return 0; }

tools/libxl/libxlu_cfg.c int xlu_cfg_get_long(const XLU_Config *c · line 217

Quoted 10 lines
tools/libxl/libxlu_cfg.c +10 -0
+    errno= 0; l= strtol(set->values[0], &ep, 0);
+    e= errno;
+    if (errno) {
+        e= errno;
+        assert(e==EINVAL || e==ERANGE);
+        fprintf(cfg->report,
+                "%s:%d: warning: parameter `%s' could not be parsed"
+                " as a number: %s\n",
+                cfg->filename, set->lineno, n, strerror(e));
+        *value_r = def;

It is unclear if the default should be used or a more serious error raised in the case of failure to parse the value if it is present, as opposed to the value not being present. I don't think you are changing the existing semantics though.

tools/libxl/libxlu_cfg.c int xlu_cfg_get_long(const XLU_Config *c

Quoted 5 lines
tools/libxl/libxlu_cfg.c +5 -0
+        return e;
+    }
+    if (*ep || ep==set->values[0]) {
+        fprintf(cfg->report,
+                "%s:%d: warning: parameter `%s' is not a valid number

\n",

tools/libxl/libxlu_cfg.c int xlu_cfg_get_long(const XLU_Config *c · line 227

Quoted 5 lines
tools/libxl/libxlu_cfg.c +5 -0
+                cfg->filename, set->lineno, n);
+        return EINVAL;
+    }
+    *value_r= l;
+    return 0;

Ian.

#2 lore raw
Quoted 9 lines · Ian Campbell
Quoted 8 lines · Gianni Tedesco

The function init_dm_info() in xl_cmdimpl.c is used to initialise a libxl_device_model_info structure with some default values. After being called, some of those values are overridden. For the string values which are not overwritten we either end up with a double free or attempt to free a string literal resulting in a segfault. This type of usage model would be complex to fix by adding strdup()'s in the init function and then checking and freeing when over-writing.

Quoted 7 lines
Quoted 2 lines

My proposed solution is to add default versions of xlu_cfg_get_string and xlu_cfg_get_long.

I like the idea but is it not possible to implement these as wrappers around the non-default providing versions and therefore avoid duplicating the code? (or maybe the other way round, defining the non-default variants in terms of default==NULL etc).

see discussion below

Quoted 8 lines

tools/libxl/xl_cmdimpl.c skip_vfb: · line 993

Quoted 4 lines
         /* then process config related to dm */
tools/libxl/xl_cmdimpl.c +1 -1
-        if (!xlu_cfg_get_string (config, "device_model", &buf))
+        if (!xlu_cfg_get_string_default (config, "device_model", &buf, "qemu-dm"))
             dm_info->device_model = strdup(buf);

Hasn't buf already been strdupped by xlu_cfg_get_string_default if the default ends up being used?

Quoted 6 lines

I'm not sure about set->values[0] in the other case but presumably it is not already dupped or we wouldn't already be doing it again. In which case it looks like xlu_cfg_get_string_default should return the literal undup'd default and let the caller take care of dupping it.

Presumably this is the same in the other cases too.

Yes that is broken, it leaks. Will fix and re-send.

Quoted 19 lines

+int xlu_cfg_get_long_default(const XLU_Config *cfg, const char *n,

Quoted 7 lines
tools/libxl/libxlu_cfg.c +7 -0
+                     long *value_r, long def) {
+    long l;
+    XLU_ConfigSetting *set;
+    int e;
+    char *ep;
+
+    e= find_atom(cfg,n,&set);  if (e) { *value_r = def; return 0; }

tools/libxl/libxlu_cfg.c int xlu_cfg_get_long(const XLU_Config *c · line 217

Quoted 10 lines
tools/libxl/libxlu_cfg.c +10 -0
+    errno= 0; l= strtol(set->values[0], &ep, 0);
+    e= errno;
+    if (errno) {
+        e= errno;
+        assert(e==EINVAL || e==ERANGE);
+        fprintf(cfg->report,
+                "%s:%d: warning: parameter `%s' could not be parsed"
+                " as a number: %s\n",
+                cfg->filename, set->lineno, n, strerror(e));
+        *value_r = def;
Quoted 4 lines

It is unclear if the default should be used or a more serious error raised in the case of failure to parse the value if it is present, as opposed to the value not being present. I don't think you are changing the existing semantics though.

I implemented these semantics since it reflects current practice where callers do not check the return value. It is also why I had duplicated the code. Not sure exactly what to do with this...

I am all in favour of pressing on after getting some bad digits, abort() would be too harsh, but converting all callers to distinguish between not-present, present-and-valid and present-but-not-valid would be a lot of extra lines of code.

Also, in xm, wouldn't a lot of these integers used as boolean be totally valid if assigned with True/False?

Gianni

#3 lore raw
In reply to Gianni Tedesco

Gianni Tedesco writes ("[PATCH]: xl: introduce xlu_cfg_get_(string|long)_default"):

Quoted 11 lines

The function init_dm_info() in xl_cmdimpl.c is used to initialise a libxl_device_model_info structure with some default values. After being called, some of those values are overridden. For the string values which are not overwritten we either end up with a double free or attempt to free a string literal resulting in a segfault. This type of usage model would be complex to fix by adding strdup()'s in the init function and then checking and freeing when over-writing.

My proposed solution is to add default versions of xlu_cfg_get_string and xlu_cfg_get_long. This way both double-free's and leaks are avoided and arguably the intention of the code is clearer.

I think in general the approach of trying to do the defaults one config setting at a time like this is insufficiently powerful.

If we're going to change it, it would be better to do all the default setting at the end, after the config file has been completely read. That means that the default for one config setting can depend on the actual values for another.

It also gets rid of the memory management problem, because defaults never need to be applied to anything that is a non-null pointer.

Ian.

#4 lore raw
Quoted 16 lines · Ian Jackson

Gianni Tedesco writes ("[PATCH]: xl: introduce xlu_cfg_get_(string|long)_default"):

Quoted 11 lines

The function init_dm_info() in xl_cmdimpl.c is used to initialise a libxl_device_model_info structure with some default values. After being called, some of those values are overridden. For the string values which are not overwritten we either end up with a double free or attempt to free a string literal resulting in a segfault. This type of usage model would be complex to fix by adding strdup()'s in the init function and then checking and freeing when over-writing.

My proposed solution is to add default versions of xlu_cfg_get_string and xlu_cfg_get_long. This way both double-free's and leaks are avoided and arguably the intention of the code is clearer.

I think in general the approach of trying to do the defaults one config setting at a time like this is insufficiently powerful.

Quoted 7 lines

If we're going to change it, it would be better to do all the default setting at the end, after the config file has been completely read. That means that the default for one config setting can depend on the actual values for another.

It also gets rid of the memory management problem, because defaults never need to be applied to anything that is a non-null pointer.

I had considered that approach first but then discarded it because it was hard to tell which values had been set or not, eg. if a boolean option had been set to 0 by an explicit config entry or due to initial memset() - meaning that you wouldn't know whether to override it with a default or not. IOW we would need a bitmap or something to keep track.

Perhaps a higher level API would be better:

struct {
...

}configs[] = {
    {.type = STR, .u.str = {&dm_info.vnclisten, "127.0.0.1"}, },
    {.type = INT, .u.num = {&dm_info.vnc, 1}, },
    ...
};

Gianni

#5 lore raw

Gianni Tedesco writes ("Re: [PATCH]: xl: introduce xlu_cfg_get_(string|long)_default"):

Quoted 5 lines

I had considered that approach first but then discarded it because it was hard to tell which values had been set or not, eg. if a boolean option had been set to 0 by an explicit config entry or due to initial memset() - meaning that you wouldn't know whether to override it with a default or not. IOW we would need a bitmap or something to keep track.

Yes. Either a bitmap for which values are valid, or a convention that booleans are ints which are given -1 or +1 values, or something.

Quoted 1 line

Perhaps a higher level API would be better:

...

Quoted 1 line
    {.type = STR, .u.str = {&dm_info.vnclisten, "127.0.0.1"}, },

Urgh, that's overly complicated, I think.

Ian.