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