Skip to content
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

Update doPrivilegedWithCombinerHelper function #20430

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,37 +318,49 @@ public AccessControlContext(ProtectionDomain[] fromContext) {
}

AccessControlContext(ProtectionDomain[] context, int authorizeState) {
super();
switch (authorizeState) {
default:
// authorizeState can't be STATE_UNKNOWN, callerPD always is NULL
throw new IllegalArgumentException();
case STATE_AUTHORIZED:
case STATE_NOT_AUTHORIZED:
break;
}
this.context = context;
this.authorizeState = authorizeState;
this.containPrivilegedContext = true;
this(context, null, null, authorizeState);
}

AccessControlContext(AccessControlContext acc, ProtectionDomain[] context, int authorizeState) {
this(context, null, acc, authorizeState);
}

AccessControlContext(ProtectionDomain[] context, AccessControlContext parentAcc, AccessControlContext acc, int authorizeState) {
super();
switch (authorizeState) {
default:
// authorizeState can't be STATE_UNKNOWN, callerPD always is NULL
// authorizeState can't be STATE_UNKNOWN, callerPD is always NULL
throw new IllegalArgumentException();
case STATE_AUTHORIZED:
if (null != acc) {
// inherit the domain combiner when authorized
this.domainCombiner = acc.domainCombiner;
if (acc != null) {
if (parentAcc == null) {
// inherit the domain combiner when authorized
this.domainCombiner = acc.domainCombiner;
this.context = context;
} else {
// when parent combiner is not null, use parent combiner to combine the current context
DomainCombiner parentCombiner = parentAcc.getCombiner();
if (parentCombiner != null) {
this.context = parentCombiner.combine(context, acc.context);
this.domainCombiner = parentCombiner;
} else {
this.context = combinePDObjs(context, acc.context);
this.domainCombiner = acc.domainCombiner;
}
}
} else {
if (parentAcc != null) {
this.domainCombiner = parentAcc.domainCombiner;
this.nextStackAcc = parentAcc;
}
this.context = context;
}
break;
case STATE_NOT_AUTHORIZED:
this.context = context;
break;
}
this.doPrivilegedAcc = acc;
this.context = context;
this.authorizeState = authorizeState;
this.containPrivilegedContext = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can AccessControlContext(AccessControlContext acc, ProtectionDomain[] context, int authorizeState) invoke this constructor to avoid code duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be in a new constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jason makes a good suggstion: the two constructors above can use the new one:

AccessControlContext(ProtectionDomain[] context, int authorizeState) {
	this(context, null, null, authorizeState);
}

AccessControlContext(AccessControlContext acc, ProtectionDomain[] context, int authorizeState) {
	this(context, null, acc, authorizeState);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,21 +1088,17 @@ public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action
/**
* Helper method to construct an AccessControlContext for doPrivilegedWithCombiner methods.
*
* @param context an AccessControlContext, if it is null, use getContextHelper() to construct a context.
* @param acc an AccessControlContext, if it is null, use getContextHelper() to construct a context.
*
* @return An AccessControlContext to be applied to the doPrivileged(action, context, perms).
*/
@CallerSensitive
private static AccessControlContext doPrivilegedWithCombinerHelper(AccessControlContext context) {
private static AccessControlContext doPrivilegedWithCombinerHelper(AccessControlContext acc) {
ProtectionDomain domain = getCallerPD(2);
ProtectionDomain[] pdArray = (domain == null) ? null : new ProtectionDomain[] { domain };
AccessControlContext fixedContext = new AccessControlContext(context, pdArray, getNewAuthorizedState(context, domain));
if (context == null) {
AccessControlContext parentContext = getContextHelper(true);
fixedContext.domainCombiner = parentContext.domainCombiner;
fixedContext.nextStackAcc = parentContext;
}
return fixedContext;
ProtectionDomain[] context = (domain == null) ? null : new ProtectionDomain[] { domain };
AccessControlContext parentAcc = getContextHelper(acc == null);

return new AccessControlContext(context, parentAcc, acc, getNewAuthorizedState(acc, domain));
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

Expand Down