Merge pull request #4008 from zmeizy/claude/getenv-invalid-name-errno

fix(libc): set EINVAL when getenv gets an invalid name
This commit is contained in:
Dean 2026-10-12 17:33:03 +13:00 committed by GitHub
commit e929880df4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View File

@ -50,7 +50,7 @@ int EnvironmentError(int error) { errno = error; return -1; }
extern "C" {
char* APS5_VABI getenv_nid_postfix(const char* name) {
if (!ValidEnvironmentName(name)) return nullptr;
if (!ValidEnvironmentName(name)) { errno = 22; return nullptr; }
try {
std::lock_guard lock(environmentMutex);
for (auto& entry : Environment())

View File

@ -37,6 +37,13 @@ int main() {
Require(std::strcmp(getenv_nid_postfix(key), "two") == 0);
Require(setenv_nid_postfix("bad=name", "x", 1) == -1);
Require(*__error_nid_postfix() == 22);
const char* const invalidNames[] = {nullptr, "", "bad=name"};
for (const char* name : invalidNames) {
*__error_nid_postfix() = 0;
Require(getenv_nid_postfix(name) == nullptr && *__error_nid_postfix() == 22);
}
*__error_nid_postfix() = 34;
Require(getenv_nid_postfix("ANYPS5_GUEST_ENV_ABSENT_4C27") == nullptr && *__error_nid_postfix() == 34);
Require(unsetenv_nid_postfix("") == -1);
char invalid[] = "no-separator";
Require(putenv_nid_postfix(invalid) == -1);