{"record":{"id":"67c53da220ce356c","repo":"mozilla/pdf.js","slug":"bad-uncompressed-block-length-in-flate-stream","errorCode":null,"errorMessage":"Bad uncompressed block length in flate stream","messagePattern":"Bad uncompressed block length in flate stream","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/flate_stream.js","lineNumber":328,"sourceCode":"      let blockLen = b;\n      if ((b = str.getByte()) === -1) {\n        this.#endsStreamOnError(\"Bad block header in flate stream\");\n        return;\n      }\n      blockLen |= b << 8;\n      if ((b = str.getByte()) === -1) {\n        this.#endsStreamOnError(\"Bad block header in flate stream\");\n        return;\n      }\n      let check = b;\n      if ((b = str.getByte()) === -1) {\n        this.#endsStreamOnError(\"Bad block header in flate stream\");\n        return;\n      }\n      check |= b << 8;\n      if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {\n        // Ignoring error for bad \"empty\" block (see issue 1277)\n        throw new FormatError(\"Bad uncompressed block length in flate stream\");\n      }\n\n      this.codeBuf = 0;\n      this.codeSize = 0;\n\n      const bufferLength = this.bufferLength,\n        end = bufferLength + blockLen;\n      buffer = this.ensureBuffer(end);\n      this.bufferLength = end;\n\n      if (blockLen === 0) {\n        if (str.peekByte() === -1) {\n          this.eof = true;\n        }\n      } else {\n        const block = str.getBytes(blockLen);\n        buffer.set(block, bufferLength);\n        if (block.length < blockLen) {","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/flate_stream.js#L310-L346","documentation":"Thrown during processing of a stored (uncompressed, BTYPE=00) DEFLATE block when the LEN field and its one's-complement NLEN check field are inconsistent. Per RFC 1951, a stored block carries LEN followed by ~LEN as an integrity check; this error means that check failed (with a special exception for empty blocks, per issue 1277).","triggerScenarios":"readBlock() encounters hdr===0 (stored block), reads 4 bytes for LEN and NLEN, and the condition check !== (~blockLen & 0xffff) && !(blockLen === 0 && check === 0) is true. This happens when the stored block's length fields are corrupt.","commonSituations":"A PDF producer that emits non-standard stored blocks with incorrect complement values. Byte-level corruption in an otherwise-valid stream. PDFs that were assembled by concatenating fragments with misaligned block boundaries.","solutions":["Re-export the PDF with a reputable tool (qpdf, Ghostscript, Acrobat) to rebuild all streams.","Validate with qpdf --check to identify the damaged object.","If you are the PDF producer, ensure stored DEFLATE blocks correctly write LEN followed by (~LEN & 0xFFFF).","Fall back to rendering the page without the affected stream content."],"exampleFix":"// before — manual stored-block write with wrong complement\nout.writeUInt16LE(len, 0);\nout.writeUInt16LE(len, 2); // bug: should be ~len\n\n// after — correct NLEN per RFC 1951\nout.writeUInt16LE(len, 0);\nout.writeUInt16LE((~len) & 0xffff, 2);","handlingStrategy":"try-catch","validationCode":"// Validate DEFLATE stored blocks server-side before embedding\nconst zlib = require('zlib');\nfunction validateDeflate(buf) {\n  try { zlib.inflateSync(buf); return { ok: true }; }\n  catch (e) { return { ok: false, error: e.message }; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await renderTask.promise;\n} catch (err) {\n  if (err.message?.includes('Bad uncompressed block length in flate stream')) {\n    console.warn('Corrupt DEFLATE stored block; attempting page without affected content.');\n  } else { throw err; }\n}","preventionTips":["Use standard DEFLATE libraries (zlib) rather than hand-rolling compression.","Validate PDF streams with qpdf --check before embedding.","If writing PDF generation code, test stored blocks with RFC 1951 conformance vectors."],"tags":["flate","deflate","stored-block","corrupt-data","pdf-stream","format-error"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}