1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/process/environment_internal.h"
11
12 #include <stddef.h>
13
14 #include <vector>
15
16 #include "build/build_config.h"
17
18 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
19 #include <string.h>
20 #endif
21
22 #if BUILDFLAG(IS_WIN)
23 #include "base/check_op.h"
24 #endif
25
26 namespace base {
27 namespace internal {
28
29 namespace {
30
31 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_WIN)
32 // Parses a null-terminated input string of an environment block. The key is
33 // placed into the given string, and the total length of the line, including
34 // the terminating null, is returned.
ParseEnvLine(const NativeEnvironmentString::value_type * input,NativeEnvironmentString * key)35 size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
36 NativeEnvironmentString* key) {
37 // Skip to the equals or end of the string, this is the key.
38 size_t cur = 0;
39 while (input[cur] && input[cur] != '=')
40 cur++;
41 *key = NativeEnvironmentString(&input[0], cur);
42
43 // Now just skip to the end of the string.
44 while (input[cur])
45 cur++;
46 return cur + 1;
47 }
48 #endif
49
50 } // namespace
51
52 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
53
AlterEnvironment(const char * const * const env,const EnvironmentMap & changes)54 base::HeapArray<char*> AlterEnvironment(const char* const* const env,
55 const EnvironmentMap& changes) {
56 std::string value_storage; // Holds concatenated null-terminated strings.
57 std::vector<size_t> result_indices; // Line indices into value_storage.
58
59 // First build up all of the unchanged environment strings. These are
60 // null-terminated of the form "key=value".
61 std::string key;
62 for (size_t i = 0; env[i]; i++) {
63 size_t line_length = ParseEnvLine(env[i], &key);
64
65 // Keep only values not specified in the change vector.
66 auto found_change = changes.find(key);
67 if (found_change == changes.end()) {
68 result_indices.push_back(value_storage.size());
69 value_storage.append(env[i], line_length);
70 }
71 }
72
73 // Now append all modified and new values.
74 for (const auto& i : changes) {
75 if (!i.second.empty()) {
76 result_indices.push_back(value_storage.size());
77 value_storage.append(i.first);
78 value_storage.push_back('=');
79 value_storage.append(i.second);
80 value_storage.push_back(0);
81 }
82 }
83
84 size_t pointer_count_required =
85 result_indices.size() + 1 + // Null-terminated array of pointers.
86 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer.
87 auto result = base::HeapArray<char*>::WithSize(pointer_count_required);
88
89 if (!value_storage.empty()) {
90 // The string storage goes after the array of pointers.
91 char* storage_data =
92 reinterpret_cast<char*>(&result[result_indices.size() + 1]);
93 memcpy(storage_data, value_storage.data(), value_storage.size());
94
95 // Fill array of pointers at the beginning of the result.
96 for (size_t i = 0; i < result_indices.size(); i++) {
97 result[i] = &storage_data[result_indices[i]];
98 }
99 }
100 result[result_indices.size()] = 0; // Null terminator.
101
102 return result;
103 }
104
105 #elif BUILDFLAG(IS_WIN)
106
AlterEnvironment(const wchar_t * env,const EnvironmentMap & changes)107 NativeEnvironmentString AlterEnvironment(const wchar_t* env,
108 const EnvironmentMap& changes) {
109 NativeEnvironmentString result;
110
111 // First build up all of the unchanged environment strings.
112 const wchar_t* ptr = env;
113 while (*ptr) {
114 std::wstring key;
115 size_t line_length = ParseEnvLine(ptr, &key);
116
117 // Keep only values not specified in the change vector.
118 if (changes.find(key) == changes.end()) {
119 result.append(ptr, line_length);
120 }
121 ptr += line_length;
122 }
123
124 // Now append all modified and new values.
125 for (const auto& i : changes) {
126 // Windows environment blocks cannot handle keys or values with NULs.
127 CHECK_EQ(std::wstring::npos, i.first.find(L'\0'));
128 CHECK_EQ(std::wstring::npos, i.second.find(L'\0'));
129 if (!i.second.empty()) {
130 result += i.first;
131 result.push_back('=');
132 result += i.second;
133 result.push_back('\0');
134 }
135 }
136
137 // Add the terminating NUL.
138 result.push_back('\0');
139 return result;
140 }
141
142 #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
143
144 } // namespace internal
145 } // namespace base
146