-
Notifications
You must be signed in to change notification settings - Fork 700
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
serveFile crashes if passed-by-reference fileName parameter goes out of scope #1167
Comments
Good find, excellent bug report. Off the top of my mind (I've not looked at the classes involved), I suspect that one possible solution is to replace the filename string with a smart pointer of the same, then touch up all code that uses it. |
Wouldn't the easiest thing to do for a fix is simply change the |
Good point, and that is a simpler and more efficient change. |
@MatthewStephenson, if you want to provide a PR, this should be an easy fix you can test. Also make sure you bump the major version in |
@kiplingw The code change itself seems easy but I'm sufficiently unfamiliar with both the A+/async way of working and the github PR process that I'd rather leave it to someone more competent, if that's ok. |
Hmm, but the string is captured by value (see |
I could be wrong (I'm not at all hot on lambdas), but isn't it the reference to the string that is captured by value (effectively just a pointer) rather than the string itself? |
No, it works this way only for pointers, not for references. So it's captured by value with this syntax. There is a short demo: https://godbolt.org/z/WeWPf3Pv9 |
serveFile takes a const std::string& fileName parameter, which is a reference to a string passed in by the caller.
It later uses that reference in the then() lambda attached to the transport->asyncWrite call (http.cc line 1133). This lambda is called in a different thread, by which time serveFile can have returned, and the caller's filename string can have gone out of scope or otherwise been deleted.
This is causing a crash in my application. I have put together the following minimal application which exhibits the problem when compiled with address-sanitizer. After serving a couple of files, address-sanitizer detects the stack-use-after-scope.
g++ -std=c++2a -Wall -g -fsanitize=address -static-libasan -fno-omit-frame-pointer -o test test.cpp -lpistache
The text was updated successfully, but these errors were encountered: