Skip to content

Crash when using ErrorDebugString with non-null-terminated string #854

Description

@thezayas

Describe the bug

We are encountering a crash in production when using ErrorDebugString(int error) from AWS CRT C++.

The function returns a const char*, but in some cases the returned string appears to not be null-terminated, which leads to undefined behavior when used with std::string.

Code Snippet

const char* err = ErrorDebugString(errorCode);

std::string result = "";
result = err;  // <-- crash originates here
result += ", HTTP Status code: " + std::to_string(httpResponseCode);

Observed Behavior

  • Crash occurs during std::string assignment or concatenation

  • Call stack indicates failure inside:

      - std::string::assign
      - std::char_traits<char>::assign
    

Full code below

std::tuple<int, int, std::string> AwsIoTDeviceProxy::GetThingShadow(const std::string& deviceId, const std::string& shadowName, AuthenticationSettings authenticationSettings, bool isAuthProxy, bool isImpersonate, bool isValidNTLMVersion, UseTimerType eTimerType)
{
    int connectionCode = -1, httpStatusCode = 0;
    std::string shadow = "", result = "";

    HP::Shared::Semaphore semGetThingShadow;

    //handler to receive call back on publish event
    auto OnGetThingShadow = [this, &connectionCode, &httpStatusCode, &shadow, &result, &semGetThingShadow](std::string response, int errorCode, int httpResponseCode) -> void
        {
            //Signal the waiting caller
            SCOPE_GUARD{ connectionCode = errorCode; httpStatusCode = httpResponseCode; shadow = response;  semGetThingShadow.Post(); };

            fprintf(stderr, "OnGetThingShadow Response: %s\n", ErrorDebugString(errorCode));
            fprintf(stderr, "OnGetThingShadow Status: %d\n", httpResponseCode);

            if (errorCode)
            {
                result = ErrorDebugString(errorCode);
                result += ", HTTP Status code: " + std::to_string(httpResponseCode);

                LogError << "Error while OnGetThingShadow - ResponseCode: " << httpResponseCode << ", ErrorCode: " << errorCode << ", Response: " << (response.empty() ? "Empty" : response);
                LogError << "Error while OnGetThingShadow, Error Message: " << ErrorDebugString(errorCode);
            }
            else
            {
                result = "Operation successful";
                if (httpResponseCode == static_cast<int>(HttpUtility::Http::Status::OK))
                {
                    LogInfo << "OnGetThingShadow successful.";
                }
                else
                {
                    LogError << "Error while OnGetThingShadow: " << httpResponseCode << " response" << response;
                }
            }
        };

Expected Behavior

  • Returned string from ErrorDebugString() should be:

    • Properly null-terminated
    OR
    
    • API should document that the string may not be null-terminated

Current Behavior

Image

Reproduction Steps

Image

Possible Solution

No response

Additional Information/Context

No response

SDK version used

V2

Environment details (OS name and version, etc.)

Operating System: Microsoft Windows 11 Enterprise | 64-bit Build Number: 26100 ServicePack: 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.closing-soonThis issue will automatically close in 5 days unless further comments are made.response-requestedWaiting on additional info and feedback. Will move to "closing-soon" in 2 days.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions