Repository navigation
Stop reading values once the declared count is reached - #473
arpitjain099 wants to merge 3 commits into
Conversation
createArrayFromFile and createVectorFromFile size the allocation from the Matrix Market header and then read to end of file, so a body longer than the header declares is written past the end. updateVectorFromFile compares the declared length against the vector size and then does the same, so a header that matches exactly plus extra values passes the check and still overflows. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
The fourth reader in this file has the same unbounded loop and carries a todo from the maintainers saying the risk of writing past the end is high. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
Correction to my own description: when I opened this, |
|
@arpitjain099, thanks for your contribution. My suggestion would be:
|
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
Done, pushed as a third commit. All four readers now compare the number of entries read against the header and return without touching the caller's data when they disagree. Too few entries and too many are both rejected; the body goes into a temporary first, so a malformed file leaves the target vector alone rather than half written. Trailing blank lines come for free, since New test One thing I noticed but left alone: |
The Matrix Market readers in
resolve/matrix/io.cpptake the length from the header, allocate that much, and then read until end of file:A file whose header says 4 and whose body carries 12 values therefore writes past the end. Built with
-fsanitize=address:createVectorFromFileis the same shape.updateVectorFromFileis the interesting one: it does checkn != vec_rhs->getSize(), but that compares the declared length, so a header that matches exactly followed by 300 extra values passes the check and then overflows anyway.Each loop now stops at the declared count. Afterwards a well-formed file reads identically, the over-long one keeps the first four values, and the update case leaves the vector at its size with nothing reported by the sanitizer.
updateArrayFromFilehas the same shape and carries a@todofrom you saying the risk of writing past the end is high; it is bounded here too.