diff --git a/Mapping.c b/Mapping.c index 749d413f62..d2ef694d5d 100644 --- a/Mapping.c +++ b/Mapping.c @@ -434,7 +434,7 @@ uint64_t enum_map_bin_search(const cs_enum_id_map *map, size_t map_len, } else if (id[i] > map[m].str[j]) { l = m + 1; } - if (m == 0 || (l + r) / 2 >= map_len) { + if ((m == 0 && id[i] < map[m].str[j]) || (l + r) / 2 >= map_len) { // Break before we go out of bounds. break; } diff --git a/suite/cstest/include/test_mapping.h b/suite/cstest/include/test_mapping.h index 139cd6b78f..bab38b5545 100644 --- a/suite/cstest/include/test_mapping.h +++ b/suite/cstest/include/test_mapping.h @@ -201,6 +201,7 @@ static const TestOptionMapEntry test_option_map[] = { static const cs_enum_id_map cs_enum_map[] = { { .str = "AAAAAAAAAAAAAAAAAAAAAAAAAA", .val = 0xffffff }, // For testing + { .str = "AAAAAAAAAAAAAAAAAAAAAAAAAB", .val = 0xffffff }, // For testing { .str = "AARCH64LAYOUT_INVALID", .val = AARCH64LAYOUT_INVALID }, { .str = "AARCH64LAYOUT_VL_16B", .val = AARCH64LAYOUT_VL_16B }, { .str = "AARCH64LAYOUT_VL_16S", .val = AARCH64LAYOUT_VL_16S }, @@ -1472,6 +1473,8 @@ static const cs_enum_id_map cs_enum_map[] = { { .str = "XCORE_OP_IMM", .val = XCORE_OP_IMM }, { .str = "XCORE_OP_MEM", .val = XCORE_OP_MEM }, { .str = "XCORE_OP_REG", .val = XCORE_OP_REG }, + { .str = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzx", + .val = 0xffffff }, // For testing { .str = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", .val = 0xffffff }, // For testing }; diff --git a/suite/cstest/src/test_run.c b/suite/cstest/src/test_run.c index 3111f4d99e..e385908463 100644 --- a/suite/cstest/src/test_run.c +++ b/suite/cstest/src/test_run.c @@ -128,6 +128,7 @@ static bool parse_input_options(const TestInput *input, cs_arch *arch, } opt_arr[opt_idx++] = test_option_map[k].opt; opt_found = true; + break; } } if (!opt_found) { diff --git a/suite/cstest/test/src/unit_tests.c b/suite/cstest/test/src/unit_tests.c index fcc34e4be1..d8d6386809 100644 --- a/suite/cstest/test/src/unit_tests.c +++ b/suite/cstest/test/src/unit_tests.c @@ -19,6 +19,26 @@ bool test_cs_enum_get_val() val); return false; } + // Get second value + val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), + "AAAAAAAAAAAAAAAAAAAAAAAAAB", + &found); + if (!found || val != 0xffffff) { + fprintf(stderr, + "enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), AAAAAAAAAAAAAAAAAAAAAAAAAB) failed is %d.\n", + val); + return false; + } + + // Get second to last value + val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzx", &found); + if (!found || val != 0xffffff) { + fprintf(stderr, + "enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map), zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzx) failed is %d.\n", + val); + return false; + } // Get last value val = enum_map_bin_search(cs_enum_map, ARR_SIZE(cs_enum_map),