Skip to content

An open-source cross-platform alternative to AirDrop

fix: do not crash at startup when the executable path cannot be resolved (#3333)

Shlomo1161ed9909fb444
diff --git a/app/lib/util/shared_preferences/shared_preferences_portable.dart b/app/lib/util/shared_preferences/shared_preferences_portable.dart
index ad77b80a..f3b927de 100644
--- a/app/lib/util/shared_preferences/shared_preferences_portable.dart
+++ b/app/lib/util/shared_preferences/shared_preferences_portable.dart
@@ -1,8 +1,11 @@
import 'dart:io';
import 'package:localsend_app/util/shared_preferences/shared_preferences_file.dart';
+import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;
+final _logger = Logger('SharedPreferencesPortable');
+
/// Custom implementation of SharedPreferencesStorePlatform
/// that uses a file named settings.json located next to the executable.
/// This is used to for portable mode.
@@ -12,7 +15,33 @@ class SharedPreferencesPortable extends SharedPreferencesFile {
/// Returns the absolute path to the settings.json file next to the executable.
String _getSettingsPathFromExecutable() {
- final executablePath = Platform.resolvedExecutable;
- final executableParentPath = File(executablePath).parent.path;
- return path.join(executableParentPath, 'settings.json');
+ return buildSettingsPath(
+ executablePath: _resolveExecutable(),
+ fallbackDirectory: Directory.current.path,
+ );
+}
+
+/// Returns [Platform.resolvedExecutable], or null when the VM cannot resolve it.
+/// The getter throws a [TypeError] on some virtual disks (e.g. an ImDisk RAM disk),
+/// where reading it would crash the app before it even starts.
+/// See https://github.com/localsend/localsend/issues/3021
+/// and https://github.com/dart-lang/sdk/issues/53424
+String? _resolveExecutable() {
+ try {
+ final executablePath = Platform.resolvedExecutable;
+ return executablePath.isEmpty ? null : executablePath;
+ } catch (e) {
+ _logger.warning('Could not resolve the executable path', e);
+ return null;
+ }
+}
+
+/// Returns the absolute path to the settings.json file next to [executablePath],
+/// falling back to [fallbackDirectory] when the executable path is unknown.
+String buildSettingsPath({
+ required String? executablePath,
+ required String fallbackDirectory,
+}) {
+ final directory = executablePath == null ? fallbackDirectory : File(executablePath).parent.path;
+ return path.join(directory, 'settings.json');
}
diff --git a/app/test/unit/util/shared_preferences/shared_preferences_portable_test.dart b/app/test/unit/util/shared_preferences/shared_preferences_portable_test.dart
new file mode 100644
index 00000000..e4ab0115
--- /dev/null
+++ b/app/test/unit/util/shared_preferences/shared_preferences_portable_test.dart
@@ -0,0 +1,26 @@
+import 'package:localsend_app/util/shared_preferences/shared_preferences_portable.dart';
+import 'package:path/path.dart' as path;
+import 'package:test/test.dart';
+
+void main() {
+ test('should put settings.json next to the executable', () {
+ expect(
+ buildSettingsPath(
+ executablePath: path.join('opt', 'localsend', 'localsend_app'),
+ fallbackDirectory: path.join('home', 'user'),
+ ),
+ path.join('opt', 'localsend', 'settings.json'),
+ );
+ });
+
+ test('should fall back to the working directory when the executable is unknown', () {
+ // https://github.com/localsend/localsend/issues/3021
+ expect(
+ buildSettingsPath(
+ executablePath: null,
+ fallbackDirectory: path.join('home', 'user'),
+ ),
+ path.join('home', 'user', 'settings.json'),
+ );
+ });
+}