Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 24, 2024
1 parent 0edfe8d commit 1036a6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 13 additions & 1 deletion cpp/devices/tape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ int Tape::ReadData(span<uint8_t> buf)

const int length = tar_mode ? GetBlockSize() : GetVariableBlockSize();

if (static_cast<off_t>(position + length) > GetFileSize()) {
throw scsi_exception(sense_key::blank_check);
}

file.seekg(position, ios::beg);
file.read((char*)buf.data(), length);
if (file.fail()) {
Expand All @@ -198,6 +202,10 @@ int Tape::WriteData(span<const uint8_t> buf, scsi_command)

const int length = GetController()->GetChunkSize();

if (static_cast<off_t>(position + length) > GetFileSize()) {
throw scsi_exception(sense_key::blank_check);
}

if (!tar_mode) {
WriteMetaData(object_type::BLOCK, length);
}
Expand Down Expand Up @@ -551,6 +559,10 @@ void Tape::WriteMetaData(Tape::object_type type, uint32_t size)
{
assert(size < 65536);

if (static_cast<off_t>(position + sizeof(meta_data_t)) > GetFileSize()) {
throw scsi_exception(sense_key::blank_check);
}

meta_data_t meta_data;
memcpy(meta_data.magic.data(), MAGIC, 4);
meta_data.type = type;
Expand Down Expand Up @@ -644,7 +656,7 @@ uint32_t Tape::GetByteCount() const
GetSignedInt24(GetController()->GetCdb(), 2);

// The non-fixed block size must be a multiple of 4 (see SSC-5)
if ((!fixed && count % 4) || static_cast<off_t>(position + count) > filesize) {
if (!fixed && count % 4) {
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_cdb);
}

Expand Down
11 changes: 6 additions & 5 deletions cpp/test/tape_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,12 @@ TEST(TapeTest, Erase6)
{
auto [controller, tape] = CreateTape();

CreateTapeFile(*tape, 4567);

tape->SetProtected(true);
TestShared::Dispatch(*tape, scsi_command::erase6, sense_key::data_protect, asc::write_protected);

tape->SetProtected(false);
TestShared::Dispatch(*tape, scsi_command::erase6, sense_key::medium_error, asc::write_error);

CreateTapeFile(*tape, 4567);
EXPECT_NO_THROW(tape->Dispatch(scsi_command::erase6));
CheckPosition(*controller, *tape, 0);
EXPECT_EQ(0b10000000, controller->GetBuffer()[0]) << "EOP must be set";
Expand Down Expand Up @@ -275,12 +274,14 @@ TEST(TapeTest, WriteFileMarks6)
controller->SetCdbByte(1, 0b001);
EXPECT_NO_THROW(tape->Dispatch(scsi_command::write_filemarks6));

CreateTapeFile(*tape);

// Count > 0
controller->SetCdbByte(2, 1);
TestShared::Dispatch(*tape, scsi_command::write_filemarks6, sense_key::medium_error, asc::write_error);
TestShared::Dispatch(*tape, scsi_command::write_filemarks6, sense_key::blank_check,
asc::no_additional_sense_information);

tape->SetProtected(true);
controller->SetCdbByte(1, 0b001);
TestShared::Dispatch(*tape, scsi_command::write_filemarks6, sense_key::data_protect, asc::write_protected);
}

Expand Down

0 comments on commit 1036a6c

Please sign in to comment.