import java.io.FileInputStream; class A{} class TestSuite{ public static void main(String[] args){ div0_1(); div0_2(); div0_3(); div0_4(); div0_5(0); nullPtr1(); nullPtr2(); nullPtr3(); nullPtr4(null); outOfBounds1(); outOfBounds2(); outOfBounds3(); outOfBounds4(); int[] array = {}; outOfBounds5(array); infiniteLoop1(); infiniteLoop2(); infiniteLoop3(); infiniteLoop4(); infiniteRecursion1(); infiniteRecursion2(2); infiniteRecursion3(); typeOverFlow1(); typeOverFlow2(); InvalidCast1(); BadStringCmp1(); BadStringCmp2(); BadStreamClose1(); BadStreamClose2(); DeadLock1.startThread(); DeadLock2.startThread(); } /*********** ****BUGS**** ************/ private static void div0_1(){ int x = 0; int y = 3/x; } private static void div0_2(){ int x = 1; int y = x - x; int z = 3/y; } private static void div0_3(){ String[] array = {}; int x = 2/array.length; } /** * JLint does not discover it! */ private static void div0_4(){ String s = ""; int x = 2/s.length(); } /** * JLint does not discover it! */ private static void div0_5(int x){ int y = 2/x; } /** * JLint does not discover it! */ private static void nullPtr1(){ String[] s = null; System.out.println(s.length); } private static void nullPtr2(){ String s = null; System.out.println(s.length()); } /** * JLint does not discover it! */ private static void nullPtr3(){ String[] strs = {"test", null}; System.out.println(strs[1].length()); } private static void nullPtr4(String s){ System.out.println(s.length()); } private static void outOfBounds1(){ int[] array = {}; int y = array[0]; } /** * JLint does not discover it! */ private static void outOfBounds2(){ int[] array = {1,2}; int[] ids = {-1}; int y = array[ids[0]]; } private static void outOfBounds3(){ int[] array = {1,2}; int x = 0; int y = array[x - 1]; } private static void outOfBounds4(){ int[] array = {1,2}; int y = array[array.length]; } private static void outOfBounds5(int[] array){ int y = array[array.length-1]; } /** * JLint does not discover it! */ private static void infiniteLoop1(){ while(true){} } /** * JLint does not discover it! */ private static void infiniteLoop2(){ for(int i = 0 ; i < 1 ; i--){} } /** * JLint does not discover it! */ private static void infiniteLoop3(){ int x = 1; for(int i = 0 ; i < x ; i++){ x++; } } private static void infiniteLoop4(){ int x = 2; for(int i = 0 ; i < x ; ){} } /** * JLint does not discover it! */ private static void infiniteRecursion1(){ infiniteRecursion1(); } /** * JLint does not discover it! */ private static void infiniteRecursion2(int x){ if(x > 0){ infiniteRecursion2(x + 1); } else{ System.out.println(x); } } private static void infiniteRecursion3(){ infiniteRecursion3b(); } private static void infiniteRecursion3b(){ infiniteRecursion3(); } /** * JLint does not discover it! */ private static void typeOverFlow1(){ int x = Integer.MAX_VALUE; x++; } /** * JLint does not discover it! */ private static void typeOverFlow2(){ int x = Integer.MAX_VALUE; int y = x + 1; } /** * JLint does not discover it! */ private static void InvalidCast1(){ Object o = new Object(); String s = (String)o; } /** * JLint does not discover it! */ private static void BadStringCmp1(){ int x = 0; if("test1" == "test2"){ x = 1; } else{ x = 2; } } /** * JLint does not discover it! */ private static void BadStringCmp2(){ int x = 0; String s1 = new String("test1"); if(s1 == "test2"){ x = 1; } else{ x = 2; } } /** * JLint does not discover it! */ private static void BadStreamClose1(){ try { FileInputStream x = new FileInputStream("z"); x.close(); } catch(Exception e){} } /** * JLint does not discover it! */ private static void BadStreamClose2(){ FileInputStream x; try{ x = new FileInputStream("z"); }catch(Exception e){} if(2 > 3){ try{ x.close(); }catch(Exception e){} } } /** * JLint does not discover it! */ private static void arrayStoreException1(){ Object x[] = new String[3]; x[0] = new Integer(0); } /******************* ****BAD PRACTICE**** ********************/ } class DeadLock1{ private static Object lock = new Object(); private static Thread th1 = new Thread(){public void run(){f1(1);}}; private static Thread th2 = new Thread(){public void run(){f2(2);}}; public static void startThread(){ th1.start(); th2.start(); } private static void f1(int num){ synchronized (lock) { try {Thread.yield();} catch (Exception e) {} f2(num); } } private static synchronized void f2(int num){ synchronized (lock) {f1(num);} } } class DeadLock2{ private static Object lock1 = new Object(); private static Object lock2 = new Object(); private static Thread th1 = new Thread(){public void run(){f1();}}; private static Thread th2 = new Thread(){public void run(){f2();}}; public static void startThread(){ th1.start(); th2.start(); } private static void f1(){ synchronized (lock1) { try {Thread.yield();} catch (Exception e) {} synchronized (lock2) {f2();} } } private static synchronized void f2(){ synchronized (lock2) { synchronized (lock1) {f1();} } } }