d2lang/d2 · error

padding %d produces invalid SVG dimensions

Error message

padding %d produces invalid SVG dimensions

What it means

The SVG renderer validates the requested padding because padding is added to the computed diagram dimensions. If padding would overflow int64 bounds (or otherwise produce non-positive/invalid SVG width/height), invalidPaddingError is raised instead of emitting a corrupt SVG.

Source

Thrown at d2renderers/d2svg/d2svg.go:105

	Center             *bool
	ThemeID            *int64
	DarkThemeID        *int64
	ThemeOverrides     *d2target.ThemeOverrides
	DarkThemeOverrides *d2target.ThemeOverrides
	Font               string
	// the svg will be scaled by this factor, if unset the svg will fit to screen
	Scale *float64

	// MasterID is passed when the diagram should use something other than its own hash for unique targeting
	// Currently, that's when multi-boards are collapsed
	MasterID    string
	NoXMLTag    *bool
	Salt        *string
	OmitVersion *bool
}

func invalidPaddingError(pad int64) error {
	return fmt.Errorf("padding %d produces invalid SVG dimensions", pad)
}

func checkedIntAdd(a, b, minInt, maxInt int64) (int64, bool) {
	if b > 0 && a > maxInt-b || b < 0 && a < minInt-b {
		return 0, false
	}
	return a + b, true
}

func checkedIntSub(a, b, minInt, maxInt int64) (int64, bool) {
	if b > 0 && a < minInt+b || b < 0 && a > maxInt+b {
		return 0, false
	}
	return a - b, true
}

func validatePadding(tl, br d2target.Point, pad int64) (int, error) {
	maxInt := int64(^uint(0) >> 1)

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Use a sane padding value (e.g. 0 to a few hundred pixels)
  2. Validate the pad value before calling Render: it must be non-negative and small enough that pad + diagram dimensions stay within int64 and positive
  3. Clamp or reject user-supplied pad values at the config boundary before rendering

Example fix

// before
pad, _ := strconv.ParseInt(userInput, 10, 64)
svg, _ := d2svg.Render(diagram, &opts{Pad: pad})
// after
if pad < 0 || pad > 10000 { pad = 0 }
svg, _ := d2svg.Render(diagram, &opts{Pad: pad})
Defensive patterns

Strategy: validation

Validate before calling

if pad < 0 || pad > 1_000_000 {
	return errors.New("padding out of safe range")
}

Prevention

When it happens

Trigger: Calling Render (e.g. d2svg.Render or the CLI --pad flag) with an extreme padding value such as a huge number near math.MaxInt64 or a negative value that underflows the dimensions.

Common situations: Scripting the CLI with --pad taken from unvalidated user input or a misparsed config value; passing int64 constants directly to the render API when testing.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/0bf808705a985cb0. Report an issue: GitHub.