Skip to content

Commit 8bb4e72

Browse files
maxploterrwinch
authored andcommitted
Prevent StackOverflowError for AccessControlEntryImpl.hashCode
Getting StackOverflowError when invoke AclImpl.hashCode because of cross-references between AclImpl and AccessControlEntryImpl Remove from AccessControlEntryImpl.hashCode method invocation of acl.hashCode fixes gh-5401
1 parent f58a262 commit 8bb4e72

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

acl/src/main/java/org/springframework/security/acls/domain/AccessControlEntryImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,10 +131,9 @@ public boolean equals(Object arg0) {
131131

132132
@Override
133133
public int hashCode() {
134-
int result = this.acl.hashCode();
135-
result = 31 * result + this.permission.hashCode();
134+
int result = this.permission.hashCode();
136135
result = 31 * result + (this.id != null ? this.id.hashCode() : 0);
137-
result = 31 * result + this.sid.hashCode();
136+
result = 31 * result + (this.sid.hashCode());
138137
result = 31 * result + (this.auditFailure ? 1 : 0);
139138
result = 31 * result + (this.auditSuccess ? 1 : 0);
140139
result = 31 * result + (this.granting ? 1 : 0);

acl/src/test/java/org/springframework/security/acls/domain/AclImplTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -560,6 +560,25 @@ public void changingParentIsSuccessful() throws Exception {
560560
childAcl.setParent(changeParentAcl);
561561
}
562562

563+
@Test
564+
public void hashCodeWithoutStackOverFlow() throws Exception {
565+
//given
566+
Sid sid = new PrincipalSid("pSid");
567+
ObjectIdentity oid = new ObjectIdentityImpl("type", 1);
568+
AclAuthorizationStrategy authStrategy = new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("role"));
569+
PermissionGrantingStrategy grantingStrategy = new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger());
570+
571+
AclImpl acl = new AclImpl(oid, 1L, authStrategy, grantingStrategy, null, null, false, sid);
572+
AccessControlEntryImpl ace = new AccessControlEntryImpl(1L, acl, sid, BasePermission.READ, true, true, true);
573+
574+
Field fieldAces = FieldUtils.getField(AclImpl.class, "aces");
575+
fieldAces.setAccessible(true);
576+
List<AccessControlEntryImpl> aces = (List<AccessControlEntryImpl>) fieldAces.get(acl);
577+
aces.add(ace);
578+
//when - then none StackOverFlowError been raised
579+
ace.hashCode();
580+
}
581+
563582
// ~ Inner Classes
564583
// ==================================================================================================
565584

0 commit comments

Comments
 (0)