diff mbox

[pacman-dev] do not rely on name hashes for matching

Message ID 20171221042236.1667-1-andrew.gregory.8@gmail.com
State Accepted, archived
Headers show

Commit Message

Andrew Gregory Dec. 21, 2017, 4:22 a.m. UTC
6cfc4757b98e813428d261dbc185e20618ca83a6 was overzealous in attempting
to optimize away a call to strcmp based on a comparison of hashes.  The
call can be skipped if the hashes are different, but different strings
could have the same hash.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
---
 lib/libalpm/deps.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Allan McRae Jan. 6, 2018, 3:36 a.m. UTC | #1
On 21/12/17 14:22, Andrew Gregory wrote:
> 6cfc4757b98e813428d261dbc185e20618ca83a6 was overzealous in attempting
> to optimize away a call to strcmp based on a comparison of hashes.  The
> call can be skipped if the hashes are different, but different strings
> could have the same hash.
> 
> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
> ---

Good catch!  I'm interest if you saw an issue in the wild?

A
Andrew Gregory Jan. 6, 2018, 5:41 a.m. UTC | #2
On 01/06/18 at 01:36pm, Allan McRae wrote:
> On 21/12/17 14:22, Andrew Gregory wrote:
> > 6cfc4757b98e813428d261dbc185e20618ca83a6 was overzealous in attempting
> > to optimize away a call to strcmp based on a comparison of hashes.  The
> > call can be skipped if the hashes are different, but different strings
> > could have the same hash.
> > 
> > Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
> > ---
> 
> Good catch!  I'm interest if you saw an issue in the wild?
> 
> A

Nope, just noticed it as I was looking through the deps code.

apg
diff mbox

Patch

diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
index 96f91739..3d3f8ef2 100644
--- a/lib/libalpm/deps.c
+++ b/lib/libalpm/deps.c
@@ -698,10 +698,8 @@  static alpm_pkg_t *resolvedep(alpm_handle_t *handle, alpm_depend_t *dep,
 		}
 		for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
 			alpm_pkg_t *pkg = j->data;
-			/* with hash != hash, we can even skip the strcmp() as we know they can't
-			 * possibly be the same string */
-			if(pkg->name_hash != dep->name_hash && _alpm_depcmp(pkg, dep)
-					&& !alpm_pkg_find(excluding, pkg->name)) {
+			if((pkg->name_hash != dep->name_hash || strcmp(pkg->name, dep->name) != 0)
+					&& _alpm_depcmp(pkg, dep) && !alpm_pkg_find(excluding, pkg->name)) {
 				if(alpm_pkg_should_ignore(handle, pkg)) {
 					alpm_question_install_ignorepkg_t question = {
 						.type = ALPM_QUESTION_INSTALL_IGNOREPKG,