Kareadita/Kavita · warning · KavitaException

Image index {bookmarkDto.ImageOffset} is out of range. Page

Error message

Image index {bookmarkDto.ImageOffset} is out of range. Page has {images.Count} images

What it means

Thrown by BookService.CopyImageToTempFromBook (line 1258) when bookmarkDto.ImageOffset is >= the number of images on the target page (zero-based index out of range). The interpolated message reports both the requested offset and the actual image count.

Source

Thrown at Kavita.Services/BookService.cs:1258

            {
                counter++;
                continue;
            }

            var content = await contentFileRef.ReadContentAsync();
            doc.LoadHtml(content);

            var images = doc.DocumentNode.SelectNodes("//img")
                         ?? doc.DocumentNode.SelectNodes("//image");

            if (images == null || images.Count == 0)
            {
                throw new KavitaException("No images found on the specified page");
            }

            if (bookmarkDto.ImageOffset >= images.Count)
            {
                throw new KavitaException($"Image index {bookmarkDto.ImageOffset} is out of range. Page has {images.Count} images");
            }

            var targetImage = images[bookmarkDto.ImageOffset];

            // Get the image source attribute
            var srcAttributeName = GetImageSrcAttributeName(targetImage);

            if (string.IsNullOrEmpty(srcAttributeName))
            {
                throw new KavitaException("Image element does not have a valid source attribute");
            }

            var imageSource = targetImage.Attributes[srcAttributeName].Value;

            // Clean and get the correct key for the image
            var imageKey = CleanContentKeys(GetKeyForImage(book, imageSource));

            // Check if it's an external URL

View on GitHub (pinned to 9c3e540000)

Solutions

  1. Clamp ImageOffset to [0, imageCount-1] before requesting; treat it as zero-based.
  2. Re-query the page's image count when the reader reloads and invalidate stale offsets.

Example fix

// before
if (bookmarkDto.ImageOffset >= images.Count)
    throw new KavitaException($"Image index {bookmarkDto.ImageOffset} is out of range. Page has {images.Count} images");

// caller
var safeOffset = Math.Clamp(bookmarkDto.ImageOffset, 0, Math.Max(0, knownImageCount - 1));
Defensive patterns

Strategy: validation

Validate before calling

// Treat ImageOffset as zero-based and clamp before the request
bookmarkDto.ImageOffset = Math.Clamp(bookmarkDto.ImageOffset, 0, Math.Max(0, pageImageCount - 1));

Prevention

When it happens

Trigger: Bookmark/copy-image request with ImageOffset pointing past the last image, e.g. offset 2 on a page with 1 image, or offset computed before images were added/removed.

Common situations: Client reused a stale ImageOffset from a previous render; off-by-one where the client treats the offset as 1-based.

Related errors


AI-assisted analysis of Kareadita/Kavita@9c3e540000 (2026-08-13). Data as JSON: /api/errors/223c33306513695f. Report an issue: GitHub.