{"record":{"id":"d5cdc68bde7406cb","repo":"bumptech/glide","slug":"cannot-add-invalid-orientation-orientation","errorCode":null,"errorMessage":"Cannot add invalid orientation: {orientation}","messagePattern":"Cannot add invalid orientation: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/load/data/ExifOrientationStream.java","lineNumber":71,"sourceCode":"        0x00,\n        0x02,\n        /* component count. */\n        0x00,\n        0x00,\n        0x00,\n        0x01,\n        /* 2 byte orientation value, the first byte of which is always 0. */\n        0x00,\n      };\n  private static final int SEGMENT_LENGTH = EXIF_SEGMENT.length;\n  private static final int ORIENTATION_POSITION = SEGMENT_LENGTH + SEGMENT_START_POSITION;\n  private final byte orientation;\n  private int position;\n\n  public ExifOrientationStream(InputStream in, int orientation) {\n    super(in);\n    if (orientation < -1 || orientation > 8) {\n      throw new IllegalArgumentException(\"Cannot add invalid orientation: \" + orientation);\n    }\n    this.orientation = (byte) orientation;\n  }\n\n  @Override\n  public boolean markSupported() {\n    return false;\n  }\n\n  // No need for synchronized since all we do is throw.\n  @SuppressWarnings(\"UnsynchronizedOverridesSynchronized\")\n  @Override\n  public void mark(int readLimit) {\n    throw new UnsupportedOperationException();\n  }\n\n  @Override\n  public int read() throws IOException {","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/load/data/ExifOrientationStream.java#L53-L89","documentation":"ExifOrientationStream wraps an InputStream to inject an EXIF orientation byte into the image data. The orientation parameter must be a valid EXIF orientation value in the range [-1, 8], where -1 indicates no orientation and 1-8 are the standard EXIF orientation values. Values outside this range are meaningless and rejected with an IllegalArgumentException at construction time.","triggerScenarios":"Creating an ExifOrientationStream with an orientation value read from EXIF metadata that was corrupt or misinterpreted. Passing a raw byte value that exceeds 8. Passing -2 or lower due to a signed/unsigned conversion error.","commonSituations":"Custom image decoders that read EXIF orientation and construct this stream directly. Incorrect parsing of EXIF orientation integer from image metadata. Unit tests with hardcoded out-of-range values.","solutions":["Validate the orientation value is in [-1, 8] before constructing ExifOrientationStream","Use ExifInterface#getOrientationAttribute and normalize the value before use","Default to 0 or -1 when the orientation value is unknown or unparseable"],"exampleFix":"// before\nnew ExifOrientationStream(stream, rawOrientation);\n// after\nint safeOrientation = (orientation < -1 || orientation > 8) ? ExifInterface.ORIENTATION_NORMAL : orientation;\nnew ExifOrientationStream(stream, safeOrientation);","handlingStrategy":"validation","validationCode":"int orientation = readExifOrientation(); // your source\nif (orientation < -1 || orientation > 8) {\n  orientation = android.media.ExifInterface.ORIENTATION_NORMAL; // default to 1 or 0\n}\nnew ExifOrientationStream(inputStream, orientation);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate EXIF orientation values are in [-1, 8] before constructing ExifOrientationStream","Default to ORIENTATION_NORMAL when the value is unknown or unparseable","Be aware of signed/unsigned conversion when reading raw EXIF bytes"],"tags":["glide","exif","orientation","validation"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}