{"record":{"id":"2bb05b0cdb97ccb0","repo":"apache/hadoop","slug":"encoded-array-length-is-negative","errorCode":null,"errorMessage":"encoded array length is negative {}","messagePattern":"encoded array length is negative (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java","lineNumber":222,"sourceCode":"   */\n  @Override\n  public void readFields(DataInput in) throws IOException {\n    \n    // read and set the component type of the array\n    @SuppressWarnings(\"deprecation\")\n    String className = UTF8.readString(in);\n    Class<?> componentType = getPrimitiveClass(className);\n    if (componentType == null) {\n      throw new IOException(\"encoded array component type \"\n          + className + \" is not a candidate primitive type\");\n    }\n    checkDeclaredComponentType(componentType);\n    this.componentType = componentType;\n  \n    // read and set the length of the array\n    int length = in.readInt();\n    if (length < 0) {\n      throw new IOException(\"encoded array length is negative \" + length);\n    }\n    this.length = length;\n    \n    // construct and read in the array\n    value = Array.newInstance(componentType, length);\n\n    // do the inner loop.  Walk the decision tree only once.\n    if (componentType == Boolean.TYPE) {             // boolean\n      readBooleanArray(in);\n    } else if (componentType == Character.TYPE) {    // char\n      readCharArray(in);\n    } else if (componentType == Byte.TYPE) {         // byte\n      readByteArray(in);\n    } else if (componentType == Short.TYPE) {        // short\n      readShortArray(in);\n    } else if (componentType == Integer.TYPE) {      // int\n      readIntArray(in);\n    } else if (componentType == Long.TYPE) {         // long","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java#L204-L240","documentation":"readFields() reads the array length as a 4-byte int after the component type and rejects negative values with IOException. A negative length is impossible for an array written by write() (which writes Array.getLength of a real array), so this always signals corrupted, truncated, or misaligned input bytes.","triggerScenarios":"Bytes damaged in transit or on disk; reading from the wrong stream position (off-by-a-few bytes makes the int land on payload); partial write followed by read; checksums disabled so corruption propagates to the application layer.","commonSituations":"Reading a corrupted HDFS block or local spill file with checksums disabled/ignored; a custom RPC layer that truncates payloads; reading a file written with a different record framing.","solutions":["Enable/verify checksums (CRC) on the storage path and re-read or delete the corrupt file.","Check that the reader consumes exactly the bytes the writer produced (same framing, no skipped bytes).","Catch IOException per record, log the offset, and skip/quarantine bad records if the job tolerates it.","If corruption recurs, inspect the producing job's output and intermediate (spill/shuffle) health."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  w.readFields(in);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"negative\")) {\n    throw new DataCorruptionException(\"Stream misaligned or corrupt at \" + position, e);\n  }\n  throw e;\n}","preventionTips":["Keep checksums enabled on HDFS/local IO so corruption is caught below the application.","Frame records (length-prefixed) so a bad record can be skipped instead of desynchronizing the stream."],"tags":["hadoop","serialization","deserialization","data-corruption","stream-validation"],"backgroundTag":"corrupt-data-stream","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}