diff --git a/include/globalvar.h b/include/globalvar.h index 6e10956..e77209b 100644 --- a/include/globalvar.h +++ b/include/globalvar.h @@ -60,7 +60,7 @@ } static inline int globalvar_add_simple_enum(const char *name, - int *value, const char **names, int max) + int *value, const char * const *names, int max) { struct param_d *p; @@ -117,7 +117,7 @@ } static inline int globalvar_add_simple_enum(const char *name, - int *value, const char **names, int max) + int *value, const char * const *names, int max) { return 0; } diff --git a/include/param.h b/include/param.h index 3a851fc..3fb4740 100644 --- a/include/param.h +++ b/include/param.h @@ -50,7 +50,7 @@ struct param_d *dev_add_param_enum(struct device_d *dev, const char *name, int (*set)(struct param_d *p, void *priv), int (*get)(struct param_d *p, void *priv), - int *value, const char **names, int max, void *priv); + int *value, const char * const *names, int max, void *priv); struct param_d *dev_add_param_int_ro(struct device_d *dev, const char *name, int value, const char *format); @@ -120,7 +120,7 @@ static inline struct param_d *dev_add_param_enum(struct device_d *dev, const char *name, int (*set)(struct param_d *p, void *priv), int (*get)(struct param_d *p, void *priv), - int *value, const char **names, int max, void *priv) + int *value, const char * const *names, int max, void *priv) { return ERR_PTR(-ENOSYS); diff --git a/lib/parameter.c b/lib/parameter.c index b238859..fd05b49 100644 --- a/lib/parameter.c +++ b/lib/parameter.c @@ -389,8 +389,8 @@ struct param_enum { struct param_d param; - int *value; - const char **names; + unsigned int *value; + const char * const *names; int num_names; int (*set)(struct param_d *p, void *priv); int (*get)(struct param_d *p, void *priv); @@ -441,7 +441,11 @@ } free(p->value); - p->value = strdup(pe->names[*pe->value]); + + if (*pe->value >= pe->num_names) + p->value = asprintf("invalid:%d", *pe->value); + else + p->value = strdup(pe->names[*pe->value]); return p->value; } @@ -467,7 +471,7 @@ struct param_d *dev_add_param_enum(struct device_d *dev, const char *name, int (*set)(struct param_d *p, void *priv), int (*get)(struct param_d *p, void *priv), - int *value, const char **names, int num_names, void *priv) + int *value, const char * const *names, int num_names, void *priv) { struct param_enum *pe; struct param_d *p;