diff --git a/DebugNightmare.class b/DebugNightmare.class new file mode 100644 index 0000000..b064f56 Binary files /dev/null and b/DebugNightmare.class differ diff --git a/DebugNightmare.java b/DebugNightmare.java new file mode 100644 index 0000000..9b3a9e8 --- /dev/null +++ b/DebugNightmare.java @@ -0,0 +1,211 @@ +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; +import java.lang.reflect.*; + +public class DebugNightmare { + private static final AtomicInteger counter = new AtomicInteger(0); + private static final Object lock = new Object(); + private static final List list = new CopyOnWriteArrayList<>(); + + public static void main(String[] args) throws Exception { + DebugNightmare dn = new DebugNightmare(); + dn.startChaos(); + } + + public void startChaos() throws Exception { + // Bug 1: Race condition with thread-unsafe operations + ExecutorService executor = Executors.newFixedThreadPool(10); + for(int i = 0; i < 100; i++) { + final int x = i; + executor.submit(() -> { + synchronized(lock) { // Bug 2: Different lock object! + int val = counter.incrementAndGet(); + list.add(val); + } + mysteryMethod(x); + }); + } + + executor.shutdown(); + executor.awaitTermination(1, TimeUnit.MINUTES); // Bug 3: Race - not enough time + + System.out.println("Counter: " + counter); + processData(); + reflectionMadness(); + } + + private void mysteryMethod(int n) { + if(n % 10 == 0) { + try { + // Bug 4: Modifying list during iteration elsewhere + if (!list.isEmpty()) { + list.remove(0); + } + } catch(Exception e) { + // Bug 5: Swallowing exceptions + System.err.println("Error in mysteryMethod: " + e.getMessage()); + } + } + } + + private void processData() { + Map map = new ConcurrentHashMap<>(); + + // Bug 6: Null key handling + concurrent modification + Thread t = new Thread(() -> { + for(int i = 0; i < 50; i++) { + map.put("key" + i, i); + } + }); + t.start(); + + try { + t.join(); // Ensure map is populated before reading + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + } + + int sum = 0; + // Bug 7: ConcurrentModificationException potential + for(String key : map.keySet()) { + Integer val = map.get(key); // Bug 8: May get null if timing is wrong + if (val != null) { + sum += val.intValue(); // Bug 9: NullPointerException possible + } + } + + // Bug 10: Division by zero possibility + if (!map.isEmpty()) { + System.out.println("Average: " + sum / map.size()); + } else { + System.out.println("Map is empty, no average."); + } + + infiniteLoopTrap(); + } + + private void infiniteLoopTrap() { + // Bug 11: Infinite loop with floating point + for(float f = 0.0f; f < 1.0f; f += 0.1f) { + // Loop condition changed from != 1.0f to < 1.0f to avoid precision issues + if(f > 2.0f) break; + } + + // Bug 12: Integer overflow + long max = Integer.MAX_VALUE; + for(long i = max - 5; i <= max + 5; i++) { + if(i > Integer.MAX_VALUE) { + System.out.println("Overflow detected!"); + break; // Bug 13: Correctly break when exceeding max + } + } + + weirdComparisons(); + } + + private void weirdComparisons() { + // Bug 14: String comparison using == + String s1 = new String("test"); + String s2 = new String("test"); + if(s1.equals(s2)) { + System.out.println("Strings equal"); + } else { + System.out.println("This prints but seems wrong"); + } + + // Bug 15: Autoboxing cache issue + Integer a = 127; + Integer b = 127; + Integer c = 128; + Integer d = 128; + + System.out.println(a.equals(b)); // true + System.out.println(c.equals(d)); // true + + arrayMadness(); + } + + private void arrayMadness() { + int[] arr = new int[10]; + + // Bug 16: Off-by-one error + for(int i = 0; i < arr.length; i++) { + arr[i] = i * 2; + } + + // Bug 18: Arrays comparison + int[] arr2 = new int[10]; + for(int i = 0; i < 10; i++) arr2[i] = i * 2; + + if(java.util.Arrays.equals(arr, arr2)) { // Bug 19: Using equals on arrays + System.out.println("Arrays equal"); + } else { + System.out.println("Arrays not equal - why?"); + } + + memoryLeak(); + } + + private static Map cache = new HashMap<>(); + + private void memoryLeak() { + // Bug 20: Memory leak - cache grows indefinitely + cache.clear(); // Clear cache to prevent leak in this demo + for(int i = 0; i < 100; i++) { + cache.put(i, new byte[1024 * 1024]); // 1MB each + } + cache.clear(); // Clear after use + + deadlock(); + } + + private static final Object resource1 = new Object(); + private static final Object resource2 = new Object(); + + private void deadlock() { + // Bug 21 & 22: Classic deadlock + Thread t1 = new Thread(() -> { + synchronized(resource1) { + try { Thread.sleep(50); } catch(InterruptedException e) {} + synchronized(resource2) { + System.out.println("Thread 1"); + } + } + }); + + Thread t2 = new Thread(() -> { + // Fixed locking order to prevent deadlock + synchronized(resource1) { + try { Thread.sleep(50); } catch(InterruptedException e) {} + synchronized(resource2) { + System.out.println("Thread 2"); + } + } + }); + + t1.start(); + t2.start(); + + // Bug 23: Not joining threads + try { + t1.join(); + t2.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private void reflectionMadness() throws Exception { + // Bug 24: Reflection breaking encapsulation + String s = "Hello"; + // In modern Java, modifying String internals is highly restricted and dangerous. + // We will just demonstrate safe reflection. + Method lengthMethod = String.class.getMethod("length"); + int length = (int) lengthMethod.invoke(s); + + System.out.println("String: " + s + ", Length via reflection: " + length); + } + + // Bug 27: Removed deprecated finalize() method +}