{"record":{"id":"47c51cb58ebcfbc6","repo":"TooTallNate/Java-WebSocket","slug":"cannot-have-negative-offset","errorCode":null,"errorMessage":"Cannot have negative offset: ","messagePattern":"Cannot have negative offset: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/util/Base64.java","lineNumber":656,"sourceCode":"   * @param len     Length of data to convert\n   * @param options Specified options\n   * @return The Base64-encoded data as a String\n   * @throws java.io.IOException      if there is an error\n   * @throws IllegalArgumentException if source array is null, if source array, offset, or length\n   *                                  are invalid\n   * @see Base64#GZIP\n   * @see Base64#DO_BREAK_LINES\n   * @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;","sourceCodeStart":638,"sourceCodeEnd":674,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/util/Base64.java#L638-L674","documentation":"Base64.encodeBytesToBytes validates the offset/length window into the source array: a negative off throws IllegalArgumentException(\"Cannot have negative offset: N\") and a negative len throws for length. Together with bounds checks these guards ensure only a valid slice of the array is encoded.","triggerScenarios":"Calling encodeBytesToBytes(source, off, len, options) with a negative off or len — typically computed values (positions, chunk sizes) that went negative, or swapped arguments where len was passed in the off position.","commonSituations":"Chunked encoding loops where chunk start/size arithmetic underflows; passing -1 as a 'default' length; wrapping native/parsed lengths that failed and returned -1 sentinel values.","solutions":["Validate off >= 0 && len >= 0 (and off + len <= source.length) before calling; clamp or reject invalid windows.","Fix the computation producing the negative value — check for -1 sentinel returns from searches/reads and handle them before encoding.","If encoding whole arrays, use the no-offset overload encodeBytesToBytes(source) so off=0/len=source.length are applied automatically."],"exampleFix":"// before\nint off = input.indexOf(marker); // may be -1\nbyte[] out = Base64.encodeBytesToBytes(data, off, data.length - off, Base64.NO_OPTIONS);\n// after\nint off = input.indexOf(marker);\nif (off >= 0 && data.length - off >= 0) {\n  byte[] out = Base64.encodeBytesToBytes(data, off, data.length - off, Base64.NO_OPTIONS);\n}","handlingStrategy":"validation","validationCode":"if (off < 0 || len < 0 || off + len > source.length) {\n  throw new IllegalArgumentException(\n      \"Invalid window: off=\" + off + \" len=\" + len + \" size=\" + source.length);\n}","typeGuard":"static boolean validWindow(byte[] src, int off, int len) {\n  return src != null && off >= 0 && len >= 0 && off <= src.length && len <= src.length - off;\n}","tryCatchPattern":"try {\n  byte[] out = Base64.encodeBytesToBytes(source, off, len, options);\n} catch (IllegalArgumentException e) {\n  log.warn(\"Invalid encode window: {}\", e.getMessage());\n  out = null;\n}","preventionTips":["Handle -1 sentinel values from indexOf-style lookups before using them as offsets","Validate off/len window arithmetic in chunking loops before each encode call","Use the single-argument overloads when encoding whole arrays to avoid manual window math"],"tags":["base64","encoding","argument-out-of-range"],"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"}