Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

/* * Use of potentially unsafe function, gets() can cause buffer * overflows in code.

/* 

* Use of potentially unsafe function, gets() can cause buffer 

* overflows in code. 

* When a user enters a string that is longer than the 

* buffer is allocated for, it could cause memory reserved for 

* other functions to be overwritten. 

* In this case, the memory for

* the admin flag is overwritten when the length of the string

* input by the user is longer the 28 characters.

*

*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/*

* Prompt user to input their name

* If name = root, set admin flag and display admin message.

* Otherwise Display limited privledge 

*/

int main(int argc, char** argv) {

char buf[11];

int admin = 0;

printf("sizeof(buf) - %dnn", sizeof(buf));

//Prompt for name

printf("Enter name : ");

// The compiler warned me of this, so I turned off the hint in the settings

char* s = gets(buf);

printf("sizeof(s) - %dnn", sizeof(s));

//Display the length of string input by the user

int len = strlen(buf);

printf("Length of input - %dnn", len);

//If string entered equals root, set the admin flag to 1

//Else inform the user they only have limited privileges...

if (strcmp(buf, "root") == 0) { // Back door

admin = 1;

} else {

printf("You have limited privileges %s, please proceed.nn",buf);

}

//Check to see what the value of admin is, not expected to

//change, unless "root" is entered.

printf("Admin - %dnn", admin);

//If admin flag is other than 0, display message...

if (admin) {

printf("You have full control %s, have fun!nn", buf);

}

//Display thank you for visiting message...

printf("Thank you for visiting...nn");

return (EXIT_SUCCESS);

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question