Skip to content

Commit

Permalink
Fixed some constness inconsistencies in lookup APIs.
Browse files Browse the repository at this point in the history
Also check for NULL name in lookup functions.
  • Loading branch information
hyperrealm committed Nov 22, 2022
1 parent 5ff6aec commit cca09fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
18 changes: 18 additions & 0 deletions doc/libconfig.texi
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,15 @@ setting was not found.

@end deftypefun

@deftypefun {const config_setting_t *} config_lookup_const (@w{const config_t * @var{config}}, @w{const char * @var{path}})

@b{Since @i{v1.7.4}}

This function is identical to @code{config_lookup()}, except that the
setting is returned as a @code{const} structure.

@end deftypefun

@deftypefun {config_setting_t *} config_setting_lookup (@w{const config_setting_t * @var{setting}}, @w{const char * @var{path}})

This function locates a setting by a path @var{path} relative to
Expand All @@ -1084,6 +1093,15 @@ setting was not found.

@end deftypefun

@deftypefun {const config_setting_t *} config_setting_lookup_const (@w{const config_setting_t * @var{setting}}, @w{const char * @var{path}})

@b{Since @i{v1.7.4}}

This function is identical to @code{config_setting_lookup()}, except that the
setting is returned as a @code{const} structure.

@end deftypefun

@deftypefun int config_setting_get_int (@w{const config_setting_t * @var{setting}})
@deftypefunx {long long} config_setting_get_int64 (@w{const config_setting_t * @var{setting}})
@deftypefunx double config_setting_get_float (@w{const config_setting_t * @var{setting}})
Expand Down
27 changes: 23 additions & 4 deletions lib/libconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,11 @@ unsigned short config_setting_get_format(const config_setting_t *setting)

/* ------------------------------------------------------------------------- */

config_setting_t *config_setting_lookup(config_setting_t *setting,
const char *path)
const config_setting_t *config_setting_lookup_const(
const config_setting_t *setting, const char *path)
{
const char *p = path;
config_setting_t *found = setting;
const config_setting_t *found = setting;

while(*p && found)
{
Expand Down Expand Up @@ -1240,13 +1240,29 @@ config_setting_t *config_setting_lookup(config_setting_t *setting,

/* ------------------------------------------------------------------------- */

config_setting_t *config_setting_lookup(const config_setting_t *setting,
const char *path)
{
return((config_setting_t *)config_setting_lookup_const(setting, path));
}

/* ------------------------------------------------------------------------- */

config_setting_t *config_lookup(const config_t *config, const char *path)
{
return(config_setting_lookup(config->root, path));
}

/* ------------------------------------------------------------------------- */

const config_setting_t *config_lookup_const(const config_t *config,
const char *path)
{
return(config_setting_lookup(config->root, path));
}

/* ------------------------------------------------------------------------- */

int config_lookup_string(const config_t *config, const char *path,
const char **value)
{
Expand Down Expand Up @@ -1554,6 +1570,9 @@ config_setting_t *config_setting_get_member(const config_setting_t *setting,
if(setting->type != CONFIG_TYPE_GROUP)
return(NULL);

if(!name)
return(NULL);

return(__config_list_search(setting->value.list, name, strlen(name), NULL));
}

Expand Down Expand Up @@ -1642,7 +1661,7 @@ int config_setting_remove(config_setting_t *parent, const char *name)
const char *settingName;
const char *lastFound;

if(! parent)
if(! parent || !name)
return(CONFIG_FALSE);

if(parent->type != CONFIG_TYPE_GROUP)
Expand Down
7 changes: 6 additions & 1 deletion lib/libconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,13 @@ extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting,

extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config,
const char *path);
extern LIBCONFIG_API const config_setting_t *config_lookup_const(
const config_t *config, const char *path);

extern LIBCONFIG_API config_setting_t *config_setting_lookup(
config_setting_t *setting, const char *path);
const config_setting_t *setting, const char *path);
extern LIBCONFIG_API const config_setting_t *config_setting_lookup_const(
const config_setting_t *setting, const char *path);

extern LIBCONFIG_API int config_lookup_int(const config_t *config,
const char *path, int *value);
Expand Down

0 comments on commit cca09fa

Please sign in to comment.