Skip to content

Commit 86d0491

Browse files
committed
Fix parsing boolean config values when using git_config_get_mapped and git_config_lookup_map_value
Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 1e98752 commit 86d0491

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ int git_config_lookup_map_value(
12381238
case GIT_CONFIGMAP_TRUE: {
12391239
int bool_val;
12401240

1241-
if (git__parse_bool(&bool_val, value) == 0 &&
1241+
if (git_config_parse_bool(&bool_val, value) == 0 &&
12421242
bool_val == (int)m->type) {
12431243
*out = m->map_value;
12441244
return 0;

tests/config/read.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,3 +928,62 @@ void test_config_read__nosection(void)
928928
git_buf_dispose(&buf);
929929
git_config_free(cfg);
930930
}
931+
932+
enum {
933+
MAP_TRUE = 0,
934+
MAP_FALSE = 1,
935+
MAP_ALWAYS = 2
936+
};
937+
938+
static git_configmap _test_map1[] = {
939+
{GIT_CONFIGMAP_STRING, "always", MAP_ALWAYS},
940+
{GIT_CONFIGMAP_FALSE, NULL, MAP_FALSE},
941+
{GIT_CONFIGMAP_TRUE, NULL, MAP_TRUE},
942+
};
943+
944+
static git_configmap _test_map2[] = {
945+
{GIT_CONFIGMAP_INT32, NULL, 0},
946+
};
947+
948+
void test_config_read__get_mapped(void)
949+
{
950+
git_config *cfg;
951+
int val;
952+
int known_good;
953+
954+
cl_set_cleanup(&clean_test_config, NULL);
955+
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");
956+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
957+
958+
// check aprsing bool and string
959+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1)));
960+
cl_assert_equal_i(val, MAP_TRUE);
961+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1)));
962+
cl_assert_equal_i(val, MAP_TRUE);
963+
964+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1)));
965+
cl_assert_equal_i(val, MAP_ALWAYS);
966+
967+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1)));
968+
cl_assert_equal_i(val, MAP_FALSE);
969+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1)));
970+
cl_assert_equal_i(val, MAP_FALSE);
971+
972+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1)));
973+
974+
// check parsing int values
975+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2)));
976+
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1"));
977+
cl_assert_equal_i(val, known_good);
978+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2)));
979+
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6"));
980+
cl_assert_equal_i(val, known_good);
981+
982+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2)));
983+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2)));
984+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2)));
985+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2)));
986+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2)));
987+
988+
git_config_free(cfg);
989+
}

0 commit comments

Comments
 (0)