A Security Engineer on Meta’s product security team is reviewing a small C++ utility used in a Messenger backend component. The code parses user-controlled input and builds a response buffer. Your task is to identify one memory leak and one buffer overflow vulnerability, explain why each occurs, and propose secure fixes.
#include <cstring>
#include <iostream>
void processMessage(const char* input) {
char* session = new char[64];
char reply[16];
strcpy(session, "active-session");
if (strlen(input) < 32) {
strcpy(reply, input);
}
std::cout << session << ": " << reply << std::endl;
}
input is user-controlled.std::string, std::array, or RAII.A strong answer should precisely distinguish between stack and heap memory issues, explain the impact of unbounded copies, and show a fix that removes both vulnerabilities rather than patching symptoms.