summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2023-02-04 22:44:19 +0000
committerAndreas Kling <kling@serenityos.org>2023-02-05 16:40:51 +0100
commitb16ec1880cec01a182e095a1e14e0c642a8e2173 (patch)
tree6b7f6689157750a5c1843c7f442b70a2576b3f16 /Userland
parenteea4dc5bfe271f405eb9e0db461a286f4fe2cc6b (diff)
downloadserenity-b16ec1880cec01a182e095a1e14e0c642a8e2173.zip
LibC+LibCore: Remove serenity_setenv()
This was called from LibCore and passed raw StringView data that may not be null terminated, then incorrectly passed those strings to getenv() and also tried printing them with just the %s format specifier.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibC/stdlib.cpp7
-rw-r--r--Userland/Libraries/LibC/stdlib.h1
-rw-r--r--Userland/Libraries/LibCore/System.cpp18
3 files changed, 11 insertions, 15 deletions
diff --git a/Userland/Libraries/LibC/stdlib.cpp b/Userland/Libraries/LibC/stdlib.cpp
index 1cada26c97..687de3a7a3 100644
--- a/Userland/Libraries/LibC/stdlib.cpp
+++ b/Userland/Libraries/LibC/stdlib.cpp
@@ -472,14 +472,9 @@ int clearenv()
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
int setenv(char const* name, char const* value, int overwrite)
{
- return serenity_setenv(name, strlen(name), value, strlen(value), overwrite);
-}
-
-int serenity_setenv(char const* name, ssize_t name_length, char const* value, ssize_t value_length, int overwrite)
-{
if (!overwrite && getenv(name))
return 0;
- auto const total_length = name_length + value_length + 2;
+ auto const total_length = strlen(name) + strlen(value) + 2;
auto* var = (char*)malloc(total_length);
snprintf(var, total_length, "%s=%s", name, value);
s_malloced_environment_variables.set((FlatPtr)var);
diff --git a/Userland/Libraries/LibC/stdlib.h b/Userland/Libraries/LibC/stdlib.h
index eb955ae98b..fbe0b7ca55 100644
--- a/Userland/Libraries/LibC/stdlib.h
+++ b/Userland/Libraries/LibC/stdlib.h
@@ -32,7 +32,6 @@ int serenity_putenv(char const* new_var, size_t length);
int unsetenv(char const*);
int clearenv(void);
int setenv(char const* name, char const* value, int overwrite);
-int serenity_setenv(char const* name, ssize_t name_length, char const* value, ssize_t value_length, int overwrite);
char const* getprogname(void);
void setprogname(char const*);
int atoi(char const*);
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp
index 8fbda2e159..758881db8f 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -1430,15 +1430,17 @@ ErrorOr<void> mkfifo(StringView pathname, mode_t mode)
ErrorOr<void> setenv(StringView name, StringView value, bool overwrite)
{
-#ifdef AK_OS_SERENITY
- auto const rc = ::serenity_setenv(name.characters_without_null_termination(), name.length(), value.characters_without_null_termination(), value.length(), overwrite);
-#else
- DeprecatedString name_string = name;
- DeprecatedString value_string = value;
- auto const rc = ::setenv(name_string.characters(), value_string.characters(), overwrite);
-#endif
+ auto builder = TRY(StringBuilder::create());
+ TRY(builder.try_append(name));
+ TRY(builder.try_append('\0'));
+ TRY(builder.try_append(value));
+ TRY(builder.try_append('\0'));
+ // Note the explicit null terminators above.
+ auto c_name = builder.string_view().characters_without_null_termination();
+ auto c_value = c_name + name.length() + 1;
+ auto rc = ::setenv(c_name, c_value, overwrite);
if (rc < 0)
- return Error::from_syscall("setenv"sv, -errno);
+ return Error::from_errno(errno);
return {};
}