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";...

  • 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]...

  • 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); } //_________________________________________________________________________________________________________________________________________________...

  • ****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;...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • c/c++ Last name: First name: ID: Sec: The name, ID and section number that appear at...

    c/c++ Last name: First name: ID: Sec: The name, ID and section number that appear at the beginning of the output of the program should be your own, not the ones used in the sample Note: results printed must show the data entered at run time, not the data shown in the sample run below! Sample run: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24...

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
ADVERTISEMENT