0

Somebody told me that this piece of code has some serious issues but I have not been able to get my head around such issues. Can you guys please educate me on this?

static char BASED_CODE szFilter[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";

const char* filter = "HTML Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||";
size_t length = strlen(filter);
strcpy_s(szFilter, length + 1, filter);

3 Answers3

2

Well, the buffer overrun leaps out at me – szFilter isn't big enough to receive filter.

Why not use std::string since you are using C++? That makes these issues vanish.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

The second parameter of strcpy_s() should be the size of the destination buffer; you've given it the size of the input string.

But as you're working in C++, you should avoid strcpy() (etc.) entirely, and use std::string.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

szFilter is shorter than filter, so there is not enough place to copy filter to szFilter. You current code has undefined behaviour.

pajton
  • 15,828
  • 8
  • 54
  • 65