Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzzy_calculate_roles asserts triggers when text's length is greater than roles's #316

Closed
harold-b opened this issue Mar 9, 2024 · 1 comment

Comments

@harold-b
Copy link
Contributor

harold-b commented Mar 9, 2024

The assert(len(text) == len(roles)) @ fuzzy.odin#L193 triggers whenever text is of greater length than roles.

This seems like an incorrect assumption is made in fuzzy_calculate_roles given fuzzy.odin#L231 allows for text's length to be greater, yet the slice passed to fuzzy_calculate_roles will never be as it still takes the minimum length of the two.

This was causing a crash when a proc length was greater than max_word.

Applying the following patch fixed the crash, where the minimum of both is taken again in fuzzy_calculate_roles as it is done in fuzzy_init. I am not familiar with the rest of the semantics that take place in fuzzy_calculate_roles, so I did not consider this PR worthy -- though I am happy to make one if this is a good enough fix.

diff --git a/src/common/fuzzy.odin b/src/common/fuzzy.odin
index c425051..86b1244 100644
--- a/src/common/fuzzy.odin
+++ b/src/common/fuzzy.odin
@@ -190,9 +190,9 @@ fuzzy_is_awful :: proc(s: int) -> bool {
 
 fuzzy_calculate_roles :: proc(text: string, roles: ^[]FuzzyCharRole) -> FuzzyCharTypeSet {
 
-	assert(len(text) == len(roles))
+	text_length := min(len(text), len(roles))
 
-	if len(text) == 0 {
+	if text_length == 0 {
 		return 0
 	}
 
@@ -202,7 +202,7 @@ fuzzy_calculate_roles :: proc(text: string, roles: ^[]FuzzyCharRole) -> FuzzyCha
 
 	types := type
 
-	for i := 0; i < len(text) - 1; i += 1 {
+	for i := 0; i < text_length - 1; i += 1 {
 		type = cast(FuzzyCharType)fuzzy_packed_lookup(char_types, cast(uint)text[i + 1])
 		type_set |= 1 << cast(uint)type
 
@@ -213,7 +213,7 @@ fuzzy_calculate_roles :: proc(text: string, roles: ^[]FuzzyCharRole) -> FuzzyCha
 
 	fuzzy_rotate(.Empty, &types)
 
-	roles[len(text) - 1] = cast(FuzzyCharRole)fuzzy_packed_lookup(char_roles, cast(uint)types)
+	roles[text_length - 1] = cast(FuzzyCharRole)fuzzy_packed_lookup(char_roles, cast(uint)types)
 
 	return type_set
 }
@DanielGavin
Copy link
Owner

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants