Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small::string::substr causes crash #29

Open
1 of 4 tasks
frank-aurich opened this issue Aug 22, 2023 · 0 comments
Open
1 of 4 tasks

small::string::substr causes crash #29

frank-aurich opened this issue Aug 22, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@frank-aurich
Copy link

The problem

Calling small::string::substr(pos, count) with a count value larger than the size of the string causes a segfault.
According to the documentation, if count is larger than the size of the string, the function should return the substring from pos to the end of the string.
This is how std::string::substr() behaves.

Steps to Reproduce

#include <small/string.hpp>

template<typename T>
std::vector<T>
b_split(const T& str, const std::string_view& delims)
{
   std::vector<T> output;
   size_t first = 0;

   while (first < str.size())
   {
      const auto second = str.find_first_of(delims, first);

      if (first != second)
      {
         output.emplace_back(str.substr(first, second-first));
      }

      if (second == std::string::npos)
         break;

      first = second + 1;
   }

   return output;
}

int
main()
{
   using namespace std::literals;

   const char inputStr[] = "This is a ctest";

   const auto res = b_split<std::string>(inputStr, " "sv); // OK
   assert(res.size() == 4);

   const auto res2 = b_split<small::string>(inputStr, " "sv); // Crash
   assert(res.size() == 4);

   return 0;
}

Output

The app crashes with a segfault when using small::string.

Platform

  • cross-platform issue - linux
  • cross-platform issue - windows
  • cross-platform issue - macos
  • other: ___________

Environment Details

  • OS: Windows 10
  • OS Version: 22H
  • Compiler: Visual Studio 2022
  • Compiler version: 17.6.4

Proposed solution

Alternatives I've considered

Additional context

@frank-aurich frank-aurich added the bug Something isn't working label Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant