Skip to content

Commit

Permalink
Fix binary search returning too early if m == 0 but next index would …
Browse files Browse the repository at this point in the history
…be 1
  • Loading branch information
Rot127 committed Sep 9, 2024
1 parent af4b72d commit 4ae6215
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions suite/cstest/include/test_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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
};
Expand Down
1 change: 1 addition & 0 deletions suite/cstest/src/test_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions suite/cstest/test/src/unit_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 4ae6215

Please sign in to comment.