ScholarMatic | 24/7 Homework Help

ScholarMatic Will Help You Write Your Essays and Term Papers

Answered » You can buy a ready-made answer or pick a professional tutor to order an original one.

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

by | Nov 28, 2023 | engineering

/* 

* 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);

}

ScholarMatic: Explanation & Answer

Your ready answer from a verified tutor is just a click away for as little as $14.99


  

Click Order Now to get 100% Original Answer Customized to your instructions!

HOME TO CERTIFIED WRITERS

Why Place An Order With Us?

  • Certified Editors
  • 24/7 Customer Support
  • Profesional Research
  • Easy to Use System Interface
  • Student Friendly Pricing

Have a similar question?

PLAGIRAISM FREE PAPERS

All papers we provide are well-researched, properly formatted and cited.

TOP QUALITY

All papers we provide are well-researched, properly formatted and cited.

HIGHLY SECURED

All papers we provide are well-researched, properly formatted and cited.

ScholarMatic: Get Started

Assignment Writing Service

Feel safe and secure when placing an order on our portal!
Fruitful cooperation begins with solid guarantees, and we are professional enough to promise perfect results. Let’s get it started!