Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid clobbering CFGF_RESET when cfg_setmulti fails #135

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/confuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ DLLIMPORT int cfg_opt_setmulti(cfg_t *cfg, cfg_opt_t *opt, unsigned int nvalues,
cfg_free_value(opt);
opt->nvalues = old.nvalues;
opt->values = old.values;
opt->flags &= ~CFGF_RESET;
opt->flags |= old.flags & CFGF_RESET;

return CFG_FAIL;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TESTS += ignore_parm
TESTS += annotate
TESTS += empty_string
TESTS += setopt_ptr
TESTS += setmulti_reset

check_PROGRAMS = $(TESTS)

Expand Down
35 changes: 35 additions & 0 deletions tests/setmulti_reset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "check_confuse.h"

int main(void)
{
cfg_opt_t opts[] = {
CFG_INT_LIST("int", "{1,2}", CFGF_NONE),
CFG_END()
};

/* Calling cfg_setopt after cfg_init should not append to the list. */
cfg_t *cfg = cfg_init(opts, 0);
fail_unless(cfg_size(cfg, "int") == 2);
fail_unless(cfg_setopt(cfg, cfg_getopt(cfg, "int"), "3"));
fail_unless(cfg_size(cfg, "int") == 1);
cfg_free(cfg);

/* Not even if you first attempt to use cfg_setmulti with bad input. */
cfg = cfg_init(opts, 0);
char *bad[] = { "bad" };
fail_unless(cfg_size(cfg, "int") == 2);
fail_unless(cfg_setmulti(cfg, "int", 1, bad) == CFG_FAIL);
fail_unless(cfg_size(cfg, "int") == 2);
fail_unless(cfg_setopt(cfg, cfg_getopt(cfg, "int"), "3"));
fail_unless(cfg_size(cfg, "int") == 1);
cfg_free(cfg);

return 0;
}

/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/