Reject a blank row in the gencost matrix instead of reading past the string - #580
Closed
arpitjain099 wants to merge 1 commit into
Closed
arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
…string readMatPowerGenCostRow checks the last character with row[row.size() - 1] after rtrim. On a blank line inside mpc.gencost that subscript is row[std::string::npos], since size() is 0 and the expression is unsigned. The read is reachable from readMatPower: the gencost loop passes every line up to "];" straight to the row reader, and nothing filters empty ones. Compiled with -fsanitize=address, a case file with one blank line between two gencost rows gives: ERROR: AddressSanitizer: stack-buffer-underflow #0 readMatPowerGenCostRow MatpowerParser.hpp:156 ORNL#1 readMatPower MatpowerParser.hpp:282 while the same file without the blank line parses both rows. Added the empty check to the condition that was already there, so a blank row now raises the same matlab_syntax_error any other malformed row does, and added the case to test_parse_gencost_row. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Collaborator
|
@arpitjain099, thanks for your contribution. Please create an issue with reproducer and suggested solution. Then we can decide how to move forward. I will close this PR until then. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
readMatPowerGenCostRowchecks the trailing semicolon withrow[row.size() - 1]afterrtrim. On a blank line the string is empty,size() - 1wraps, and the subscript reads past the buffer. The gencost loop inreadMatPowerhands every line up to];to the row reader without filtering empty ones, so a case file with a stray blank line between two gencost rows reaches it.Built with
-fsanitize=addressagainst the header as it stands:The file that triggers it is a normal case file, nothing malicious, just an extra newline in the matrix.
The condition already throws
matlab_syntax_errorfor a row without a trailing semicolon, so addingrow.empty()to it puts a blank row on that same path rather than inventing new behaviour. I added the case totest_parse_gencost_row, which aborts under ASan before the change and passes after it.One thing I noticed while checking the siblings, not changed here:
readMatPowerGenRowandreadMatPowerBranchRowboth end withcheckEndOfMatrixRow, so they reject a blank row, butreadMatPowerBusRowdoes not call it. A blank line inmpc.busis currently accepted and appends a bus row with every field zero. Happy to send that separately if you want it tightened up, since it changes what the bus parser accepts rather than fixing a read.