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.
const char* err = ErrorDebugString(errorCode);
std::string result = "";
result = err; // <-- crash originates here
result += ", HTTP Status code: " + std::to_string(httpResponseCode);
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;
}
}
};
Operating System: Microsoft Windows 11 Enterprise | 64-bit Build Number: 26100 ServicePack: 0
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
Observed Behavior
Crash occurs during std::string assignment or concatenation
Call stack indicates failure inside:
Full code below
Expected Behavior
Returned string from ErrorDebugString() should be:
Current Behavior
Reproduction Steps
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