Question: Your task The sanitizers / directory contains several buggy programs. For each of the programs below, your task is to: Compile the program as instructed

Your task
The sanitizers/ directory contains several buggy programs. For each of the programs below, your task is to:
Compile the program as instructed
Run the program until you get an error message from AddressSanitizer or MemorySanitizer
With help from the Sanitisers Guide, identify the following pieces of information from the error message:
The type of error, and what it means
The name of the function in which the error occurred, and the source file and line number of the instruction that triggered the error
The series of functions that were on the function call stack when the error occurred
The meaning of any other stack traces in the error message (these usually have a line of pink text above them)
Deduce what is wrong with the program (but don't actually fix the program)
In the file part-1-answers.txt, under the heading for the program, paste the error message given by the program and briefly explain what the problem with the program is. During lab marking, the notes you write in this file will be helpful.
Do not fix the programs - the purpose of this task is to learn how to interpret error messages. During lab marking, your tutor will ask you to reproduce the error messages, which won't be possible if you have fixed the programs. Program 2: shuffleArray . c
The purpose of this program is to read values into an array and then shuffle the array. Here is the intended
behaviour of the program:
$./shuffleArray
Enter array size: 5
Enter array values: 31415
Original array: 3,1,4,1,5
Shuffled array: 1,3,1,5,4
Note that the program randomly shuffles the array, so it is expected to produce different outputs each time it is run.
Compile the program with the following command:
$ clang -Wall -Werror -g -fsanitize=address, leak, undefined -o shuffleArray shuffleArray.c
Hint: Note that unlike Program 1, the error in this program will occur in a function that is not in the source code
(as there is no function called scanf_common in shuffleArray , c). This suggests that the error occurred in
a library function. In this case, you should look further down the function call stack until you find a function that
exists in the source code. // Reads in values, then outputs them in random order
#include
#include
#include
void shuffle(int *arr, int size);
void printArray(int *arr, int size);
int main(void){
srand(time(NULL));
printf("Enter array size: ");
int maxVals =0;
if (scanf("%d", &maxVals)!=1|| maxVals 0){
fprintf(stderr, "error: invalid array size
");
}
int *vals = malloc(maxVals);
if (vals == NULL){
fprintf(stderr, "error: out of memory
");
exit(EXIT_FAILURE);
}
printf("Enter array values: ");
int numVals =0;
while (numVals maxVals && scanf("%d", &vals[numVals])==1){
numVals++;
}
printf("Original array: ");
printArray(vals, numVals);
shuffle(vals, numVals);
printf("Shuffled array: ");
printArray(vals, numVals);
free(vals);
}
// Randomly shuffles the given array
void shuffle(int *arr, int size){
for (int i =1; i size; i++){
int j = rand()%(i +1);
int tmp = arr[j];
arr[j]= arr[i];
arr[i]= tmp;
}
}
// Prints the given array
void printArray(int *arr, int size){
printf("[");
for (int i =0; i size; i++){
if (i >0){
printf(",");
}
printf("%d", arr[i]);
}
printf("]
");
}
 Your task The sanitizers/ directory contains several buggy programs. For each

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!