Skip to content

Commit

Permalink
Merge pull request #55 from equinor/too-large-padbytes
Browse files Browse the repository at this point in the history
Avoid crash on too large padbyte values
  • Loading branch information
jokva authored Mar 4, 2019
2 parents 1250731 + 39deb33 commit fa6d203
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,15 @@ record& stream::at( int i, record& rec ) noexcept (false) {

this->fs.seekg( tell );

const auto chop = []( std::vector< char >& vec, int bytes ) {
vec.erase( vec.end() - bytes, vec.end() );
const auto chop = [](std::vector< char >& vec, int bytes) {
const int size = vec.size();
const int new_size = (std::max)(0, size - bytes);

if (size - bytes < 0) {
// TODO: user-warning
// const auto msg = "at::chop() would remove more bytes than read";
}
vec.resize(new_size);
};

while (true) {
Expand Down
Binary file added python/data/padbytes-large-as-record.dlis
Binary file not shown.
10 changes: 10 additions & 0 deletions python/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,13 @@ def test_load_pre_vrl_garbage():
def test_load_file_with_broken_utf8():
with dlisio.load('data/broken-degree-symbol.dlis') as f:
pass

def test_padbytes_as_large_as_record():
# 180-byte long explicit record with padding, and padbytes are set to 180
# (leaving the resulting len(data) == 0)
f = dlisio.open('data/padbytes-large-as-record.dlis')
f.reindex([0], [180])

rec = f.extract([0])[0]
assert rec.explicit
assert len(memoryview(rec)) == 0

0 comments on commit fa6d203

Please sign in to comment.