The getEntries() method in the Matrix class currently returns a shallow copy of the internal ENTRIES array. This means that any modifications made to the returned array will directly affect the original data within the matrix object, potentially leading to unintended side effects and data corruption.
Reproduction Steps
Create a Matrix object.
Call getEntries() to obtain a reference to the array.
Modify an element within the returned array.
Observe that the corresponding element in the original matrix object has also been modified.
// Create a new matrix object
Matrix m = new Matrix(new double[][] {
{ 12, 12, 12 },
{ 13, 13, 13 }
});
double[][] entries = m.getEntries(); // Retrieve the entries
for (int i = 0; i < entries[0].length; i++) {
// Modify the first row entries to 5.0 for each
entries[0][i] = 5.0;
}
// Display and compare both objects
m.display();
Matrix.display(entries);
Description
The
getEntries()
method in the Matrix class currently returns a shallow copy of the internalENTRIES
array. This means that any modifications made to the returned array will directly affect the original data within the matrix object, potentially leading to unintended side effects and data corruption.Reproduction Steps
Matrix
object.getEntries()
to obtain a reference to the array.Output:
Expected Behavior
The
getEntries()
method should either:Severity
Medium to high, depending on the potential impact of unintended modifications to matrix data in the application's context.
Solutions
ENTRIES
.ENTRIES
and copy each element individually to the new array.