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

hash: fix hash item offset calculating in uhash.dat #255

Merged
merged 6 commits into from Aug 9, 2017

Conversation

Projects
None yet
3 participants
@cwlin
Contributor

cwlin commented Feb 17, 2017

When a userphrase is going to be removed, HashModify calculates the offset according to its item_index.

pItem->item_index * FIELD_SIZE + 4 + strlen(BIN_HASH_SIG)

However, the item_index will be shifted if there has previously removed item in uhash.dat.

Considering the following example:
chewing_userphrase_add(ctx, p1, b)
chewing_userphrase_add(ctx, p2, b)

item_index wordSeq offset in uhash.dat
0 p1 8
1 p2 133

chewing_userphrase_remove(ctx, p1, b)

item_index wordSeq offset in uhash.dat
p1' 8
1 p2 133

When libchewing restarts, InitUserphrase calls ReadHashItem_bin, it will find that p1' is invalid.
So the item_index of p2 will be set to 0.

item_index wordSeq offset in uhash.dat
p1' 8
0 p2 133

When p2 is going to be removed, the offset of p2 in uhash.dat will be wrong.

pItem->item_index * FIELD_SIZE + 4 + strlen(BIN_HASH_SIG)
= 0 * FIELD_SIZE + 4 + strlen(BIN_HASH_SIG)
= 8

In this case, chewing_userphrase_remove returns success, but p2 won't be removed (marked as invalid) in uhash.dat.
Next time the libchewing starts, p2 will still be there since it is valid in uhash.dat.
Moreover, this problem could also impact the userphrase modification, or deletes an unexcepted one in uhash.dat.

This fix tries to find the physical offset in uhash.dat based on the wordSeq.
By this method, HashModify will update the correct record in uhash.dat.

Show outdated Hide outdated src/hash.c
Show outdated Hide outdated src/hash.c
@coveralls

This comment has been minimized.

Show comment
Hide comment
@coveralls

coveralls Aug 5, 2017

Coverage Status

Changes Unknown when pulling 7efa9bc on cwlin:fix_hash_item_offset into ** on chewing:master**.

coveralls commented Aug 5, 2017

Coverage Status

Changes Unknown when pulling 7efa9bc on cwlin:fix_hash_item_offset into ** on chewing:master**.

cwlin
Show outdated Hide outdated src/hash.c
Show outdated Hide outdated src/hash.c

@jserv jserv merged commit 5d02863 into chewing:master Aug 9, 2017

0 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
coverage/coveralls Coverage pending from Coveralls.io
Details
@coveralls

This comment has been minimized.

Show comment
Hide comment
@coveralls

coveralls Aug 9, 2017

Coverage Status

Changes Unknown when pulling f4cc674 on cwlin:fix_hash_item_offset into ** on chewing:master**.

coveralls commented Aug 9, 2017

Coverage Status

Changes Unknown when pulling f4cc674 on cwlin:fix_hash_item_offset into ** on chewing:master**.

@cwlin cwlin deleted the cwlin:fix_hash_item_offset branch Aug 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment