From dfeeca03664a7857405dbf7dbcb5c717c31d3ca1 Mon Sep 17 00:00:00 2001 From: "Wilhelmina \"Willow\" Poortenga" <156546731+wittywilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:58:55 -0500 Subject: [PATCH] Server folders: no crash on a huge page size; sizes counted like the server (#38) page compares the limit with what's left instead of adding it to the offset, which wrapped round and panicked. size counts 1024 to a KB, as the file browser's window, Statistics and the server do. FuzzItemsPaging and TestSizeCountsLikeTheServer cover both. Closes #38 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 ++++++++++ fuzz_test.go | 35 +++++++++++++++++++++++++++++++++++ module.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- sdui.go | 7 +++++-- sdui_test.go | 17 ++++++++++++++++- 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 fuzz_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7115d25..c4f927c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ What changed in WittyIntegratedFileBrowser, newest first. What changed before it had a repository of its own is in the [WittyMediaServer changelog](https://github.com/wittywilla/WittyMediaServer/blob/master/CHANGELOG.md). +## 0.4.2 + +- **Drive sizes match the rest of the server.** Server folders counted 1000 bytes to a KB while the file browser's own window, Statistics and the server count 1024, as your computer does, so the same disk read 511 GB in one place and 476 GB in another. It now reads the same everywhere. +- **Fixed:** asking for a page of server folders with an enormous size could stop the file browser's helper. + +## Developer notes for 0.4.2 + +- `page` compares the limit with what's left instead of adding it to the offset: `start+limit` wrapped round for a limit near the largest int and sliced with a negative end (a panic, found by the new `FuzzItemsPaging`). +- `size` uses 1024, as `fmtSize` in `web/FileBrowser.tsx` and the server's `fmtBytes` do; `TestSizeCountsLikeTheServer`. + ## 0.4.1 - The README's picture is retaken from WittyMediaServer 0.8.0-alpha, on a test server with open media only. diff --git a/fuzz_test.go b/fuzz_test.go new file mode 100644 index 0000000..5861afb --- /dev/null +++ b/fuzz_test.go @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +// Copyright (C) 2026 WittyWilla and contributors + +package wittyintegratedfilebrowser + +import ( + "encoding/json" + "testing" + + "github.com/wittywilla/wittymodule/sdui" +) + +// A page of places is asked for with any offset and limit a caller sends: none may crash the +// helper, and a page never holds more than was asked for or than there is. +func FuzzItemsPaging(f *testing.F) { + f.Add(`{"source":{"type":"wittyintegratedfilebrowser.places"},"offset":1,"limit":9223372036854775807,"viewer":{"admin":true}}`) + f.Add(`{"source":{"type":"wittyintegratedfilebrowser.places"},"offset":-5,"limit":-1,"viewer":{"admin":true}}`) + f.Add(`{"source":{"type":"wittyintegratedfilebrowser.folder","path":"../.."},"offset":0,"limit":2}`) + places := func() []Place { + return []Place{{Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1, Total: 2}, {Name: "Movies", Path: "/home/me/Movies", Group: "folders"}, {Name: "NAS", Path: "/mnt/nas", Group: "drives"}} + } + f.Fuzz(func(t *testing.T, raw string) { + var r sdui.ItemsRequest + if json.Unmarshal([]byte(raw), &r) != nil { + return + } + a, err := Items(r, places) + if err != nil { + return + } + if len(a.Items) > a.Total || (r.Limit > 0 && len(a.Items) > r.Limit) { + t.Errorf("offset %d limit %d: %d items of %d", r.Offset, r.Limit, len(a.Items), a.Total) + } + }) +} diff --git a/module.json b/module.json index 9b4b68e..0dc7a35 100644 --- a/module.json +++ b/module.json @@ -1,7 +1,7 @@ { "id": "wittyintegratedfilebrowser", "name": "WittyIntegratedFileBrowser", - "version": "0.4.1", + "version": "0.4.2", "repo": "wittywilla/WittyIntegratedFileBrowser", "license": "AGPL-3.0-or-later", "delivery": "helper", diff --git a/package-lock.json b/package-lock.json index 070036f..07266f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@wittywilla/wittyintegratedfilebrowser", - "version": "0.4.0", + "version": "0.4.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wittywilla/wittyintegratedfilebrowser", - "version": "0.4.0", + "version": "0.4.2", "license": "AGPL-3.0-or-later", "devDependencies": { "react": "^19.0.0", diff --git a/package.json b/package.json index bb3f82e..2c62338 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wittywilla/wittyintegratedfilebrowser", - "version": "0.4.1", + "version": "0.4.2", "description": "A folder browser in a window, for choosing folders on another computer from any device: drives, places, path bar, filtering, several folders at once. Read-only.", "license": "AGPL-3.0-or-later", "author": "WittyWilla (https://github.com/wittywilla)", diff --git a/sdui.go b/sdui.go index 053a61f..34a62bb 100644 --- a/sdui.go +++ b/sdui.go @@ -61,7 +61,8 @@ func Items(r sdui.ItemsRequest, places func() []Place) (sdui.ItemsAnswer, error) func page(r sdui.ItemsRequest, all []sdui.Item) sdui.ItemsAnswer { start := min(max(r.Offset, 0), len(all)) end := len(all) - if r.Limit > 0 && start+r.Limit < end { + // Compared as what's left, not start+limit: a limit near the largest int would wrap round. + if r.Limit > 0 && r.Limit < end-start { end = start + r.Limit } return sdui.ItemsAnswer{Items: all[start:end], More: end < len(all), Total: len(all)} @@ -153,8 +154,10 @@ func folderItems(path string) []sdui.Item { // fileIcons is the catalogue's icon for each kind of file FileKind names. var fileIcons = map[string]string{"video": "video", "audio": "music", "book": "book", "disc": "disc", "image": "photo", "subtitle": "list"} +// size is a number of bytes as the server and the file browser's own window write it: 1024 to a +// KB, as the operating systems show a disk, so a drive reads the same everywhere. func size(b uint64) string { - const k = 1000 + const k = 1024 switch { case b >= k*k*k*k: return fmt.Sprintf("%.1f TB", float64(b)/(k*k*k*k)) diff --git a/sdui_test.go b/sdui_test.go index 334772b..c69b910 100644 --- a/sdui_test.go +++ b/sdui_test.go @@ -29,7 +29,7 @@ func ask(t *testing.T, raw string) sdui.ItemsRequest { func TestPlaceItems(t *testing.T) { places := func() []Place { return []Place{ - {Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1_500_000_000_000, Total: 4_000_000_000_000}, + {Name: "Media", Path: "/mnt/media", Group: "drives", Free: 1_649_267_441_664, Total: 4_398_046_511_104}, {Name: "Movies", Path: "/home/me/Movies", Group: "folders"}, {Name: "NAS", Path: "/mnt/nas", Group: "drives", Label: "Network"}, } @@ -154,3 +154,18 @@ func TestItemsAreWhatEachSourceYields(t *testing.T) { } } } + +// A drive's size reads as the server and the file browser's window write it: 1024 to a KB. +func TestSizeCountsLikeTheServer(t *testing.T) { + for b, want := range map[uint64]string{ + 512: "512 bytes", + 2048: "2 KB", + 5 << 20: "5 MB", + 548_682_072_064: "511 GB", + 2_000_398_934_016: "1.8 TB", + } { + if got := size(b); got != want { + t.Errorf("size(%d) = %q, want %q", b, got, want) + } + } +}