Skip to content

Commit abfb630

Browse files
jpenillaCitymonstret
authored andcommitted
fix: deep nesting on permissions causing stack overflow errors
1 parent ffe284e commit abfb630

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ public final class AndPermission implements CommandPermission {
5050
* @return Constructed permission
5151
*/
5252
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
53-
return new AndPermission(new HashSet<>(permissions));
53+
final Set<CommandPermission> objects = new HashSet<>();
54+
for (final CommandPermission permission : permissions) {
55+
if (permission instanceof AndPermission) {
56+
objects.addAll(permission.getPermissions());
57+
} else {
58+
objects.add(permission);
59+
}
60+
}
61+
return new AndPermission(objects);
5462
}
5563

5664
@Override

cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface CommandPermission {
6464
final Set<CommandPermission> permission = new HashSet<>(2);
6565
permission.add(this);
6666
permission.add(other);
67-
return new OrPermission(permission);
67+
return OrPermission.of(permission);
6868
}
6969

7070
/**
@@ -79,7 +79,7 @@ public interface CommandPermission {
7979
final Set<CommandPermission> permission = new HashSet<>(other.length + 1);
8080
permission.add(this);
8181
permission.addAll(Arrays.asList(other));
82-
return new OrPermission(permission);
82+
return OrPermission.of(permission);
8383
}
8484

8585
/**
@@ -94,7 +94,7 @@ public interface CommandPermission {
9494
final Set<CommandPermission> permission = new HashSet<>(2);
9595
permission.add(this);
9696
permission.add(other);
97-
return new AndPermission(permission);
97+
return AndPermission.of(permission);
9898
}
9999

100100
/**
@@ -109,7 +109,7 @@ public interface CommandPermission {
109109
final Set<CommandPermission> permission = new HashSet<>(other.length + 1);
110110
permission.add(this);
111111
permission.addAll(Arrays.asList(other));
112-
return new AndPermission(permission);
112+
return AndPermission.of(permission);
113113
}
114114

115115
}

cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ public final class OrPermission implements CommandPermission {
5050
* @return Constructed permission
5151
*/
5252
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
53-
return new OrPermission(new HashSet<>(permissions));
53+
final Set<CommandPermission> objects = new HashSet<>();
54+
for (final CommandPermission permission : permissions) {
55+
if (permission instanceof OrPermission) {
56+
objects.addAll(permission.getPermissions());
57+
} else {
58+
objects.add(permission);
59+
}
60+
}
61+
return new OrPermission(objects);
5462
}
5563

5664
@Override

0 commit comments

Comments
 (0)