{"record":{"id":"9c2c4328f4d4c44d","repo":"TheAlgorithms/Go","slug":"negative-dimensions-are-not-allowed","errorCode":null,"errorMessage":"negative dimensions are not allowed","messagePattern":"negative dimensions are not allowed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/submatrix.go","lineNumber":12,"sourceCode":"package matrix\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"sync\"\n)\n\n// SubMatrix extracts a submatrix from the current matrix.\nfunc (m Matrix[T]) SubMatrix(rowStart, colStart, numRows, numCols int) (Matrix[T], error) {\n\tif rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {\n\t\treturn Matrix[T]{}, errors.New(\"negative dimensions are not allowed\")\n\t}\n\n\tif rowStart+numRows > m.rows || colStart+numCols > m.columns {\n\t\treturn Matrix[T]{}, errors.New(\"submatrix dimensions exceed matrix bounds\")\n\t}\n\n\tvar zeroVal T\n\tif numRows == 0 || numCols == 0 {\n\t\treturn New(numRows, numCols, zeroVal), nil // Return an empty matrix\n\t}\n\n\tsubMatrix := New(numRows, numCols, zeroVal)\n\n\tctx, cancel := context.WithCancel(context.Background())\n\tdefer cancel() // Make sure it's called to release resources even if no errors\n\n\tvar wg sync.WaitGroup\n\terrCh := make(chan error, 1)","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/submatrix.go#L1-L30","documentation":"SubMatrix returns this error when any of rowStart, colStart, numRows, numCols is negative. Negative coordinates or sizes are meaningless for an extraction window, so the call is rejected before any bounds math runs.","triggerScenarios":"Calling m.SubMatrix with a negative argument, e.g. SubMatrix(-1, 0, 2, 2) or SubMatrix(0, 0, -3, 3); computed offsets that underflow (start - padding < 0).","commonSituations":"Subtracting a padding/margin from an index without clamping to zero; loop variables that go negative near boundaries; swapped sign in a computed offset.","solutions":["Clamp offsets: if rowStart < 0 { rowStart = 0 } (same for colStart) and ensure numRows/numCols >= 0.","Validate all four arguments are >= 0 before calling SubMatrix.","Fix the computation that produced the negative value (order of operands in subtraction)."],"exampleFix":"// before\nsub, err := m.SubMatrix(i-pad, j-pad, h, w) // error when i < pad\n\n// after\nrs, cs := max(0, i-pad), max(0, j-pad)\nsub, err := m.SubMatrix(rs, cs, h, w)","handlingStrategy":"validation","validationCode":"if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {\n    rowStart, colStart = max(rowStart, 0), max(colStart, 0)\n    numRows, numCols = max(numRows, 0), max(numCols, 0)\n}\nsub, err := m.SubMatrix(rowStart, colStart, numRows, numCols)","typeGuard":null,"tryCatchPattern":"sub, err := m.SubMatrix(rs, cs, nr, nc)\nif err != nil {\n    return Matrix{}, fmt.Errorf(\"submatrix(%d,%d,%d,%d): %w\", rs, cs, nr, nc, err)\n}","preventionTips":["Clamp computed offsets with max(0, x) before slicing.","Keep padding arithmetic explicit and tested near boundaries.","Distinguish start offsets from sizes in variable names."],"tags":["matrix","submatrix","negative-index","go"],"backgroundTag":"negative-index-parameter","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}