fix(scanner): keep tag numbers within the int32 range (#6202)

* fix(scanner): keep tag numbers within the int32 range

A track number of 4294967295 (-1 stored as an unsigned 32-bit tag) was saved
as-is by 64-bit builds. 32-bit builds (armv5/6/7, 386) cannot read that value
back into an int, so every scan failed with "converting driver.Value type
int64 to a int: value out of range" when loading the folder's media files.

Track and disc numbers (and their totals) are now parsed as int32 and fall
back to 0 when out of range, matching how unparseable values are handled.
BPM values outside the int32 range are dropped. A migration resets existing
out-of-range track_number, disc_number and bpm values, and removes
out-of-range keys from album.discs, so databases written by 64-bit builds are
readable again by 32-bit ones. Persistent IDs are unaffected because they use
the raw tag text.

Fixes #6200

* fix(scanner): accept the int32 minimum as a BPM value

The BPM range check compared the absolute value against MaxInt32, which
rejected -2147483648 even though it fits in an int32. Compare against
MinInt32 and MaxInt32 separately, matching atoi32 and the migration.

* fix(scanner): treat negative track, disc and BPM values as missing

Track numbers, disc numbers and BPM can never be negative, so negative tag
values now map to 0 (track/disc, including totals) or nil (BPM), the same as
unparseable ones. The migration resets existing negative values as well as the
ones above the int32 range, and keeps only album disc keys from 0 to MaxInt32.
This commit is contained in:
Deluan Quintão 2026-09-22 19:51:32 -04:00 • committed by GitHub
commit 961ee8c413
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 51 additions and 9 deletions

View file

@ -0,0 +1,24 @@
-- +goose Up
-- +goose StatementBegin
-- 32-bit builds cannot read values above the int32 range written by 64-bit builds.
update media_file set track_number = 0
where track_number < 0 or track_number > 2147483647;
update media_file set disc_number = 0
where disc_number < 0 or disc_number > 2147483647;
update media_file set bpm = null
where bpm < 0 or bpm > 2147483647;
update album set discs = (
select json_group_object(key, value) from json_each(album.discs)
where cast(key as integer) between 0 and 2147483647
)
where json_valid(discs) and exists (
select 1 from json_each(album.discs)
where cast(key as integer) not between 0 and 2147483647
);
-- +goose StatementEnd
-- +goose Down
SELECT 1;

View file

@ -37,8 +37,8 @@ func (md Metadata) ToMediaFile(libID int, folderID string) model.MediaFile {
mf.CatalogNum = md.String(model.TagCatalogNumber)
mf.Comment = md.String(model.TagComment)
if f := md.NullableFloat(model.TagBPM); f != nil {
if v := int(math.Round(*f)); v != 0 {
mf.BPM = new(v)
if r := math.Round(*f); r > 0 && r <= math.MaxInt32 {
mf.BPM = new(int(r))
}
}
mf.Lyrics = md.mapLyrics()

View file

@ -131,6 +131,15 @@ var _ = Describe("ToMediaFile", func() {
Expect(toMediaFile(model.RawTags{"BPM": {"0"}}).BPM).To(BeNil())
Expect(toMediaFile(model.RawTags{"BPM": {"fast"}}).BPM).To(BeNil())
})
It("leaves BPM nil when the tag does not fit in 32 bits", func() {
Expect(toMediaFile(model.RawTags{"BPM": {"4294967295"}}).BPM).To(BeNil())
})
It("leaves BPM nil when the tag is negative", func() {
Expect(toMediaFile(model.RawTags{"BPM": {"-120"}}).BPM).To(BeNil())
})
It("keeps the largest 32-bit BPM value", func() {
Expect(toMediaFile(model.RawTags{"BPM": {"2147483647"}}).BPM).To(Equal(new(2147483647)))
})
})
Describe("BitDepth", func() {

View file

@ -148,15 +148,20 @@ func (md Metadata) tuple(key model.TagName) (int, int) {
return 0, 0
}
tuple := strings.Split(tag, "/")
t1, t2 := 0, 0
t1, _ = strconv.Atoi(tuple[0])
total := md.first(key + "total")
if len(tuple) > 1 {
t2, _ = strconv.Atoi(tuple[1])
} else {
t2tag := md.first(key + "total")
t2, _ = strconv.Atoi(t2tag)
total = tuple[1]
}
return t1, t2
return tagNumber(tuple[0]), tagNumber(total)
}
// tagNumber rejects negatives and values above int32, so the DB stays readable by 32-bit builds.
func tagNumber(s string) int {
v, err := strconv.ParseInt(s, 10, 32)
if err != nil || v < 0 {
return 0
}
return int(v)
}
var dateRegex = regexp.MustCompile(`([12]\d\d\d)`)

View file

@ -226,6 +226,10 @@ var _ = Describe("Metadata", func() {
Entry(nil, "2/10", "", 2, 10),
Entry(nil, "", "", 0, 0),
Entry(nil, "A", "", 0, 0),
Entry("ignores values that do not fit in 32 bits", "4294967295", "4294967296", 0, 0),
Entry("ignores a total that does not fit in 32 bits", "2/4294967295", "", 2, 0),
Entry("keeps the largest 32-bit value", "2147483647", "", 2147483647, 0),
Entry("ignores negative values", "-1", "-2", 0, 0),
)
Describe("Performers", func() {