To detect Buffer Overflow > Java Program

To detect Buffer Overflow > Java Program

Cryptography and System Security


Program:

import java.util.Scanner;

public class Overflow {

    public static void main(String[] args) {


        int buff, test, count;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Buffer Size:");
        buff = sc.nextInt();
        int[] tests = new int[buff];
        System.out.println("Enter Number of Elements:");
        count = sc.nextInt();
        System.out.println("Enter Numbers:");
        try {
            for (int i = 0; i < count; i++) {
                test = sc.nextInt();
                tests[i] = test;
            }

            for (int i = 0; i < count; i++) {
                System.out.println(tests[i]);
            }
        } catch (ArrayIndexOutOfBoundsException ae) {
            System.out.println("Buffer Overflow");

        }

    }
}

Output:
run:
Enter Buffer Size:
5
Enter Number of Elements:
6
Enter Numbers:
23 32 32 32 32 23
Buffer Overflow
BUILD SUCCESSFUL (total time: 16 seconds)

Comments

Popular posts from this blog

Intermediate Code Generation > C Program