fix(libc): keep EINVAL from strerror_r for an unknown error with a short buffer

This commit is contained in:
fma-rc 2026-10-11 00:50:49 +02:00
parent d2f43cb87b
commit d90dff97a8
2 changed files with 4 additions and 1 deletions

View File

@ -54,7 +54,7 @@ int APS5_VABI strerror_r_nid_postfix(int error, char* buffer, std::size_t length
else std::snprintf(temporary, sizeof(temporary), "Unknown error: %d", error);
const auto required = std::strlen(temporary) + 1;
int result = Known(error) ? 0 : 22;
if (!buffer || length < required) result = 34;
if (Known(error) && (!buffer || length < required)) result = 34;
if (buffer && length) {
const auto count = required <= length ? required - 1 : length - 1;
std::memcpy(buffer, temporary, count);

View File

@ -36,4 +36,7 @@ int main() {
sentinel[0] = 'x';
Require(strerror_r_nid_postfix(22, sentinel, 0) == 34 && sentinel[0] == 'x');
Require(strerror_r_nid_postfix(22, nullptr, 0) == 34);
char truncated[4];
Require(strerror_r_nid_postfix(97, truncated, sizeof(truncated)) == 22 && std::strcmp(truncated, "Unk") == 0);
Require(strerror_r_nid_postfix(-1, truncated, sizeof(truncated)) == 22 && std::strcmp(truncated, "Unk") == 0);
}