After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 792196 - PotentialMatch: fix array indexing if first string is longer than second
PotentialMatch: fix array indexing if first string is longer than second
Status: RESOLVED FIXED
Product: folks
Classification: Platform
Component: general
git master
Other Linux
: Normal normal
: Unset
Assigned To: folks-maint
folks-maint
Depends on:
Blocks:
 
 
Reported: 2018-01-04 10:56 UTC by Niels De Graef
Modified: 2018-01-04 13:10 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
[Patch] Check for `pos` before doing trying to use it as index. (1.19 KB, patch)
2018-01-04 10:56 UTC, Niels De Graef
needs-work Details | Review
[Patch] Check for `pos` before doing trying to use it as index. (1.12 KB, patch)
2018-01-04 12:07 UTC, Niels De Graef
committed Details | Review

Description Niels De Graef 2018-01-04 10:56:24 UTC
Created attachment 366274 [details] [review]
[Patch] Check for `pos` before doing trying to use it as index.

In the `_matches` method of PotentialMatch, we iterate over the 1st string and try to find a match for the character on the same position in the 2nd string. If it's not the same, we also perform the same check within a margin of `max_dist`, *with* some boundary checks. On the original position however, it only does this check *after* already using it as index (potential-match.vala:668). If the 1st string is longer than the 2nd string, it will hence try to an out-of-bounds access on the 2nd string.

The attached patch fixes this by returning -1 in `_contains()` if the index is too large.




On a side note: I was curious why the `match-name.vala` is situated in the tests for the Tracker backend (which is disabled by default and which I can't even get building locally without some tinkering) rather than a general test.
Comment 1 Philip Withnall 2018-01-04 11:42:12 UTC
Review of attachment 366274 [details] [review]:

I don’t think this is quite right, since it discounts a match within the bounds of pos ± max_dist. For example, if pos = haystack.length, max_dist = 5, and haystack[haystack.length-1] = c, then -1 will be returned incorrectly.
Comment 2 Philip Withnall 2018-01-04 11:45:20 UTC
(In reply to Niels De Graef from comment #0)
> On a side note: I was curious why the `match-name.vala` is situated in the
> tests for the Tracker backend (which is disabled by default and which I
> can't even get building locally without some tinkering) rather than a
> general test.

Probably because it was written at the same time as the Tracker backend, I think.

I agree, it’s a bit of a weird place for the test. Moving it to tests/dummy/match-name.vala would be better, but it would need a bit of rework to use dummy personas rather than Tracker personas to provide the test data. That should be fairly straightforward (see how search-view.vala does it). I’d be really happy to review a patch for it if you want to open a separate bug about it. :-)
Comment 3 Niels De Graef 2018-01-04 12:07:46 UTC
Created attachment 366277 [details] [review]
[Patch] Check for `pos` before doing trying to use it as index.

(In reply to Philip Withnall from comment #2)
> (In reply to Niels De Graef from comment #0)
> > On a side note: I was curious why the `match-name.vala` is situated in the
> > tests for the Tracker backend (which is disabled by default and which I
> > can't even get building locally without some tinkering) rather than a
> > general test.
> 
> Probably because it was written at the same time as the Tracker backend, I
> think.
> 
> I agree, it’s a bit of a weird place for the test. Moving it to
> tests/dummy/match-name.vala would be better, but it would need a bit of
> rework to use dummy personas rather than Tracker personas to provide the
> test data. That should be fairly straightforward (see how search-view.vala
> does it). I’d be really happy to review a patch for it if you want to open a
> separate bug about it. :-)
Indeed, you're right. Great find!

The attached patch should fix this.
Comment 4 Philip Withnall 2018-01-04 12:23:24 UTC
Review of attachment 366277 [details] [review]:

Looks good. If you do end up moving the match-name.vala tests, it would be good to add some regression tests to it for this bug (and the other PotentialMatch) bug.
Comment 5 Niels De Graef 2018-01-04 13:09:55 UTC
Comment on attachment 366277 [details] [review]
[Patch] Check for `pos` before doing trying to use it as index.

Landed on master as commit 7a71777.
Comment 6 Niels De Graef 2018-01-04 13:10:25 UTC
Also made bug 792201 for the test migration. Thanks for the quick feedback again!