Question

Question 3. In the following code, answer these questions: Analyze the code and how it works?...

Question 3. In the following code, answer these questions:

  1. Analyze the code and how it works?
  2. How can we know if this code has been overwritten? Justify how?

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

int main(int argc, char **argv)

{

int changed = 0;

char buff[8];

while (changed == 0){

gets(buff);

if (changed !=0){

break;}

else{

    printf("Enter again: ");

    continue;

}

}

     printf("the 'changed' variable is modified\n %d", changed);

}

Homework Answers

Answer #1

a. Analysis of the code:

  • This program is taking parameters as arguments while running the program.
  • The while loop will run for the infinite time as the value of "changed" is never changing.
  • The final print statement will never execute.
  • Please look at the comments of the program for more clarity.

b. We can say the code has been overwritten because of below reasons:

  • Since the value of "changed" is always 0, so there is no need to write
    if (changed !=0) { break; }, it is never going to execute. Always, else statement will get executed.
  • Also, there is a clear case of an infinite loop in the program, the while loop in the program will never get terminated and because of it, the last print statement will also never get executed.
  • So, here we clearly see that it is a case of overwritten code.

Please look at the comments of the program for more clarity.

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv) // Here basically this program is accepting parameters as arguments
{
    int changed = 0; // Initializing the variable
    char buff[8];  // Initializing a char array of size 8
    while (changed == 0)  // Staring of while loop
    { 
        /*
            This loop will continue running if changed == 0.
            if the value of "changed" is not changing then this while loop will run infinite time.
        */
        gets(buff); // Taking input in buff
        if (changed !=0)  
        { 
            /*
                The program will enter this "if" statement when the value of "changed" is not equal to 0
                This is never going to happen as when "changed" is something other than 0, it will not even enter the while loop.
                If it will not enter the while loop, how can it enter the if statement.
            */
            break;
        }
        else{
            printf("Enter again: "); // It will enter when changed == 0, so it will always execute.
            continue;
            }
    }
    printf("the 'changed' variable is modified\n %d", changed); // If "while loop" ends then the program will come at this point and this print statement will execute.
}


Please let me know in the comments in case of any confusion. Also, please upvote if you like.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question 1. The following code asks the user to enter the password and check it with...
Question 1. The following code asks the user to enter the password and check it with the pre-defined password that stored in the system. If the entered password is correct, it will authenticate the user to log-in into the account, otherwise reject! Analyze the following code, how it works? Do you think this code is safe? Justify? Try to overwrite this code and report the outputs? Define any vulnerabilities in this code if exist? What we can do to make...
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2;...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } There is an issue with this...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
Debug code to get it to only count non alpha numeric values. Right now it always...
Debug code to get it to only count non alpha numeric values. Right now it always outputs zero. Why? please explain /*Problem 1*/ #include <stdio.h> #include <string.h> #include <stdlib.h> int loop_count; char scanned_line[100]; int non_alpha_num_checker = 0; int total; int main() { printf("Enter a line: "); fgets(scanned_line,sizeof scanned_line, stdin); for(loop_count = 0; loop_count != '\0'; loop_count++){ if(scanned_line[loop_count] >= 'A' && scanned_line[loop_count]<='Z') { continue; }    else if(scanned_line[loop_count] >= 'a' && scanned_line[loop_count]<='z') { continue; } else if(scanned_line[loop_count] >= '0' && scanned_line[loop_count]<='9')...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A child is created and prints a message five times. The original process (parent) prints a message only three times. How would this code look on the compiler if you could please post the code and the compiler results. Need Reference Thank You #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t pid; char *message; int n; printf("fork program starting\n"); pid =...
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...