Skip to main content
added 163 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

I can see a few potential problems here:

  1. Since you allocated a char* array of input.size() elements, result[input.size()] is out of bounds.
  2. Similarly, std::string's size() is the number of characters - it doesn't include the trailing \0 needed for C-style strings. So every strcpy here risks doing a buffer overflow (risks, because it is possible for C++ std::strings to contain a null in the middle, terminating the strcpy mid way).
  3. You have set a limit on the number of elements of strings you delete, but then delete strings irrespective of whether that limit was breached. This risks a memory leak if the limit was exceeded.
  4. You use the array version of new new <type>[<size>]; This means you need to use the array version of delete delete [] <object> (Note the square brackets).

I can see a few potential problems here:

  1. Since you allocated a char* array of input.size() elements, result[input.size()] is out of bounds.
  2. Similarly, std::string's size() is the number of characters - it doesn't include the trailing \0 needed for C-style strings. So every strcpy here risks doing a buffer overflow (risks, because it is possible for C++ std::strings to contain a null in the middle, terminating the strcpy mid way).
  3. You have set a limit on the number of elements of strings you delete, but then delete strings irrespective of whether that limit was breached. This risks a memory leak if the limit was exceeded.

I can see a few potential problems here:

  1. Since you allocated a char* array of input.size() elements, result[input.size()] is out of bounds.
  2. Similarly, std::string's size() is the number of characters - it doesn't include the trailing \0 needed for C-style strings. So every strcpy here risks doing a buffer overflow (risks, because it is possible for C++ std::strings to contain a null in the middle, terminating the strcpy mid way).
  3. You have set a limit on the number of elements of strings you delete, but then delete strings irrespective of whether that limit was breached. This risks a memory leak if the limit was exceeded.
  4. You use the array version of new new <type>[<size>]; This means you need to use the array version of delete delete [] <object> (Note the square brackets).
Source Link
muru
  • 953
  • 4
  • 14

I can see a few potential problems here:

  1. Since you allocated a char* array of input.size() elements, result[input.size()] is out of bounds.
  2. Similarly, std::string's size() is the number of characters - it doesn't include the trailing \0 needed for C-style strings. So every strcpy here risks doing a buffer overflow (risks, because it is possible for C++ std::strings to contain a null in the middle, terminating the strcpy mid way).
  3. You have set a limit on the number of elements of strings you delete, but then delete strings irrespective of whether that limit was breached. This risks a memory leak if the limit was exceeded.