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

Pass test suite with sanitizers #165

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions src/confuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# include <unistd.h>
#endif
#include <ctype.h>
#include <limits.h>

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
Expand Down Expand Up @@ -260,7 +261,7 @@ static long int cfg_opt_gettsecidx(cfg_opt_t *opt, const char *title)
}

static cfg_opt_t *cfg_getopt_secidx(cfg_t *cfg, const char *name,
long int *index)
unsigned int *index)
{
cfg_opt_t *opt = NULL;
cfg_t *sec = cfg;
Expand Down Expand Up @@ -312,13 +313,14 @@ static cfg_opt_t *cfg_getopt_secidx(cfg_t *cfg, const char *name,
break;
}

i = strtol(title, &endptr, 0);
if (*endptr != '\0')
errno = 0;
i = strtoul(title, &endptr, 0);
if (*endptr != '\0' || errno || i > UINT_MAX)
i = -1;
} while(0);

if (index)
*index = i;
*index = i >= 0 ? i : UINT_MAX;

sec = i >= 0 ? cfg_opt_getnsec(opt, i) : NULL;
if (!sec && !is_set(CFGF_IGNORE_UNKNOWN, cfg->flags)) {
Expand Down Expand Up @@ -595,7 +597,7 @@ DLLIMPORT cfg_t *cfg_gettsec(cfg_t *cfg, const char *name, const char *title)
DLLIMPORT cfg_t *cfg_getsec(cfg_t *cfg, const char *name)
{
cfg_opt_t *opt;
long int index;
unsigned int index;

opt = cfg_getopt_secidx(cfg, name, &index);
return cfg_opt_getnsec(opt, index);
Expand Down Expand Up @@ -671,7 +673,8 @@ static cfg_opt_t *cfg_dupopt_array(cfg_opt_t *opts)
if (!dupopts)
return NULL;

memcpy(dupopts, opts, n * sizeof(cfg_opt_t));
if (n)
memcpy(dupopts, opts, n * sizeof(cfg_opt_t));

for (i = 0; i < n; i++) {
/* Clear dynamic ptrs, protecting the original on failure */
Expand Down Expand Up @@ -2377,7 +2380,7 @@ DLLIMPORT int cfg_rmnsec(cfg_t *cfg, const char *name, unsigned int index)
DLLIMPORT int cfg_rmsec(cfg_t *cfg, const char *name)
{
cfg_opt_t *opt;
long int index;
unsigned int index;

opt = cfg_getopt_secidx(cfg, name, &index);
return cfg_opt_rmnsec(opt, index);
Expand Down
16 changes: 13 additions & 3 deletions tests/empty_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@ int main(void)
fail_unless(strcmp(cfg_getstr(cfg, "string"), "") == 0);
f = fmemopen(buf, sizeof(buf), "w+");
fail_unless(f != NULL);
cfg_print(cfg, f);
fail_unless(cfg_print(cfg, f) == CFG_SUCCESS);
cfg_free(cfg);

#if defined(__has_feature)
# if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
/* Skip check since fmemopen(2) is broken with sanitizers, see
* https://github.com/google/sanitizers/issues/628
*/
# else
/*
* try to reload the generated temporary config file to check
* that the default is indeed overridden by an empty string
*/
cfg = cfg_init(opts, 0);
fseek(f, 0L, SEEK_SET);
fail_unless(cfg != NULL);
fail_unless(fseek(f, 0L, SEEK_SET) == 0);
fail_unless(cfg_parse_fp(cfg, f) == CFG_SUCCESS);
fclose(f);
fail_unless(strcmp(cfg_getstr(cfg, "string"), "") == 0);
cfg_free(cfg);
# endif
#endif

fclose(f);

return 0;
}
Expand Down
12 changes: 10 additions & 2 deletions tests/print_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,31 @@ int main(void)
cfg_set_print_filter_func(cfg, no_foo);
f = fmemopen(buf, sizeof(buf), "w+");
fail_unless(f != NULL);
cfg_print(cfg, f);
fail_unless(cfg_print(cfg, f) == CFG_SUCCESS);
fclose(f);

#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
/* Skip check since fmemopen(2) is broken with sanitizers, see
* https://github.com/google/sanitizers/issues/628
*/
# else
fprintf(stderr, "no_foo filter:\n%s", buf);
fail_unless(strstr(buf, "foo-") == NULL);
fail_unless(strstr(buf, "bar-") != NULL);

cfg_set_print_filter_func(cfg, no_bar);
f = fmemopen(buf, sizeof(buf), "w+");
fail_unless(f != NULL);
cfg_print(cfg, f);
fail_unless(cfg_print(cfg, f) == CFG_SUCCESS);
fclose(f);

fprintf(stderr, "----\n");
fprintf(stderr, "no_bar filter:\n%s", buf);
fail_unless(strstr(buf, "foo-") != NULL);
fail_unless(strstr(buf, "bar-") == NULL);
# endif
#endif

cfg_free(cfg);

Expand Down