Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,23 @@ std::istream &ExplicitDataElement::ReadValue(std::istream &is, bool readvalues)
{
//gdcm_assert( TagField != Tag(0x7fe0,0x0010) );
ValueField = new ByteValue;
if( readvalues )
{
const std::streampos cur = is.tellg();
if( cur != std::streampos(-1) )
{
is.seekg(0, std::ios::end);
const std::streampos end = is.tellg();
is.seekg(cur);
if( end != std::streampos(-1) && is.good()
&& static_cast<uint64_t>(end - cur) < static_cast<uint32_t>(ValueLengthField) )
{
gdcmWarningMacro( "Value Length " << ValueLengthField
<< " exceeds remaining stream size for tag " << TagField );
throw Exception( "Value Length exceeds remaining stream size" );
}
}
}
}
// We have the length we should be able to read the value
this->SetValueFieldLength( ValueLengthField, readvalues );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ class GDCM_EXPORT Fragment : public DataElement
{
// Self
SmartPointer<ByteValue> bv = new ByteValue;
const std::streampos cur = is.tellg();
if( cur != std::streampos(-1) )
{
is.seekg(0, std::ios::end);
const std::streampos end = is.tellg();
is.seekg(cur);
if( end != std::streampos(-1) && is.good()
&& static_cast<uint64_t>(end - cur) < static_cast<uint32_t>(ValueLengthField) )
{
throw Exception( "Fragment Value Length exceeds remaining stream size" );
}
}
bv->SetLength(ValueLengthField);
if( !bv->Read<TSwap>(is) )
{
Expand Down Expand Up @@ -144,6 +156,18 @@ class GDCM_EXPORT Fragment : public DataElement

// Self
SmartPointer<ByteValue> bv = new ByteValue;
const std::streampos cur2 = is.tellg();
if( cur2 != std::streampos(-1) )
{
is.seekg(0, std::ios::end);
const std::streampos end2 = is.tellg();
is.seekg(cur2);
if( end2 != std::streampos(-1) && is.good()
&& static_cast<uint64_t>(end2 - cur2) < static_cast<uint32_t>(ValueLengthField) )
{
throw Exception( "Fragment Value Length exceeds remaining stream size" );
}
}
bv->SetLength(ValueLengthField);
if( !bv->Read<TSwap>(is) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ std::istream &ImplicitDataElement::ReadValue(std::istream &is, bool readvalues)
ValueLengthField = 202; // 0xca
}
#endif
if( !ValueLengthField.IsUndefined() && readvalues )
{
const std::streampos cur = is.tellg();
if( cur != std::streampos(-1) )
{
is.seekg(0, std::ios::end);
const std::streampos end = is.tellg();
is.seekg(cur);
if( end != std::streampos(-1) && is.good()
&& static_cast<uint64_t>(end - cur) < static_cast<uint32_t>(ValueLengthField) )
{
gdcmWarningMacro( "Value Length " << ValueLengthField
<< " exceeds remaining stream size for tag " << TagField );
throw Exception( "Value Length exceeds remaining stream size" );
}
}
}
// We have the length we should be able to read the value
this->SetValueFieldLength( ValueLengthField, readvalues );
bool failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ std::istream& ReadValue(std::istream &is, bool /*readvalues*/)
{
gdcm_assert( Fragments.size() == 1 );
const ByteValue *bv = Fragments[0].GetByteValue();
gdcm_assert( (unsigned char)bv->GetPointer()[ bv->GetLength() - 1 ] == 0xfe );
gdcm_assert( bv->GetLength() >= 1 && (unsigned char)bv->GetPointer()[ bv->GetLength() - 1 ] == 0xfe );
// Yes this is an extra copy, this is a bug anyway, go fix YOUR code
Fragments[0].SetByteValue( bv->GetPointer(), bv->GetLength() - 1 );
gdcmWarningMacro( "JPEG Fragment length was declared with an extra byte"
Expand All @@ -188,7 +188,7 @@ std::istream& ReadValue(std::istream &is, bool /*readvalues*/)
const size_t lastf = Fragments.size() - 1;
const ByteValue *bv = Fragments[ lastf ].GetByteValue();
const char *a = bv->GetPointer();
gdcmAssertAlwaysMacro( (unsigned char)a[ bv->GetLength() - 1 ] == 0xfe );
gdcmAssertAlwaysMacro( bv->GetLength() >= 1 && (unsigned char)a[ bv->GetLength() - 1 ] == 0xfe );
Fragments[ lastf ].SetByteValue( bv->GetPointer(), bv->GetLength() - 1 );
is.seekg( -9, std::ios::cur );
gdcm_assert( is.good() );
Expand All @@ -212,7 +212,7 @@ std::istream& ReadValue(std::istream &is, bool /*readvalues*/)
const size_t lastf = Fragments.size() - 1;
const ByteValue *bv = Fragments[ lastf ].GetByteValue();
const char *a = bv->GetPointer();
gdcmAssertAlwaysMacro( (unsigned char)a[ bv->GetLength() - 2 ] == 0xfe );
gdcmAssertAlwaysMacro( bv->GetLength() >= 2 && (unsigned char)a[ bv->GetLength() - 2 ] == 0xfe );
Fragments[ lastf ].SetByteValue( bv->GetPointer(), bv->GetLength() - 2 );
is.seekg( -10, std::ios::cur );
gdcm_assert( is.good() );
Expand Down
Loading