Question: Review the following code and improve the code and remove vulnerabilities. #include #include #include #define bzero(b,len) (memset((b), '0', (len)), (void) 0) using namespace std; void

Review the following code and improve the code and remove vulnerabilities.

#include

#include

#include

#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)

using namespace std;

void setPassword(char pass[])

{

cout << "The password accepted is " << pass << endl;

}

char* getPasswordFromUser(char* prompt) {

char tmp[100];

cout << prompt;

cin >> tmp;

int len = strlen(tmp);

char* ret = (char*)malloc(len + 1);

strncpy_s(ret, len+1,tmp, len + 1);

bzero(tmp, len); /* don't leave passwd copy in memory */

return ret;

}

void changePassword() {

char* pass1 = getPasswordFromUser("enter new password: ");

char* pass2 = getPasswordFromUser("re-enter new password: ");

if (strcmp(pass1, pass2)) {

printf("passwords do not match ");

}

else {

setPassword(pass1);

}

bzero(pass1, strlen(pass1)); /* don't leave in memory */

bzero(pass2, strlen(pass2)); /* don't leave in memory */

free(pass1);

free(pass2);

}

int main()

{

changePassword();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!