{"record":{"id":"c4283d83bc39091d","repo":"TheAlgorithms/Go","slug":"submatrix-dimensions-exceed-matrix-bounds","errorCode":null,"errorMessage":"submatrix dimensions exceed matrix bounds","messagePattern":"submatrix dimensions exceed matrix bounds","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/submatrix.go","lineNumber":16,"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)\n\n\tfor i := 0; i < numRows; i++ {\n\t\ti := i // Capture the loop variable for the goroutine\n\t\twg.Add(1)","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/submatrix.go#L1-L34","documentation":"SubMatrix validates that rowStart+numRows and colStart+numCols fit within the matrix; the requested window extends past the matrix's rows or columns, so extraction is refused rather than returning a clipped matrix.","triggerScenarios":"Calling m.SubMatrix(1, 1, 3, 3) on a 3x3 matrix (needs indices up to 3, valid max 2); window size larger than the matrix; start offset plus size overflowing the edge.","commonSituations":"Tiling/chunking loops where the last tile is clipped but the code requests a full tile; confusing submatrix size with end coordinate (passing an exclusive end as a count); Strassen-style partitioning on non-power-of-two sizes.","solutions":["Clamp: numRows = min(numRows, m.Rows()-rowStart), numCols likewise, before calling.","Validate rowStart+numRows <= m.Rows() and colStart+numCols <= m.Columns() first.","Fix off-by-one where an end index was passed instead of a length (use end-start)."],"exampleFix":"// before\nsub, err := m.SubMatrix(1, 1, 3, 3) // 3x3 matrix: out of bounds\n\n// after\nsub, err := m.SubMatrix(1, 1, m.Rows()-1, m.Columns()-1) // fits","handlingStrategy":"validation","validationCode":"if rowStart+numRows > m.Rows() {\n    numRows = m.Rows() - rowStart\n}\nif colStart+numCols > m.Columns() {\n    numCols = m.Columns() - colStart\n}\nif numRows > 0 && numCols > 0 {\n    sub, err := m.SubMatrix(rowStart, colStart, numRows, numCols)\n}","typeGuard":"func fitsIn[T constraints.Integer](m math.Matrix[T], rowStart, colStart, numRows, numCols int) bool {\n    return rowStart+numRows <= m.Rows() && colStart+numCols <= m.Columns()\n}","tryCatchPattern":"sub, err := m.SubMatrix(rs, cs, nr, nc)\nif err != nil {\n    return Matrix{}, fmt.Errorf(\"submatrix exceeds %dx%d bounds: %w\", m.Rows(), m.Columns(), err)\n}","preventionTips":["In tiling loops, clip the last tile to the remaining rows/cols.","Never pass an end index where a length is expected (use end-start).","Test partitioning logic on non-power-of-two sizes."],"tags":["matrix","submatrix","out-of-bounds","go"],"backgroundTag":"index-out-of-range","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}