Skip to content

Fix backwards-compatibility issue introduced with PR#59/#60 #67

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

Merged
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
16 changes: 11 additions & 5 deletions src/java/ognl/ASTStaticField.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,21 @@ public boolean isNodeConstant(OgnlContext context)
result = true;
} else
{
Field f = OgnlRuntime.getField(c, fieldName);
if (f == null) {
throw new NoSuchFieldException(fieldName);
Field f;
try {
f = c.getField(fieldName); // Public fields checked first (direct)
} catch (NoSuchFieldException nsfe) {
f = OgnlRuntime.getField(c, fieldName); // Non-public fields checked (access controlled)
if (f == null) {
throw new NoSuchFieldException(fieldName);
}
}
final int fModifiers = f.getModifiers();

if (!Modifier.isStatic(f.getModifiers()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @aleksandr-m .
It was just a minor optimization (equivalent to one already used in OgnlRuntime) to replace two calls to f.getModifiers():
.. if (!Modifier.isStatic(f.getModifiers())) / result = Modifier.isFinal(f.getModifiers());
with a single call/assignment for the field modifiers fModifiers = f.getModifiers():
.. if (!Modifier.isStatic(fModifiers)) / result = Modifier.isFinal(fModifiers).

Copy link
Contributor

@aleksandr-m aleksandr-m Jan 24, 2019

Choose a reason for hiding this comment

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

getModifiers() is just a getter it is ok to use it directly. If you want to keep fModifiers then declaration and assignment can be moved to the same line, currently it looks very odd.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @aleksandr-m .
Thanks for the feed-back and suggestion.
The declaration/assignment for fModifiers has been moved to the same line in both modules (and the declaration of one variable moved closer to its usage point). Hopefully it looks a little cleaner now ?

if (!Modifier.isStatic(fModifiers))
throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");

result = Modifier.isFinal(f.getModifiers());
result = Modifier.isFinal(fModifiers);
}
} catch (ClassNotFoundException e) {
reason = e;
Expand Down
36 changes: 23 additions & 13 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2099,24 +2099,34 @@ public static Object getStaticField(OgnlContext context, String className, Strin
}
}

final Field f = getField(c, fieldName);
if (f == null) {
throw new NoSuchFieldException(fieldName);
Field f;
try {
f = c.getField(fieldName); // Public fields checked first (direct)
} catch (NoSuchFieldException nsfe) {
f = OgnlRuntime.getField(c, fieldName); // Non-public fields checked (access controlled)
if (f == null) {
throw new NoSuchFieldException(fieldName);
}
}
if (!Modifier.isStatic(f.getModifiers())) {
final int fModifiers = f.getModifiers();
if (!Modifier.isStatic(fModifiers)) {
throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");
}
final Object result;

Object result = null;
if (context.getMemberAccess().isAccessible(context, null, f, null)) {
final Object state = context.getMemberAccess().setup(context, null, f, null);
try {
result = f.get(null);
} finally {
context.getMemberAccess().restore(context, null, f, null, state);
}
if (Modifier.isPublic(fModifiers)) {
result = f.get(null); // Valid static public field (no access check required)
} else {
throw new IllegalAccessException("Access to " + fieldName + " of class " + className + " is forbidden");
if (context.getMemberAccess().isAccessible(context, null, f, null)) {
final Object state = context.getMemberAccess().setup(context, null, f, null);
try {
result = f.get(null);
} finally {
context.getMemberAccess().restore(context, null, f, null, state);
}
} else {
throw new IllegalAccessException("Access to " + fieldName + " of class " + className + " is forbidden");
}
}

return result;
Expand Down