Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1 #include <stdio.

h>
2 //This program creates a board to play a CarGame. When properly executed the a board
composed of 0's and 1's will be printed.
3 int main(int argc, char **argv){
4
5 int argv1= atoi(argv[1]);
6 int argv2= atoi(argv[2]);
7
8 if ( argv1 < 50 && argv2 < 50) {
9 printf("CarGame board initialized \n");
10 }else{
11 printf("Wrong sizes....initialization cannot proceed \n");
12 return 0;
13 }
14
15 int *boardWhite[argv1];
16 int *boardBlack[argv2];
17
18
19
20 int i=0;
21 int j=0;
22 for(i=0;i<argv2;i++){
23 //fulfilled with 1=white
24 boardWhite[i]=1;
25 }
26 for(i=0;i<argv1;i++){
27 //fulfilled with 0=black
28 boardBlack[i]=0;
29 }
30
31
32 if(argv1>argv2){
33 for(i=0;i<argv1;i++){
34 printf("%d ",boardWhite[i]);
35 }
36 printf("\n");
37 }else if(argv1<argv2){
38 for(i=0;i<argv1;i++){
39 printf("%d ",boardBlack[i]);
40 }
41 printf("\n");
42 }else{
43 for(i=0;i<argv1;i++){
44 for(j=0;j<argv2;j++){
45 printf("%d ",boardBlack[j]);
46 printf("%d ",boardWhite[j]);
47 }
48 printf("\n");
49 }
50 }
51
52 printf("CarGame board constructed \n");
53
54 }
55
56 //When does the following code works? when argv1=argv2 this code works properly but
when argv1>argv2 also works but not properly because when executed printf("%d
",boardWhite[i]); you will realize that not all positions are initialized due to the
"for" applied before.
57 //What is the result of executing the given code when it runs without errors? a
board composed of 0s and 1s will be displayed whose size is argv1 x argv2 being
argv1 = argv2
58 //What kind of vulnerabilities can be identified in the following code? Wrong input
validation because negative values or too small values of arguments are not
evaluated. Moreover, segmentation fault can also occur because boardWhite's size is
argv1 and boardBlack's size is argv2, and then, each buffer is fulfilled with 0's
and 1's according to the opposite variable (for(i=0;i<argv2;i++) for boardWhite; and
for(i=0;i<argv1;i++) for boardBlack).
59

You might also like