1
0

fix: change the behavior of printf in string op.

- add compiler hint for checking the arguments of printf.
- change the return value of printf. from std::expected to normal value. use C++ exception to indicate error.
	* the error of printf usually caused by programmer. so it can be found when testing program.
	* so i use std::logic_error to indicate this and programmer should fix this before releasing program.
- change the use of encoding convertion. for those cases that convertion must be safe, we unwrap it directly.
This commit is contained in:
2025-09-22 22:14:36 +08:00
parent 45e4031b5c
commit c85830902b
22 changed files with 240 additions and 164 deletions

View File

@ -25,10 +25,7 @@ namespace yycc::rust::env {
#if defined(YYCC_OS_WINDOWS)
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablew
// Convert to wchar
auto wname = ENC::to_wchar(name);
if (!wname.has_value()) {
return std::unexpected(EnvError::BadEncoding);
}
auto wname = ENC::to_wchar(name).value();
// Prepare a variable with proper init size for holding value.
std::wstring wvalue;
@ -42,7 +39,7 @@ namespace yycc::rust::env {
// So we forcely use checked add and sub for this bad behavior.
auto fct_size = SAFEOP::checked_add<size_t>(wvalue.size(), 1);
if (!fct_size.has_value()) return std::unexpected(EnvError::BadArithmetic);
auto rv = ::GetEnvironmentVariableW(wname.value().c_str(), wvalue.data(), fct_size.value());
auto rv = ::GetEnvironmentVariableW(wname.c_str(), wvalue.data(), fct_size.value());
// Check the return value
if (rv == 0) {
@ -95,20 +92,10 @@ namespace yycc::rust::env {
#if defined(YYCC_OS_WINDOWS)
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew
// Convert to wchar
auto wname = ENC::to_wchar(name);
auto wvalue = ENC::to_wchar(value);
if (!(wname.has_value() && wvalue.has_value())) {
return std::unexpected(EnvError::BadEncoding);
}
// Delete variable and check result.
auto rv = ::SetEnvironmentVariableW(wname.value().c_str(), wvalue.value().c_str());
if (!rv) {
return std::unexpected(EnvError::BadCall);
}
return {};
// Convert to wchar, set variable, and check result.
auto rv = ::SetEnvironmentVariableW(ENC::to_wchar(name).value().c_str(), ENC::to_wchar(value).value().c_str());
if (!rv) return std::unexpected(EnvError::BadCall);
else return {};
#else
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
@ -129,19 +116,10 @@ namespace yycc::rust::env {
#if defined(YYCC_OS_WINDOWS)
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew
// Convert to wchar
auto wname = ENC::to_wchar(name);
if (!wname.has_value()) {
return std::unexpected(EnvError::BadEncoding);
}
// Delete variable and check result.
auto rv = ::SetEnvironmentVariableW(wname.value().c_str(), NULL);
if (!rv) {
return std::unexpected(EnvError::BadCall);
}
return {};
// Convert to wchar, delete variable, and check result.
auto rv = ::SetEnvironmentVariableW(ENC::to_wchar(name).value().c_str(), NULL);
if (!rv) return std::unexpected(EnvError::BadCall);
else return {};
#else
// Reference: https://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html