Skip to content

Commit 61fc5ac

Browse files
authored
Fix: Fixed possible crash when adding a new settings value (#13701)
1 parent 93b1566 commit 61fc5ac

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

src/Files.App/Utils/Serialization/Implementation/CachingJsonSettingsDatabase.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.Collections;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
84
namespace Files.App.Utils.Serialization.Implementation
95
{
106
internal sealed class CachingJsonSettingsDatabase : DefaultJsonSettingsDatabase
117
{
12-
private Dictionary<string, object?>? _settingsCache;
8+
private IDictionary<string, object?>? _settingsCache;
139

1410
public CachingJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJsonSettingsSerializer jsonSettingsSerializer)
1511
: base(settingsSerializer, jsonSettingsSerializer)
@@ -27,7 +23,7 @@ public CachingJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
2723
else
2824
{
2925
if (base.SetValue(key, defaultValue))
30-
_settingsCache.Add(key, defaultValue);
26+
_settingsCache.TryAdd(key, defaultValue);
3127

3228
return defaultValue;
3329
}
@@ -37,12 +33,8 @@ public CachingJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
3733
{
3834
_settingsCache ??= GetFreshSettings();
3935

40-
if (!_settingsCache.ContainsKey(key))
41-
{
42-
_settingsCache.Add(key, newValue);
43-
36+
if (_settingsCache.TryAdd(key, newValue))
4437
return SaveSettings(_settingsCache);
45-
}
4638
else
4739
return UpdateValueInCache(_settingsCache[key]);
4840

src/Files.App/Utils/Serialization/Implementation/DefaultJsonSettingsDatabase.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Diagnostics;
4+
using System.Collections.Concurrent;
75
using System.Text.Json;
86

97
namespace Files.App.Utils.Serialization.Implementation
@@ -20,7 +18,7 @@ public DefaultJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
2018
JsonSettingsSerializer = jsonSettingsSerializer;
2119
}
2220

23-
protected Dictionary<string, object?> GetFreshSettings()
21+
protected IDictionary<string, object?> GetFreshSettings()
2422
{
2523
string data = SettingsSerializer.ReadFromFile();
2624

@@ -29,10 +27,10 @@ public DefaultJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
2927
data = "null";
3028
}
3129

32-
return JsonSettingsSerializer.DeserializeFromJson<Dictionary<string, object?>?>(data) ?? new();
30+
return JsonSettingsSerializer.DeserializeFromJson<ConcurrentDictionary<string, object?>?>(data) ?? new();
3331
}
3432

35-
protected bool SaveSettings(Dictionary<string, object?> data)
33+
protected bool SaveSettings(IDictionary<string, object?> data)
3634
{
3735
var jsonData = JsonSettingsSerializer.SerializeToJson(data);
3836

@@ -58,14 +56,8 @@ public virtual bool SetValue<TValue>(string key, TValue? newValue)
5856
{
5957
var data = GetFreshSettings();
6058

61-
if (!data.ContainsKey(key))
62-
{
63-
data.Add(key, newValue);
64-
}
65-
else
66-
{
59+
if (!data.TryAdd(key, newValue))
6760
data[key] = newValue;
68-
}
6961

7062
return SaveSettings(data);
7163
}
@@ -88,7 +80,7 @@ public virtual bool ImportSettings(object? import)
8880
try
8981
{
9082
// Try convert
91-
var data = (Dictionary<string, object?>?)import;
83+
var data = (IDictionary<string, object?>?)import;
9284
if (data is null)
9385
{
9486
return false;

0 commit comments

Comments
 (0)