Commit graph

12 commits

Author SHA1 Message Date
Paolo Antinori
5d580abb08 fix: avoid NRE when sorting by user-dependent keys without a user
A query sorted by a user-dependent key (PlayCount, IsFavoriteOrLiked,
DatePlayed, IsPlayed, IsUnplayed) but carrying no User caused a
NullReferenceException inside UserDataManager.GetUserData, surfacing as
"Failed to compare two elements in the array" (InvalidOperationException
wrapping the NRE from the LINQ sort) and 500-ing the /Items request.

Root cause: LibraryManager.GetComparer assigned comparer.User = user
without a null guard, so PlayCountComparer.GetValue called
UserDataManager.GetUserData(null, item), dereferencing user.Id.

Two-part fix:
- LibraryManager.GetComparer: when user is null and the sort key requires a
  user (IUserBaseItemComparer), substitute the SortName comparer so the
  result stays deterministic instead of 500-ing. SortName is the project's
  canonical tiebreaker (ItemsController injects it for album-by-artist).
- UserDataManager.GetUserData: ArgumentNullException.ThrowIfNull(user) as
  defense in depth (matches the existing guards on the SaveUserData
  overloads in the same file). On master this overload was rewritten to use
  ResolveUserDataRow, so the NRE dereferences user.Id rather than
  user.InternalId as on the release branch — same bug, different line.

Also fixes DateLastMediaAddedComparer being statically mis-tagged as
IUserBaseItemComparer: its GetDate is static and never reads User, so it
does not need one. Without this, the SortName fallback above would wrongly
engage for DateLastContentAdded on anonymous queries (returning SortName
order instead of date order). Re-tagged to IBaseItemComparer and dropped the
unused User/UserManager/UserDataManager properties.

Tests:
- UserDataManagerTests.GetUserData_NullUser_ThrowsArgumentNullException:
  reproduces the crash (NRE -> now ArgumentNullException). Added to master's
  existing UserDataManagerTests.
- LibraryManagerSortTests.Sort_UserDependentKey_NullUser_FallsBackToSortNameWithoutThrowing:
  Sort with a user-dependent key + null user no longer throws and returns
  items ordered by the SortName fallback (direction preserved).
- LibraryManagerSortTests.Sort_DateLastContentAdded_NullUser_OrdersByDateNotSortName:
  guards that DateLastContentAdded still sorts by date with no user (fixture
  chosen so date-desc and SortName-desc disagree, so a revert is caught).

Full Jellyfin.Server.Implementations.Tests suite: 642 passed, 0 failed.

Fixes #17393
2026-07-22 07:43:58 +02:00
Tim Eisele
49c6a99e00
Cleanup external item data cleanup (#14072) 2025-05-09 08:35:29 -06:00
JPVenson
42bdb22bfb Fixed namespaces 2025-03-25 16:45:00 +01:00
Cody Robibero
c7a94d48ae
Convert ItemSortBy to enum (#9765)
* Convert ItemSortBy to enum

* Rename Unknown to Default
2023-11-09 14:00:13 -07:00
Bond_009
7e8428e588 Enable nullable reference types for Emby.Server.Implementations 2021-05-20 22:48:53 +02:00
Patrick Barron
06f9cde22f Merge branch 'master' into userdb-efcore
# Conflicts:
#	Emby.Server.Implementations/EntryPoints/ServerEventNotifier.cs
#	Emby.Server.Implementations/Library/UserManager.cs
#	Emby.Server.Implementations/Sorting/IsFavoriteOrLikeComparer.cs
#	Emby.Server.Implementations/Sorting/IsPlayedComparer.cs
#	Emby.Server.Implementations/Sorting/IsUnplayedComparer.cs
#	Emby.Server.Implementations/TV/TVSeriesManager.cs
#	Jellyfin.Server.Implementations/Users/DefaultAuthenticationProvider.cs
2020-06-04 16:52:23 -04:00
Bond_009
4748105dce Enable TreatWarningsAsErrors for Jellyfin.Server.Implementations in Release mode 2020-05-29 11:28:19 +02:00
Patrick Barron
c464f700db Remove redundant qualifiers 2020-05-20 13:07:53 -04:00
Patrick Barron
9ad839c776 Initial migration code 2020-05-12 22:10:35 -04:00
Erwin de Haan
0efc699e3d Visual Studio Reformat: Emby.Server.Implementations Part S-S 2019-01-13 20:22:24 +01:00
Erwin de Haan
ec1f5dc317 Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.

Added exception messages to quite a few Argument*Exceptions.

Fixed rethorwing to be proper syntax.

Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)

Added some TODOs to log certain exceptions.

Fix sln again.

Fixed all AssemblyInfo's and added proper copyright (where I could find them)

We live in *current year*.

Fixed the use of braces.

Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.

Made more Methods that should be static static.

You can now use static to find bad functions!

Removed unused variable. And added one more proper XML comment.
2019-01-10 20:38:53 +01:00
Luke Pulverenti
3eb4091808 move additional classes to new server lib 2016-11-03 02:37:52 -04:00
Renamed from MediaBrowser.Server.Implementations/Sorting/DateLastMediaAddedComparer.cs (Browse further)