jellyfin/Emby.Server.Implementations/Library
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
..
Resolvers Add TVDB provider ID support for movies (#17255) 2026-07-20 12:23:20 +02:00
Search Apply suggestions from code review 2026-06-07 22:07:35 +02:00
SimilarItems Limit similar items to user accessible libraries 2026-07-15 11:47:59 +02:00
Validators Merge remote-tracking branch 'upstream/master' into perf-rebased 2026-04-19 10:23:34 +02:00
CoreResolutionIgnoreRule.cs Skip non-media folders in movie resolver (#14724) 2025-09-07 13:02:52 +02:00
DotIgnoreIgnoreRule.cs Fix Sonar comments 2026-05-04 02:16:00 +02:00
ExternalDataManager.cs Fix external data pruning on item deletion 2026-05-26 20:44:03 +02:00
IgnorePatterns.cs Add ignore patterns for Hungarian sample files (#16238) 2026-03-29 12:38:46 +02:00
KeyframeManager.cs Recognize file changes and remove data on change (#13839) 2025-05-04 21:21:44 -06:00
LibraryManager.cs fix: avoid NRE when sorting by user-dependent keys without a user 2026-07-22 07:43:58 +02:00
LiveStreamHelper.cs Enable nullable for more files 2024-03-13 14:56:51 +01:00
MediaSourceManager.cs remove PlaybackPositionTicks from MediaSourceInfo 2026-07-14 10:05:32 +02:00
MediaStreamSelector.cs WIP fixed namespaces 2025-03-25 15:30:22 +00:00
MusicManager.cs Backport pull request #15133 from jellyfin/release-10.11.z 2025-10-27 15:43:17 -04:00
PathExtensions.cs Replaced string.Empty with ReadOnlySpan<char>.Empty 2026-07-08 01:40:26 +02:00
PathManager.cs Omit external trickplay directory if itme has no path 2026-06-07 14:56:31 +02:00
ResolverHelper.cs Fix modification checks and make sure to use UTC (#14347) 2025-06-26 17:50:37 -06:00
SplashscreenPostScanTask.cs Set max rating for splashscreen to 13 2026-05-14 18:43:39 -04:00
UserDataManager.cs fix: avoid NRE when sorting by user-dependent keys without a user 2026-07-22 07:43:58 +02:00
UserViewManager.cs Fix BoxSet library visibility 2026-04-26 18:53:17 +02:00