koral--/android-gif-drawable · error · IllegalArgumentException

Loop count of range <0, 65535>

Error message

Loop count of range <0, 65535>

What it means

GifInfoHandle.setLoopCount restricts the loop count to the GIF integer range <0, 65535> (Character.MAX_VALUE). Values outside this range raise IllegalArgumentException('Loop count of range <0, 65535>'). 0 typically means infinite looping, matching the GIF format's 16-bit loop count field.

Solutions

  1. Clamp the value: Math.max(0, Math.min(loopCount, Character.MAX_VALUE)) before calling setLoopCount.
  2. Use 0 for infinite looping instead of -1.
  3. Validate user/server supplied values against the 0..65535 range first.

Example fix

// before
gifInfoHandle.setLoopCount(-1);
// after
gifInfoHandle.setLoopCount(0); // 0 = infinite
// or: gifInfoHandle.setLoopCount(Math.max(0, Math.min(requested, Character.MAX_VALUE)));
Defensive patterns

Strategy: validation

Validate before calling

if (loopCount >= 0 && loopCount <= Character.MAX_VALUE) { gifInfoHandle.setLoopCount(loopCount); }

Type guard

boolean isValidLoopCount(int n) { return n >= 0 && n <= Character.MAX_VALUE; }

Try / catch

try { gifInfoHandle.setLoopCount(loopCount); } catch (IllegalArgumentException e) { gifInfoHandle.setLoopCount(0); }

Prevention

When it happens

Trigger: Calling setLoopCount(-1), setLoopCount(100000), or any int > 65535 / < 0 (e.g. using -1 to mean 'infinite', a convention from other libraries).

Common situations: Porting code from other GIF libraries where -1 means infinite; computing loop counts from user input or server data without clamping.

Related errors


AI-assisted analysis of koral--/android-gif-drawable@26ff795f78 (2026-09-10). Data as JSON: /api/errors/2d9cb2d6258e9724. Report an issue: GitHub.

Appendix: source

Thrown at android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifInfoHandle.java:230

	synchronized boolean reset() {
		return reset(gifInfoPtr);
	}

	synchronized void saveRemainder() {
		saveRemainder(gifInfoPtr);
	}

	synchronized String getComment() {
		return getComment(gifInfoPtr);
	}

	synchronized int getLoopCount() {
		return getLoopCount(gifInfoPtr);
	}

	void setLoopCount(@IntRange(from = 0, to = Character.MAX_VALUE) final int loopCount) {
		if (loopCount < 0 || loopCount > Character.MAX_VALUE) {
			throw new IllegalArgumentException("Loop count of range <0, 65535>");
		}
		synchronized (this) {
			setLoopCount(gifInfoPtr, (char) loopCount);
		}
	}

	synchronized long getSourceLength() {
		return getSourceLength(gifInfoPtr);
	}

	synchronized int getNativeErrorCode() {
		return getNativeErrorCode(gifInfoPtr);
	}

	void setSpeedFactor(@FloatRange(from = 0, fromInclusive = false) float factor) {
		if (factor <= 0f || Float.isNaN(factor)) {
			throw new IllegalArgumentException("Speed factor is not positive");
		}

View on GitHub (pinned to 26ff795f78)