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

Fix for out of bound read in std::strtol while parsing HTTP requests #1201

Closed
wants to merge 0 commits into from

Conversation

alexprabhat99
Copy link

Fixes: #1193

@tyler92
Copy link
Contributor

tyler92 commented Apr 12, 2024

Is it possible to use more modern way?

https://en.cppreference.com/w/cpp/utility/from_chars

It requires C++17

@kiplingw
Copy link
Member

Thanks for your contribution @alexprabhat99.

The std::strtol function really shouldn't be used in any new code. As @tyler92 already pointed out you're better off using std::from_chars which was introduced in C++ 17. Pistache supports C++17.

There are a number of advantages to using the newer std::from_chars. Unlike std::strtol and related, it is non-throwing, does everything it needs to do on the stack (non-allocating), which also means better memory safety, locale independent, better error reporting, and apparently has better performance.

I recommend refactoring your PR to use the std::from_chars in src/common/{http,http_header}.cc as well as in src/common/net.cc. Make sure you bump the patch version in version.txt per our documentation because you've changed the internal implementation, but not any interfaces.

@Tachi107, feel free to chime in.

@dgreatwood
Copy link
Contributor

(Just commenting since I noticed I was referenced on the issue)
I agree std::from_chars is a nice approach.
There are a couple of tiny differences between strtol and from_chars:
1/ from_chars does not ignore leading white space, strtol does.
2/ from_chars does not recognize a leading "+" sign (or signs), whereas strtol treats a leading plus (or pluses) as indicating that the value is positive, which is the default in any case.
For safety, you could move the cursor forward until it reaches a spot that is not whitespace, and is not a "+", ignoring those characters, or it reaches the end of the buffer. Then (presuming cursor has not reached buffer end) apply from_chars.

@alexprabhat99
Copy link
Author

I sincerely apologize for the delay. @kiplingw I have made the required changes and incorporated @dgreatwood's points. This PR is now ready for review.

version.txt Outdated Show resolved Hide resolved
src/common/http.cc Outdated Show resolved Hide resolved
src/common/net.cc Outdated Show resolved Hide resolved
src/common/net.cc Outdated Show resolved Hide resolved
src/common/net.cc Outdated Show resolved Hide resolved
src/common/http_header.cc Outdated Show resolved Hide resolved
src/common/http.cc Outdated Show resolved Hide resolved
@kiplingw
Copy link
Member

I sincerely apologize for the delay. @kiplingw I have made the required changes and incorporated @dgreatwood's points. This PR is now ready for review.

No worries @alexprabhat99. Thanks for contributing. I've left some comments above.

@alexprabhat99
Copy link
Author

@kiplingw I have made the changes as per your review.

@kiplingw
Copy link
Member

Thanks @alexprabhat99, but it looks like there are multiple failures in CI related to unit tests. I'm not sure if this is a result of your PR per se, but normally they don't all bail like that. Unfortunately I don't have time to go into a deep dive right now, but maybe @Tachi107 can take a look.

@dgreatwood
Copy link
Contributor

dgreatwood commented Apr 23, 2024 via email

@dgreatwood
Copy link
Contributor

dgreatwood commented Apr 23, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Out of bound read in std::strtol while parsing HTTP requests
4 participants