Skip to content

Commit

Permalink
Fix parsing boolean config values when using git_config_get_mapped an…
Browse files Browse the repository at this point in the history
…d git_config_lookup_map_value

(fixes issue libgit2#5555)

Signed-off-by: Sven Strickroth <email@cs-ware.de>
  • Loading branch information
csware committed Sep 9, 2020
1 parent 1e98752 commit a4b8600
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ int git_config_lookup_map_value(
case GIT_CONFIGMAP_TRUE: {
int bool_val;

if (git__parse_bool(&bool_val, value) == 0 &&
if (git_config_parse_bool(&bool_val, value) == 0 &&
bool_val == (int)m->type) {
*out = m->map_value;
return 0;
Expand Down
59 changes: 59 additions & 0 deletions tests/config/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,62 @@ void test_config_read__nosection(void)
git_buf_dispose(&buf);
git_config_free(cfg);
}

enum {
MAP_TRUE = 0,
MAP_FALSE = 1,
MAP_ALWAYS = 2
};

static git_configmap _test_map1[] = {
{GIT_CONFIGMAP_STRING, "always", MAP_ALWAYS},
{GIT_CONFIGMAP_FALSE, NULL, MAP_FALSE},
{GIT_CONFIGMAP_TRUE, NULL, MAP_TRUE},
};

static git_configmap _test_map2[] = {
{GIT_CONFIGMAP_INT32, NULL, 0},
};

void test_config_read__get_mapped(void)
{
git_config *cfg;
int val;
int known_good;

cl_set_cleanup(&clean_test_config, NULL);
cl_git_mkfile("./testconfig", "[header]\n key1 = 1\n key2 = true\n key3\n key4 = always\n key5 = false\n key6 = 0\n key7 = never\n");
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

// check aprsing bool and string
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1)));
cl_assert_equal_i(val, MAP_TRUE);
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1)));
cl_assert_equal_i(val, MAP_TRUE);

cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1)));
cl_assert_equal_i(val, MAP_ALWAYS);

cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1)));
cl_assert_equal_i(val, MAP_FALSE);
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1)));
cl_assert_equal_i(val, MAP_FALSE);

cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1)));

// check parsing int values
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1"));
cl_assert_equal_i(val, known_good);
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6"));
cl_assert_equal_i(val, known_good);

cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2)));
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2)));

git_config_free(cfg);
}

0 comments on commit a4b8600

Please sign in to comment.