Question

How would you correct this function in C to prevent buffer overflow? void nameBuilder() {   ...

How would you correct this function in C to prevent buffer overflow?

void nameBuilder()
{
   char fname[10];
   char lname[10];
   char fullname[20];

   printf("Enter your first name: ");
   scanf("%s", fname);

   printf("Enter your last name: ");
   scanf("%s", lname);

   strcat(fullname, fname);
   strcat(fullname, " ");
   strcat(fullname, lname);

   printf("Welcome. %s\n", fullname);
   return;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// "while ( (getchar()) != '\n');"
// after "scanf()" statement
// flushes the input buffer

// using it after the “scanf()” statement clears the input buffer and allows the input in the desired container.
void nameBuilder()
{
char fname[10];
char lname[10];
char fullname[20];

printf("Enter your first name: ");
scanf("%s", fname);

   // flushes the standard input
// (clears the input buffer)
while ((getchar()) != '\n');
printf("Enter your last name: ");
scanf("%s", lname);
while ((getchar()) != '\n');
strcat(fullname, fname);
strcat(fullname, " ");
strcat(fullname, lname);
printf("Welcome. %s\n", fullname);
return;
}

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
How would you correct this function in C to prevent buffer overflow? void nameBuilder() {   ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • In C , checks if the entered password for admin is correct by calling checkAdminPassword. Part...

    In C , checks if the entered password for admin is correct by calling checkAdminPassword. Part of this function has been implemented, complete the statement in if(). Keep in mind that the entered password is plaintext whereas the password in struct user is in the hashsed form. The password for user admin is “s#1Pa5”. int checkAdminPassword(char* password, struct user* users, int count) { for (int i = 0; i < count; ++i) { if (strcmp((users + i)->username, "admin") == 0)...

  • A)Correct this code and explain what was corrected // C code - For Loop / Array...

    A)Correct this code and explain what was corrected // C code - For Loop / Array Population // This program will populate integers into an array, and then display those integers. // Developer: Johnson, William CMIS102 // Date: Mar 4, 2020 // Global constants #define INTLIMIT 10 #include <stdio.h> // This function will handle printing my name, class/section number, and the date void printStudentData() { char fullName[19] = "William J. Johnson"; char classNumber[9] = "CMIS 102"; char sectionNumber[5] = "4020";...

  • // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...

    // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors {     struct InvalidName { }; } class Name { public:     Name        ( string first_name = "", string last_name = "");     string first() const;     string last() const;     void   set_first( string fname );     void   set_last( string lname);     friend ostream& operator<< ( ostream & os, Name name );     friend bool operator== (Name name1, Name...

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

  • This one is a little tricky for me. Please put the following pseudocode into C++. Below...

    This one is a little tricky for me. Please put the following pseudocode into C++. Below the pseudocode is a header file as well as a cpp file that needs to be included. // Start // Declarations // TermPaper paper1 // string firstName // string lastName // string subject // character grade // output "Please enter first name. " // input firstName // output "Please enter last name. " // input lastName // output "Please enter subject. " // input...

  • Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char...

    Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char firstName[100]; char lastName[100]; printf("Enter Your Full Name: \n"); scanf("%s %s", firstName, lastName); printf("First Name: %s\n", firstName); printf("Last Name: %s\n", lastName); return 0; }

  • Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(cha...

    Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption                     void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey);    for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') {    letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...

  • You must use C Language. The Main Objective: Make the first and last name ALL CAPITALS...

    You must use C Language. The Main Objective: Make the first and last name ALL CAPITALS even if the user types in lower case letters. Lastly, flip the first name and last name around. So: HEIDI, HATFIELD to HATFIELD, HEIDI. (PLEASE uppercase all the letters) End Goal: HATFIELD, HEIDI KAISER, RUSSELL LIPSHUTZ, HOWARD PENKERT, DAWN WRIGHT, ELIZABETH Please use this reference below, to fix the code given. Code given is below, I want the user to be able to enter...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT