{"record":{"id":"90c6f3b7fc545a30","repo":"mermaid-js/mermaid","slug":"packet-block-start-is-invalid-cannot-have-a-ze","errorCode":null,"errorMessage":"Packet block ${start} is invalid. Cannot have a zero bit field.","messagePattern":"Packet block (.+?) is invalid\\. Cannot have a zero bit field\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/diagrams/packet/parser.ts","lineNumber":31,"sourceCode":"  let lastBit = -1;\n  let word: PacketWord = [];\n  let row = 1;\n  const { bitsPerRow } = db.getConfig();\n\n  for (let { start, end, bits, label } of ast.blocks) {\n    if (start !== undefined && end !== undefined && end < start) {\n      throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);\n    }\n    start ??= lastBit + 1;\n    if (start !== lastBit + 1) {\n      throw new Error(\n        `Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${\n          lastBit + 1\n        }.`\n      );\n    }\n    if (bits === 0) {\n      throw new Error(`Packet block ${start} is invalid. Cannot have a zero bit field.`);\n    }\n    end ??= start + (bits ?? 1) - 1;\n    bits ??= end - start + 1;\n    lastBit = end;\n    log.debug(`Packet block ${start} - ${lastBit} with label ${label}`);\n\n    while (word.length <= bitsPerRow + 1 && db.getPacket().length < maxPacketSize) {\n      const [block, nextBlock] = getNextFittingBlock({ start, end, bits, label }, row, bitsPerRow);\n      word.push(block);\n      if (block.end + 1 === row * bitsPerRow) {\n        db.pushWord(word);\n        word = [];\n        row++;\n      }\n      if (!nextBlock) {\n        break;\n      }\n      ({ start, end, bits, label } = nextBlock);","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/packet/parser.ts#L13-L49","documentation":"Thrown by the packet populate loop when a block's bits field is exactly 0. A zero-width field has no bits to render and is invalid; the check runs after start/end contiguity is verified, so it specifically catches an explicit bits: 0 (or a computed zero via end-start+1 when end<start after the earlier guard, though that path is already rejected).","triggerScenarios":"Declaring a packet block with bits: 0; writing a range where the computed width collapses to zero.","commonSituations":"Placeholder/reserved field left as zero by mistake; templating system emitting bits:0 for optional fields; misunderstanding that every block must consume at least one bit.","solutions":["Set bits to at least 1, or remove the block entirely if it represents nothing.","If you need a visual spacer, use a small non-zero reserved block instead.","Check any code generator that emits packet syntax and ensure it never produces bits: 0."],"exampleFix":"// before\npacket-beta\n  0-7: Field A\n  8-7: Zero width   // bits computes to 0\n\n// after\npacket-beta\n  0-7: Field A\n  8: Single bit field","handlingStrategy":"validation","validationCode":"function validateNoZeroBits(blocks: {start?:number;end?:number;bits?:number}[]): string[] {\n  const errors: string[] = [];\n  for (const b of blocks) {\n    if (b.bits === 0) errors.push(`Block at ${b.start ?? '?'} has zero bits`);\n  }\n  return errors;\n}","typeGuard":"function hasPositiveBits(bits: number | undefined): boolean {\n  return bits === undefined || bits > 0;\n}","tryCatchPattern":"try {\n  await mermaid.render('g', diagramText);\n} catch (e) {\n  if (e instanceof Error && /Cannot have a zero bit field/.test(e.message)) {\n    showUserError('A packet block declares zero bits. Set bits >= 1 or remove the block.');\n  } else {\n    throw e;\n  }\n}","preventionTips":["Never set bits to 0; every block must consume at least one bit.","Remove placeholder blocks rather than zeroing them.","Audit generated packet syntax for zero-width fields."],"tags":["mermaid","packet","parsing","validation"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}