{"record":{"id":"84671735e5fe9f28","repo":"TooTallNate/Java-WebSocket","slug":"cannot-have-offset-of-d-and-length-of-d-with-arr","errorCode":null,"errorMessage":"Cannot have offset of %d and length of %d with array of length %d","messagePattern":"Cannot have offset of (.+?) and length of (.+?) with array of length (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/util/Base64.java","lineNumber":664,"sourceCode":"   * @since 2.3.1\n   */\n  public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options)\n      throws java.io.IOException {\n\n    if (source == null) {\n      throw new IllegalArgumentException(\"Cannot serialize a null array.\");\n    }   // end if: null\n\n    if (off < 0) {\n      throw new IllegalArgumentException(\"Cannot have negative offset: \" + off);\n    }   // end if: off < 0\n\n    if (len < 0) {\n      throw new IllegalArgumentException(\"Cannot have length offset: \" + len);\n    }   // end if: len < 0\n\n    if (off + len > source.length) {\n      throw new IllegalArgumentException(\n          String\n              .format(\"Cannot have offset of %d and length of %d with array of length %d\", off, len,\n                  source.length));\n    }   // end if: off < 0\n\n    // Compress?\n    if ((options & GZIP) != 0) {\n      java.io.ByteArrayOutputStream baos = null;\n      java.util.zip.GZIPOutputStream gzos = null;\n      Base64.OutputStream b64os = null;\n\n      try {\n        // GZip -> Base64 -> ByteArray\n        baos = new java.io.ByteArrayOutputStream();\n        b64os = new Base64.OutputStream(baos, ENCODE | options);\n        gzos = new java.util.zip.GZIPOutputStream(b64os);\n\n        gzos.write(source, off, len);","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/util/Base64.java#L646-L682","documentation":"encodeBytesToBytes verifies that the requested slice off+len fits within the source array. When it would read past the end, the library throws IllegalArgumentException with the offending offset, length, and array length. It protects against out-of-bounds reads that would otherwise throw ArrayIndexOutOfBoundsException deeper in the encoder.","triggerScenarios":"Calling encodeBytesToBytes(source, off, len) where off + len > source.length, e.g. passing a length measured in a different unit (chars vs bytes) or reusing a length from another array.","commonSituations":"Copying a cached length from a previous larger buffer; off-by-one when the length was meant to be source.length - off; encoding a subrange after the array was reallocated smaller.","solutions":["Clamp the slice: len = Math.min(len, source.length - off) before encoding.","Assert off >= 0 && off + len <= source.length before the call.","If the length comes from external data, validate it against source.length at the deserialization boundary."],"exampleFix":"// before\nbyte[] out = Base64.encodeBytesToBytes(data, off, len);\n\n// after\nif (off < 0 || len < 0 || off + len > data.length) {\n    throw new IllegalArgumentException(\"slice out of bounds\");\n}\nbyte[] out = Base64.encodeBytesToBytes(data, off, len);","handlingStrategy":"validation","validationCode":"if (data != null && off >= 0 && len >= 0 && off + len <= data.length) {\n    byte[] out = Base64.encodeBytesToBytes(data, off, len);\n}","typeGuard":"static boolean inBounds(byte[] a, int off, int len) {\n    return a != null && off >= 0 && len >= 0 && off <= a.length && len <= a.length - off;\n}","tryCatchPattern":"try {\n    out = Base64.encodeBytesToBytes(data, off, len);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"encode slice out of bounds: \" + e.getMessage());\n    out = EMPTY;\n}","preventionTips":["Clamp with len = Math.min(len, data.length - off).","Re-derive lengths from the current array, never cache them across buffer resizes.","Watch for off-by-one: length is count of bytes, not an end index."],"tags":["java","base64","out-of-bounds","illegal-argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}