{"record":{"id":"0bf808705a985cb0","repo":"d2lang/d2","slug":"padding-d-produces-invalid-svg-dimensions","errorCode":null,"errorMessage":"padding %d produces invalid SVG dimensions","messagePattern":"padding (.+?) produces invalid SVG dimensions","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2renderers/d2svg/d2svg.go","lineNumber":105,"sourceCode":"\tCenter             *bool\n\tThemeID            *int64\n\tDarkThemeID        *int64\n\tThemeOverrides     *d2target.ThemeOverrides\n\tDarkThemeOverrides *d2target.ThemeOverrides\n\tFont               string\n\t// the svg will be scaled by this factor, if unset the svg will fit to screen\n\tScale *float64\n\n\t// MasterID is passed when the diagram should use something other than its own hash for unique targeting\n\t// Currently, that's when multi-boards are collapsed\n\tMasterID    string\n\tNoXMLTag    *bool\n\tSalt        *string\n\tOmitVersion *bool\n}\n\nfunc invalidPaddingError(pad int64) error {\n\treturn fmt.Errorf(\"padding %d produces invalid SVG dimensions\", pad)\n}\n\nfunc checkedIntAdd(a, b, minInt, maxInt int64) (int64, bool) {\n\tif b > 0 && a > maxInt-b || b < 0 && a < minInt-b {\n\t\treturn 0, false\n\t}\n\treturn a + b, true\n}\n\nfunc checkedIntSub(a, b, minInt, maxInt int64) (int64, bool) {\n\tif b > 0 && a < minInt+b || b < 0 && a > maxInt+b {\n\t\treturn 0, false\n\t}\n\treturn a - b, true\n}\n\nfunc validatePadding(tl, br d2target.Point, pad int64) (int, error) {\n\tmaxInt := int64(^uint(0) >> 1)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2renderers/d2svg/d2svg.go#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a sane padding value (e.g. 0 to a few hundred pixels)","Validate the pad value before calling Render: it must be non-negative and small enough that pad + diagram dimensions stay within int64 and positive","Clamp or reject user-supplied pad values at the config boundary before rendering"],"exampleFix":"// before\npad, _ := strconv.ParseInt(userInput, 10, 64)\nsvg, _ := d2svg.Render(diagram, &opts{Pad: pad})\n// after\nif pad < 0 || pad > 10000 { pad = 0 }\nsvg, _ := d2svg.Render(diagram, &opts{Pad: pad})","handlingStrategy":"validation","validationCode":"if pad < 0 || pad > 1_000_000 {\n\treturn errors.New(\"padding out of safe range\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp user/config-supplied pad values before rendering","Never pass raw parsed int64 input straight to Render","Default pad to a small sane value (e.g. 100)"],"tags":["svg","rendering","validation"],"backgroundTag":"invalid-svg-dimensions","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}