-2

can someone please convert this line:

strcpy_s(this->name, SIZE_NAME, d.getName());

to a strcpy function instead of strcpy_s?

thank you

Adb
  • 21
  • 4
  • 1
    why not use `std::string`s and get rid of `strcpy_x` completely, when you are writing c++? – 463035818_is_not_an_ai May 26 '20 at 14:45
  • 1
    Read the function documentation, then drop the size... that's pretty much it. – Lundin May 26 '20 at 14:45
  • Do you need to copy all of the functionality of `strcpy_s`? – David Schwartz May 26 '20 at 14:46
  • @idclev463035818 Because maybe wild and uncontrolled heap allocations aren't feasible for their product? – Lundin May 26 '20 at 14:46
  • @Lundin I think you are exaggerating a bit, but I do get your point ;) – 463035818_is_not_an_ai May 26 '20 at 14:47
  • @idclev463035818 Why I avoid the C++ tag... too many knee-jerk dogmas based on religious believes and too little disassembly. – Lundin May 26 '20 at 14:52
  • Note: `strncpy` might also be available to you. Like `strcpy_s` it reduces the possibility for a buffer overflow, but does it through truncation rather than a configurable handler. – user4581301 May 26 '20 at 15:06
  • @user4581301 -- yes, `strncpy` won't, on its own, overflow the buffer. But subsequent uses of the target array may well run off the end, since `strncpy` just stops when it hits the limit; it does not nul-terminate the array. – Pete Becker May 26 '20 at 15:42
  • @PeteBecker I deserve scorn for not expanding on exactly what truncation entailed in this case. A truly nasty surprise if you didn't read the documentation and expect to be safe.. – user4581301 May 26 '20 at 16:36

2 Answers2

1
strcpy(this->name, d.getName());

That was easy

john
  • 85,011
  • 4
  • 57
  • 81
  • 2
    This will overflow the buffer while the original code won't. So this does something different from the original code which I don't think is what the OP wanted -- but it's hard to tell. – David Schwartz May 26 '20 at 14:47
  • @DavidSchwartz What makes you think `d.getName()` contains strings with uncontrolled length? strcpy works just fine if the size of the data is known in advance. – Lundin May 26 '20 at 14:49
  • @Lundin The fact that someone chose to write this code using `strcpy_s` makes me think that and that someone who doesn't know the difference between `strcpy_s` and `strcpy` is trying to change it to use `strcpy` is the clincher. – David Schwartz May 26 '20 at 14:56
  • @DavidSchwartz Or they could simply be using MSVC which gives very strange diagnostics. There need not be a rationale other than ""Microsoft told me to". https://stackoverflow.com/questions/23486938/c4996-function-unsafe-warning-for-strcpy-but-not-for-memcpy/23490019#23490019 – Lundin May 26 '20 at 14:59
  • @Lundin Yeah, unfortunately, given that `strcpy_s` is a quirky function too. – David Schwartz May 26 '20 at 15:00
  • 1
    @DavidSchwartz And C11 `strcpy_s` in optional bounds-checking interface, which may or may not be compatible with MS libs, didn't exactly improve the situation. My take is that `_s` suffix means that the function is un`s`afe and non-portable. – Lundin May 26 '20 at 15:02
0

Here is an example:

if (strlen(d.getName()) >= SIZE_NAME) {
    throw std::runtime_error("There was a bug. Contact your programmer and tell them to use std::string");
}
strcpy(this->name, d.getName());
eerorika
  • 232,697
  • 12
  • 197
  • 326