P
Lab Assessment Report
B. Tech Computing Science Engineering
Specialisation in Cyber Security And Digital Forensics
CSD4001 - Software Vulnerability Testing
WINTER SEMESTER (2023-2024)
Submitted by,
Student Name: Tisha Mahato
Registration No.: 21BCY10183
Slot: A11+A12+A13
Class No.: BL2023240500528
Course faculty:
Dr. Sahana Gajala Qureshi
Associate Professor
Division of Cyber Security and Digital Forensics
School of Computing Science and Engineering
VIT Bhopal University
Madhya Pradesh -466114ra
INDEX
EXP NO. NAME OF EXPERIMENTS SIGNATURE
1. BUFFER OVERFLOW
2. ARRAY LIST
3. ARRAY TESTS
4. VARIABLES
LAB: 1
1) What will the output of the following code (Run the code in java and paste
the screenshot of the output with proper explanation)?
OUTPUT:
After the execution of the provided code, a ArrayIndexOutOfBoundsException
will be thrown, and the program will terminate abnormally (crash). This
exception occurs because the code attempts to write to an index outside the
bounds of the buffer array.
Here's what happens step by step:
1. The variable importantData is initialized with the value 1.
2. An integer array buffer of size 10 is created.
3. The for loop starts iterating from 0 to 14 (inclusive).
4. For the first 10 iterations (from 0 to 9), the value 7 is assigned to the
corresponding indices of the buffer array (buffer[0] to buffer[9]). This is valid
because the array size is 10.
5. At the 11th iteration (i = 10), the code tries to assign buffer[10] = 7, but this
index is out of bounds for the buffer array, which has a valid index range from
0 to 9.
[Link] Java Virtual Machine (JVM) detects this violation of array bounds and
throws an ArrayIndexOutOfBoundsException.
7. The program terminates without executing the println statements.
To fix this buffer overflow issue, you need to ensure that the loop condition
does not attempt to access indices beyond the valid range of the array. You can
modify the for loop condition to use the [Link] instead of a hard-coded
value:
With this change, the loop will iterate from 0 to 9 (inclusive), which are valid
indices for the buffer array of size 10. After this modification, the code will
execute without any exceptions, and the output will be:
LAB: 2
2. Run the following code and answer the following questions:
OUTPUT:
Regarding what will happen after the execution of the provided codeThe code
will prompt the user to enter the number of integers they want to input. Let's
assume the user enters a value less than or equal to 10 (the size of the tests
array). In this case, the code will then prompt the user to enter that many
numbers, one by one. Each number entered by the user will be stored in the
tests array at the corresponding index.
However, if the user enters a value greater than 10 for the number of integers,
the code will print the error message "Error: Maximum number of values
allowed is 10" and then exit without prompting the user to enter any numbers.
This is because the tests array has a fixed size of 10, and attempting to store
more than 10 values in it would result in an ArrayIndexOutOfBoundsException.
The reason why the code behaves this way is because of the way arrays work
in Java. Arrays have a fixed size, determined at the time of their creation. Once
an array is created with a specific size, it cannot be resized dynamically.
Attempting to access an index outside the valid range of the array (either
negative or greater than or equal to the array length) will result in an
ArrayIndexOutOfBoundsException.
In this code, the tests array is created with a size of 10. So, if the user tries to
store more than 10 values in it, the code would attempt to access an index
beyond the valid range, causing the exception.
To fix this issue, you have two options:
Use an ArrayList instead of an array: Instead of using a fixed-size array, you can
use an ArrayList, which is a dynamically resizable data structure in Java. With
an ArrayList, you can add elements without worrying about the size limit.
Here's how you can modify the code:
Validate user input: If you want to stick with using an array, you can add a
check to ensure that the user does not enter a value greater than the array
size. Here's how you can modify the code:
In this modified version, an if statement checks if the user-entered count value
is greater than the size of the tests array ([Link]). If it is, an error message
is printed. Otherwise, the loop proceeds to store the user-entered values in the
tests array, up to the valid array size.
Both solutions address the potential buffer overflow issue by either using a
dynamically resizable data structure or validating the user input against the
fixed array size.
LAB: 3
Find out the given program and discuss the answer.
Explaination:
The given program is a Java program that takes input from the user and stores
it in an array. Here's a detailed explanation of the program:
1. The program starts by importing the Scanner class from the [Link]
package, which is used to take input from the user.
2. The Overflow2 class is defined, and the main method is the entry point
of the program.
3. An array tests of size 10 is declared to store the user-provided numbers.
4. The variables test and count are declared, where count is initialized to -
1.
5. A Scanner object scan is created to read input from the user.
6. The program enters a while loop that continues until count is between 0
and 10 (inclusive).
7. Inside the loop, the program prompts the user to enter the number of
values they want to input, and the user's input is stored in the count
variable.
8. Once the valid number of values is entered, the program enters another
for loop that iterates count times.
9. Inside this loop, the program prompts the user to enter a number, which
is stored in the test variable.
10. The value of test is then stored in the tests array at the index i.
11. The purpose of this program is to allow the user to input a set of
numbers, with the restriction that the number of values must be
between 0 and 10 (inclusive). The program then stores the input values
in the tests array.
The program does not perform any further processing or output the stored
values. It simply takes the user input and stores it in the array.
OUTPUT:
The program does not have any explicit output. It is designed to take user
input and store it in an array. The output will depend on the user's input.
In this example, the user entered 5 as the number of values to input, and then
entered the numbers 10, 20, 30, 40, and 50. These values would then be
stored in the tests array.
The program does not print or display the stored values. It simply takes the
user input and stores it in the array. If you want to see the stored values, you
would need to add additional code to print or access the tests array after the
input has been stored.
LAB : 4
What will be the output of the following program.
Explaination
The output of the given C++ program will depend on the values entered by the
user for the variables x and y.
Here's the step-by-step execution of the program:
1. The program prompts the user to enter a value for x.
2. The user enters a value for x, which is stored in the variable x.
3. The program prompts the user to enter a value for y.
4. The user enters a value for y, which is stored in the variable y.
5. The program then checks the values of x and y using a series of if-else
statements:
· If both x and y are less than 0, the program outputs "Third Quadrant".
· If both x and y are greater than 0, the program outputs "First Quadrant".
· If x is less than 0 and y is greater than 0, the program outputs "2nd
Quadrant".
· If x is greater than 0 and y is less than 0, the program outputs "4th
Quadrant".
6. The program returns 0 to indicate successful execution.
OUTPUT:
The output of the program will be one of the following, depending on the
values entered for x and y:
· If x < 0 and y < 0, the output will be "Third Quadrant".
· If x > 0 and y > 0, the output will be "First Quadrant".
· If x < 0 and y > 0, the output will be "2nd Quadrant".
· If x > 0 and y < 0, the output will be "4th Quadrant".
For example, if the user enters x = -2 and y = 3, the program will output "2nd
Quadrant".