Skip to content

Commit a1f0135

Browse files
authored
Merge pull request libgit2#5626 from csware/parse_bool
boolean config parsing fails in some cases with mapped values
2 parents 36dc681 + c464f12 commit a1f0135

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/config.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,6 @@ int git_config_lookup_map_value(
12271227
{
12281228
size_t i;
12291229

1230-
if (!value)
1231-
goto fail_parse;
1232-
12331230
for (i = 0; i < map_n; ++i) {
12341231
const git_configmap *m = maps + i;
12351232

@@ -1238,7 +1235,7 @@ int git_config_lookup_map_value(
12381235
case GIT_CONFIGMAP_TRUE: {
12391236
int bool_val;
12401237

1241-
if (git__parse_bool(&bool_val, value) == 0 &&
1238+
if (git_config_parse_bool(&bool_val, value) == 0 &&
12421239
bool_val == (int)m->type) {
12431240
*out = m->map_value;
12441241
return 0;
@@ -1252,15 +1249,14 @@ int git_config_lookup_map_value(
12521249
break;
12531250

12541251
case GIT_CONFIGMAP_STRING:
1255-
if (strcasecmp(value, m->str_match) == 0) {
1252+
if (value && strcasecmp(value, m->str_match) == 0) {
12561253
*out = m->map_value;
12571254
return 0;
12581255
}
12591256
break;
12601257
}
12611258
}
12621259

1263-
fail_parse:
12641260
git_error_set(GIT_ERROR_CONFIG, "failed to map '%s'", value);
12651261
return -1;
12661262
}

tests/config/read.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,3 +928,79 @@ 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"
956+
" key1 = 1\n"
957+
" key2 = true\n"
958+
" key3\n"
959+
" key4 = always\n"
960+
" key5 = false\n"
961+
" key6 = 0\n"
962+
" key7 = never\n"
963+
" key8 = On\n"
964+
" key9 = off\n");
965+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
966+
967+
// check parsing bool and string
968+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1)));
969+
cl_assert_equal_i(val, MAP_TRUE);
970+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1)));
971+
cl_assert_equal_i(val, MAP_TRUE);
972+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key3", _test_map1, ARRAY_SIZE(_test_map1)));
973+
cl_assert_equal_i(val, MAP_TRUE);
974+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key8", _test_map1, ARRAY_SIZE(_test_map1)));
975+
cl_assert_equal_i(val, MAP_TRUE);
976+
977+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1)));
978+
cl_assert_equal_i(val, MAP_ALWAYS);
979+
980+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1)));
981+
cl_assert_equal_i(val, MAP_FALSE);
982+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1)));
983+
cl_assert_equal_i(val, MAP_FALSE);
984+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key9", _test_map1, ARRAY_SIZE(_test_map1)));
985+
cl_assert_equal_i(val, MAP_FALSE);
986+
987+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1)));
988+
989+
// check parsing int values
990+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2)));
991+
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1"));
992+
cl_assert_equal_i(val, known_good);
993+
cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2)));
994+
cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6"));
995+
cl_assert_equal_i(val, known_good);
996+
997+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2)));
998+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2)));
999+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2)));
1000+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2)));
1001+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2)));
1002+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key8", _test_map2, ARRAY_SIZE(_test_map2)));
1003+
cl_git_fail(git_config_get_mapped(&val, cfg, "header.key9", _test_map2, ARRAY_SIZE(_test_map2)));
1004+
1005+
git_config_free(cfg);
1006+
}

0 commit comments

Comments
 (0)