fix(downloads): guard ensureAbsolutePath against RangeError on short paths

This commit is contained in:
edde746
2026-04-25 14:10:37 +02:00
parent e61f0002e5
commit 23d958fac8
2 changed files with 21 additions and 4 deletions
+6 -4
View File
@@ -413,10 +413,12 @@ class DownloadStorageService {
// Recover from doubled app base path corruption:
// /data/.../app_flutter/data/.../app_flutter/downloads/...
final firstBaseIndex = storedPath.indexOf(baseDir.path);
final secondBaseIndex = storedPath.indexOf(baseDir.path, firstBaseIndex + baseDir.path.length);
if (firstBaseIndex != -1 && secondBaseIndex != -1) {
final tail = trimLeadingSeparators(storedPath.substring(secondBaseIndex + baseDir.path.length));
addCandidate(path.join(baseDir.path, tail));
if (firstBaseIndex != -1) {
final secondBaseIndex = storedPath.indexOf(baseDir.path, firstBaseIndex + baseDir.path.length);
if (secondBaseIndex != -1) {
final tail = trimLeadingSeparators(storedPath.substring(secondBaseIndex + baseDir.path.length));
addCandidate(path.join(baseDir.path, tail));
}
}
// Recover from paths that contain downloads/ but wrong prefix.
@@ -401,6 +401,21 @@ void main() {
final expected = p.join(tmpRoot.path, 'support', stored);
expect(resolved, expected);
});
test('absolute path shorter than the base dir does not throw RangeError', () async {
final settings = await SettingsService.getInstance();
final dss = DownloadStorageService.instance;
await dss.initialize(settings);
// Stored absolute path is 9 chars; base dir is much longer. Previously,
// the doubled-base-prefix recovery passed `firstBaseIndex + baseDir.path.length`
// (negative + len > storedPath.length) to a second `indexOf`, throwing.
const stored = '/nope.mkv';
final resolved = await dss.ensureAbsolutePath(stored);
// Falls back to the original absolute path (it doesn't exist on disk,
// and no other candidate could be derived from it).
expect(resolved, stored);
});
});
group('getReadablePath', () {