Skip to content

Commit 570f982

Browse files
trop[bot]zcbenz
andauthored
fix: LC_ALL env should not be changed (electron#26550)
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
1 parent 9900e69 commit 570f982

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

shell/browser/electron_browser_main_parts.cc

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/base_switches.h"
1212
#include "base/command_line.h"
1313
#include "base/feature_list.h"
14+
#include "base/optional.h"
1415
#include "base/path_service.h"
1516
#include "base/run_loop.h"
1617
#include "base/strings/string_number_conversions.h"
@@ -369,23 +370,34 @@ int ElectronBrowserMainParts::PreCreateThreads() {
369370
// which keys off of getenv("LC_ALL").
370371
// We must set this env first to make ui::ResourceBundle accept the custom
371372
// locale.
372-
g_setenv("LC_ALL", locale.c_str(), TRUE);
373+
std::unique_ptr<base::Environment> env(base::Environment::Create());
374+
base::Optional<std::string> lc_all;
375+
if (!locale.empty()) {
376+
std::string str;
377+
if (env->GetVar("LC_ALL", &str))
378+
lc_all.emplace(std::move(str));
379+
env->SetVar("LC_ALL", locale.c_str());
380+
}
373381
#endif
374382

375383
// Load resources bundle according to locale.
376384
std::string loaded_locale = LoadResourceBundle(locale);
377385

378-
#if defined(OS_LINUX)
379-
// Reset to the loaded locale if the custom locale is invalid.
380-
if (loaded_locale != locale)
381-
g_setenv("LC_ALL", loaded_locale.c_str(), TRUE);
382-
#endif
383-
384386
// Initialize the app locale.
385387
std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale);
386388
ElectronBrowserClient::SetApplicationLocale(app_locale);
387389
fake_browser_process_->SetApplicationLocale(app_locale);
388390

391+
#if defined(OS_LINUX)
392+
// Reset to the original LC_ALL since we should not be changing it.
393+
if (!locale.empty()) {
394+
if (lc_all)
395+
env->SetVar("LC_ALL", *lc_all);
396+
else
397+
env->UnSetVar("LC_ALL");
398+
}
399+
#endif
400+
389401
// Force MediaCaptureDevicesDispatcher to be created on UI thread.
390402
MediaCaptureDevicesDispatcher::GetInstance();
391403

spec-main/chromium-spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,13 @@ describe('command line switches', () => {
299299
});
300300
describe('--lang switch', () => {
301301
const currentLocale = app.getLocale();
302-
const testLocale = async (locale: string, result: string) => {
302+
const testLocale = async (locale: string, result: string, printEnv: boolean = false) => {
303303
const appPath = path.join(fixturesPath, 'api', 'locale-check');
304-
const electronPath = process.execPath;
305-
appProcess = ChildProcess.spawn(electronPath, [appPath, `--set-lang=${locale}`]);
304+
const args = [appPath, `--set-lang=${locale}`];
305+
if (printEnv) {
306+
args.push('--print-env');
307+
}
308+
appProcess = ChildProcess.spawn(process.execPath, args);
306309

307310
let output = '';
308311
appProcess.stdout.on('data', (data) => { output += data; });
@@ -314,6 +317,15 @@ describe('command line switches', () => {
314317

315318
it('should set the locale', async () => testLocale('fr', 'fr'));
316319
it('should not set an invalid locale', async () => testLocale('asdfkl', currentLocale));
320+
321+
const lcAll = String(process.env.LC_ALL);
322+
ifit(process.platform === 'linux')('current process has a valid LC_ALL env', async () => {
323+
// The LC_ALL env should not be set to DOM locale string.
324+
expect(lcAll).to.not.equal(app.getLocale());
325+
});
326+
ifit(process.platform === 'linux')('should not change LC_ALL', async () => testLocale('fr', lcAll, true));
327+
ifit(process.platform === 'linux')('should not change LC_ALL when setting invalid locale', async () => testLocale('asdfkl', lcAll, true));
328+
ifit(process.platform === 'linux')('should not change LC_ALL when --lang is not set', async () => testLocale('', lcAll, true));
317329
});
318330

319331
describe('--remote-debugging-pipe switch', () => {

spec/fixtures/api/locale-check/main.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const { app } = require('electron');
22

33
const locale = process.argv[2].substr(11);
4-
app.commandLine.appendSwitch('lang', locale);
4+
if (locale.length !== 0) {
5+
app.commandLine.appendSwitch('lang', locale);
6+
}
57

68
app.whenReady().then(() => {
7-
process.stdout.write(app.getLocale());
9+
if (process.argv[3] === '--print-env') {
10+
process.stdout.write(String(process.env.LC_ALL));
11+
} else {
12+
process.stdout.write(app.getLocale());
13+
}
814
process.stdout.end();
915

1016
app.quit();

0 commit comments

Comments
 (0)