undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Basic pattern bindings scope test
|
||||
* @compile/fail/ref=DuplicateBindingTest.out -XDrawDiagnostics DuplicateBindingTest.java
|
||||
*/
|
||||
|
||||
public class DuplicateBindingTest {
|
||||
|
||||
int f;
|
||||
|
||||
public static boolean main(String[] args) {
|
||||
Object o1 = "";
|
||||
Object o2 = "";
|
||||
|
||||
if (args != null) {
|
||||
int s;
|
||||
if (o1 instanceof String s) { // NOT OK. Redef same scope.
|
||||
}
|
||||
if (o1 instanceof String f) { // OK to redef field.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (o1 instanceof String s && o2 instanceof String s) {} //error - already in scope on RHS (in scope due to LHS when true)
|
||||
if (o1 instanceof String s && !(o2 instanceof String s)) {} //error - already in scope on RHS (in scope due to LHS when true)
|
||||
if (!(o1 instanceof String s) && !(o2 instanceof String s)) {} //error - the same binding variable on LHS and RHS when false
|
||||
if (!(o1 instanceof String s) && (o2 instanceof String s)) {} //ok
|
||||
|
||||
if (!(o1 instanceof String s) || o2 instanceof String s) {} //error - already in scope on RHS (in scope due to LHS when false)
|
||||
if (!(o1 instanceof String s) || !(o2 instanceof String s)) {} //error - already in scope on RHS (in scope due to LHS when false)
|
||||
if (o1 instanceof String s || o2 instanceof String s) {} //error - the same binding variable on LHS and RHS when true
|
||||
if (o1 instanceof String s || !(o2 instanceof String s)) {} //ok
|
||||
|
||||
if (o1 instanceof String s ? o2 instanceof String s : true) {} //error - already in scope in the true section (due to condition when true)
|
||||
if (o1 instanceof String s ? true : o2 instanceof String s) {} //error - the same binding variable in condition when true and false section when true
|
||||
if (!(o1 instanceof String s) ? o2 instanceof String s : true) {} //error - the same binding variable in condition when false and true section when true
|
||||
if (args.length == 1 ? o2 instanceof String s : o2 instanceof String s) {} //error - the same binding variable in true section when true and false section when true
|
||||
if (o1 instanceof String s ? true : !(o2 instanceof String s)) {} //error - the same binding variable in condition when true and false section when false
|
||||
if (!(o1 instanceof String s) ? !(o2 instanceof String s) : true) {} //error - the same binding variable in condition when false and true section when false
|
||||
if (args.length == 1 ? !(o2 instanceof String s) : !(o2 instanceof String s)) {} //error - the same binding variable in true section when false and false section when false
|
||||
|
||||
//verify that errors are reported to clashes in expression in non-conditional statements:
|
||||
boolean b = o1 instanceof String s && o2 instanceof String s;
|
||||
b = o1 instanceof String s && o2 instanceof String s;
|
||||
b &= o1 instanceof String s && o2 instanceof String s;
|
||||
assert o1 instanceof String s && o2 instanceof String s;
|
||||
testMethod(o1 instanceof String s && o2 instanceof String s, false);
|
||||
b = switch (args.length) { default -> o1 instanceof String s && o2 instanceof String s; };
|
||||
b = switch (args.length) { default -> { yield o1 instanceof String s && o2 instanceof String s; } };
|
||||
if (true) return o1 instanceof String s && o2 instanceof String s;
|
||||
|
||||
//verify the bindings don't go through method invocation parameters or outside a lambda:
|
||||
b = ((VoidPredicate) () -> o1 instanceof String s).get() && s.isEmpty();
|
||||
testMethod(o1 instanceof String s, s.isEmpty());
|
||||
}
|
||||
|
||||
static void testMethod(boolean b1, boolean b2) {}
|
||||
interface VoidPredicate {
|
||||
public boolean get();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue