I'm getting a buffer overflow in this code, specifically the strcpy line, when I compile a release version of my program using G++.
Is there a better way to do what I'm trying to do here?
int main()
{
/*
* Calculates the date from a week number.
* @param week_num: the week number to calculate the date for.
* @return an integer denoting date, in YYYYMMDD format.
*/
struct tm tm{};
std::string week_num {"2020-47-1"}; // This value is what's being passed into the function when it crashes
const std::string week_num_tmp {week_num + "-1"};
volatile size_t test {std::strlen(week_num_tmp.c_str())}; //debugging variable - 93824994280448
auto week_char {std::make_unique<char>(week_num_tmp.length() + 1)};
strcpy(week_char.get(), week_num_tmp.c_str()); // Think the error is because length of c_str far greater than size of week_char?
strptime(week_char.get(), "%Y-%W-%w", &tm);
std::string year {std::to_string(tm.tm_year +1900)};
std::string month {std::to_string(tm.tm_mon + 1)};
std::string day {std::to_string(tm.tm_mday)};
month = (month.length() == 1) ? '0' + month : month; // Zero pad if single digit month
day = (day.length() == 1) ? '0' + day : day; // Zero pad if single digit day
const std::string date_str = year + month + day;
return std::stoi(date_str);
}