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

cfg_t *cfg_addtsec(): can't return the pointer to the section if already exists #158

Open
mewo4cor opened this issue Oct 15, 2021 · 0 comments

Comments

@mewo4cor
Copy link

mewo4cor commented Oct 15, 2021

Description

cfg_t *cfg_addtsec(cfg_t *cfg, const char *name, const char *title) can't return the pointer to the section if already exists.

Source Code Snippet

Definition

DLLIMPORT cfg_t *cfg_addtsec(cfg_t *cfg, const char *name, const char *title)
{
	cfg_opt_t *opt;
	cfg_value_t *val;

	if (cfg_gettsec(cfg, name, title))
		return NULL;

	opt = cfg_getopt(cfg, name);
	if (!opt) {
		cfg_error(cfg, _("no such option '%s'"), name);
		return NULL;
	}
	val = cfg_setopt(cfg, opt, title);
	if (!val)
		return NULL;

	val->section->path = cfg->path; /* Remember global search path. */
	val->section->line = 1;
	val->section->errfunc = cfg->errfunc;

	return val->section;
}

Declaration

Note:
* @return A pointer to the created section orif the section already exists a pointer to that section is returned. If the section could not be created or found, 0 is returned.

/** Create a new titled config section.
 *
 * @param cfg The configuration file context.
 * @param name The name of the option.
 * @param title The title of this section.
 *
 * @return A pointer to the created section or if the section
 * already exists a pointer to that section is returned.
 * If the section could not be created or found, 0 is returned.
 */
DLLIMPORT cfg_t *cfg_addtsec(cfg_t *cfg, const char *name, const char *title);

Possible Resolution

 DLLIMPORT cfg_t *cfg_addtsec(cfg_t *cfg, const char *name, const char *title)
 {
+	cfg_t *sec;
 	cfg_opt_t *opt;
 	cfg_value_t *val;

+	sec = cfg_gettsec(cfg, name, title);
+	if (sec)
+		return sec;
-	if (cfg_gettsec(cfg, name, title))
-		return NULL;
 
 	opt = cfg_getopt(cfg, name);
 	if (!opt) {
 		cfg_error(cfg, _("no such option '%s'"), name);
 		return NULL;
 	}
 	val = cfg_setopt(cfg, opt, title);
 	if (!val)
 		return NULL;
 
 	val->section->path = cfg->path; /* Remember global search path. */
 	val->section->line = 1;
 	val->section->errfunc = cfg->errfunc;
 
 	return val->section;
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant