Skip to content

Commit 54fa946

Browse files
committed
Fix stack indexing bugs
1 parent 89c2797 commit 54fa946

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

CHANGELOG.md

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
1111
### Added
1212
- New detector `SharedVariableAtomicityDetector` for new bug types `AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE`, `AT_NONATOMIC_64BIT_PRIMITIVE` and `AT_STALE_THREAD_WRITE_OF_PRIMITIVE` (See SEI CERT rules [VNA00-J](https://wiki.sei.cmu.edu/confluence/display/java/VNA00-J.+Ensure+visibility+when+accessing+shared+primitive+variables), [VNA02-J](https://wiki.sei.cmu.edu/confluence/display/java/VNA02-J.+Ensure+that+compound+operations+on+shared+variables+are+atomic) and [VNA05-J](https://wiki.sei.cmu.edu/confluence/display/java/VNA05-J.+Ensure+atomicity+when+reading+and+writing+64-bit+values)).
1313
- New detector `FindHiddenMethod` for bug type `HSM_HIDING_METHOD`. This bug is reported whenever a subclass method hides the static method of super class. (See [SEI CERT MET07-J](https://wiki.sei.cmu.edu/confluence/display/java/MET07-J.+Never+declare+a+class+method+that+hides+a+method+declared+in+a+superclass+or+superinterface)).
14-
- New detector `ResourceInMultipleThreadsDetector` and introduced new bug type:
15-
- `AT_UNSAFE_RESOURCE_ACCESS_IN_THREAD` is reported in case of unsafe resource access in multiple threads.
1614
- New detector `AtomicOperationsCombinedDetector` and introduced new bug types:
1715
- `AT_COMBINED_ATOMIC_OPERATIONS_ARE_NOT_ATOMIC` is reported in case of combined atomic operations are not synchronized.
1816
- `AT_ATOMIC_OPERATION_NEEDS_SYNCHRONIZATION` is reported when an atomic operation is not synchronized, but should be because of thread safety.

spotbugs/src/main/java/edu/umd/cs/findbugs/detect/AtomicOperationsCombinedDetector.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ private void collectFieldsForAtomicityCheck(ClassContext classContext, Method me
143143

144144
if (instruction instanceof PUTFIELD) {
145145
OpcodeStack stack = OpcodeStackScanner.getStackAt(classContext.getJavaClass(), method, handle.getPosition());
146-
OpcodeStack.Item stackItem = stack.getStackItem(0);
147-
if (isAtomicField(stackItem.getReturnValueOf())) {
148-
fieldsForAtomicityCheck.add(XFactory.createXField((FieldInstruction) instruction, cpg));
146+
if (stack.getStackDepth() > 0) {
147+
OpcodeStack.Item stackItem = stack.getStackItem(0);
148+
if (isAtomicField(stackItem.getReturnValueOf())) {
149+
fieldsForAtomicityCheck.add(XFactory.createXField((FieldInstruction) instruction, cpg));
150+
}
149151
}
150152
}
151153
}
@@ -223,7 +225,7 @@ private void analyzeFieldsForAtomicityViolations(ClassContext classContext, Meth
223225
}
224226

225227
private Optional<XField> findFieldRequiringAtomicityCheck(OpcodeStack stack) {
226-
if (stack.getStackDepth() > 1 && stack.getStackItem(0).getReturnValueOf() != null) {
228+
if (stack.getStackDepth() > 0 && stack.getStackItem(0).getReturnValueOf() != null) {
227229
return Optional.empty();
228230
}
229231
return IntStream.range(0, stack.getStackDepth())

0 commit comments

Comments
 (0)