cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

Post request: httplib.h hanging with a EXC_BAD_ACCESS

Open aebian opened this issue 1 year ago • 7 comments

Hi yhirose,

Thanks for the library, appreciated. I have a issue with a post request and I'm not sure where the issue comes from.

Problem Description: httplib.h hanging with a EXC_BAD_ACCESS. --> Exception: EXC_BAD_ACCESS (code=1, address=0x10) Cause unkown. I get redirect to the function.h in CLion on line 379 which has the following code: _f = __f._f->__clone();

Steps to reproduce:

  1. Have a simple httplib.h Server running that accepts arguments.
  2. Call that URL and let it execute your function.
  3. Function gets called and spits put a connected, then tries a post request.
  4. Server crashes with a EXC_BAD_ACCESS fault. --> Exception: EXC_BAD_ACCESS (code=1, address=0x10)
  5. The CLion debugger mentions something that this = httplib:DataSink& at 0x16f5ac068 but it seems empty.
  6. Post request failed.

Expected Result: The post request succeeds and a json response is returned. At least a 200 should be picked up. In CURL this works...

Impact: Application throws major exception and hangs.

Attachments:

  1. Code snippet (the function) of my post request
  2. Screenshot of the params values
  3. Screenshot of the Crash

Additional Information: It does not matter if I hardcode the params directly (including the base_url). I get the same error always. With my IDE I can confirm the crash only happens when the post request is being executed. Everything before works flawless.

I can also confirm that the params are all strings and correctly filled. Can you please help me understand, if something on my side is causing the issue and not the library?

I did a test via curl -X POST "https://<complete-url>" -d <complete-list-of-params> and it returned a 200 and a josn response.

bool handleAuthCode(const std::string& code, const std::string& acsRef)
{
    httplib::SSLClient cliACS("login.microsoftonline.com", 443);
    httplib::Params acsParams
    {
        {"client_id", std::string(client_id)},
        {"redirect_uri", acsBaseURL() + "/auth/proc"},
        {"grant_type", "authorization_code"},
        {"client_secret", std::string(client_secret)},
        {"code", code},
        {"state", acsRef}
    };
    std::string base_url = "/" + std::string(tenant_id) + "/oauth2/v2.0/token";

    try
    {
        const auto res = cliACS.Post (base_url, acsParams);
        if (!res)
        {
            std::print("HTTP Error: {0}\n", to_string(res.error()));
            std::cerr << "HTTP Error:" << to_string(res.error());
            return false;
        }
        if (res->status == 200)
        {
            return true;
        }
    }
    catch (const std::exception& e)
    {
        std::cerr << "ACS warning: handleAuthCode function had a major malfunction" << e.what() << std::endl;
    }
    return false;
}

image image

aebian avatar Dec 16 '24 21:12 aebian

@Aebian thank you for the report, but I have no idea why the problem happens to be honest. There is one issue talking about EXC_BAD_ACCESS, but it was a user's mistake. Could you investigate a possibility that outside code causes this EXC_BAD_ACCESS problem instead of cpp-httplib? Sorry that I can't be of much help...

https://github.com/yhirose/cpp-httplib/issues/635#issuecomment-686446935

yhirose avatar Dec 17 '24 01:12 yhirose

@yhirose Thanks for the reply.

I have a hard time finding the root cause for the EXC_BAD_ACCESS. It only gets triggered when the const auto res = cliACS.Post (base_url, acsParams); is fired. When I set a breakpoint on this, as long as I don't let it execute, No issue.

I stepped into the call and the last thing it did was:

inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
                               const Params &params) {
  auto query = detail::params_to_query_str(params);
  return Post(path, headers, query, "application/x-www-form-urlencoded");
}

The return basically, but I'm not sure if this is just a false positive. The library was building the query_string correctly, I saw that in my debugging.

As seen above, all params are filled, I also tried hardcoding the values. If it would be outside code, I wonder why it only crashes when the post is executed, but not before.

Do you have other ideas that I could try? Is there a way I could get detailed error logging of the post request itself? I'm unsure what exactly causes the crash, but if Microsoft would be at fault, I would expect a error message to be returned, but my debug efforts in catching a error that helps me find the root cause went nowhere as of now.

Thanks!

aebian avatar Dec 17 '24 06:12 aebian

How about Get method as below? Does it cause the crash problem?

  httplib::Client client("https://google.com");
  client.set_follow_location(true);
  auto res = client.Get("/");

yhirose avatar Dec 17 '24 23:12 yhirose

That's odd. It causes the crash problem too and goes to the same cursor in function.h on line 379 which has the following code: _f = __f._f->__clone();

image

If I set a breakpoint and don't let it run, no crash. If I comment out the post and let the program run without a post, it doesn't crash and continues as expected.

My version of httplib.h seems to be 0.18.3 (so the latest published release) Oh gosh and what I forgot to add, my C++ version used is 23. Not sure if this could cause issues, as it is still in development...

aebian avatar Dec 18 '24 06:12 aebian

If the simple Get method doesn't work, the problem seems to reside out of cpp-httplib since I have never experienced EXC_BAD_ACCESS in the past. I recommend you to make the smallest possible example which can reproduce the problem. Sorry that I am not able to give you meaningful information...

yhirose avatar Dec 18 '24 13:12 yhirose

Hi,

I built a sample test program now. The C++ code for it is:

#include <iostream>
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include <httplib.h>

int main()
{
    httplib::Server srvACS;

    srvACS.Get("/", [](const httplib::Request& req, httplib::Response& res)
    {
        httplib::Client client("https://google.com");
        client.set_follow_location(true);
        client.enable_server_certificate_verification(false);
        auto res2 = client.Get("/");

        if (res2->status == 200)
        {
            std::cerr << "Likely success." << std::endl;
        }
        else
        {
            std::cerr << "Likely failed." << std::endl;
        }
    });

    std::print("{0}\n", "Test Server is running on port 4288");
    srvACS.listen("0.0.0.0", 4288);
}

And it seems to work... image

This is strange... In my main project it only happens on the Post. But the debugger tells me, all variables are filled as strings. It makes no sense to me. But seems like httplib.h under C++23 is not the issue here, otherwise the test program would've failed as well...

aebian avatar Dec 18 '24 20:12 aebian

Okay found the reason for the crash. I passed along the request and response as references and not directly and it seems those were already destroyed by the time my other code kicked in.

Thanks for the help!

aebian avatar Dec 19 '24 22:12 aebian