Skip to content

Commit

Permalink
COMMON: Reduce maximum Span size to 4GiB
Browse files Browse the repository at this point in the history
Until C++11 (which introduces the z and t length modifiers), there
is no consistent way to print size_t and ptrdiff_t types using
printf formatting across 32-bit, LLP64, and LP64 architectures
without using a cumbersome macro to select the appropriate length
modifier for the target architecture. Since ScummVM engines
currently need to support 32-bit targets, there is no reason at
the moment to support any larger memory sizes in Span anyway.

Span error output is also updated in this commit to reflect that
index values are unsigned.
  • Loading branch information
csnover committed Mar 30, 2017
1 parent 09da421 commit 993d83f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions common/span.h
Expand Up @@ -268,9 +268,9 @@ class SpanBase : public SafeBool<Derived<ValueType> > {

public:
typedef ValueType value_type;
typedef ptrdiff_t difference_type;
typedef size_t index_type;
typedef size_t size_type;
typedef int32 difference_type;
typedef uint32 index_type;
typedef uint32 size_type;
typedef SpanInternal::SpanIterator<derived_type, true> const_iterator;
typedef SpanInternal::SpanIterator<derived_type, false> iterator;
typedef value_type *pointer;
Expand Down Expand Up @@ -601,11 +601,11 @@ class SpanImpl : public SpanBase<ValueType, Derived> {
break;
}

return String::format("Access violation %s %s: %ld + %ld > %ld",
return String::format("Access violation %s %s: %u + %d > %u",
modeName,
this->impl().name().c_str(),
index,
deltaInBytes / sizeof(value_type),
deltaInBytes / (int)sizeof(value_type),
size());
}

Expand Down Expand Up @@ -829,7 +829,7 @@ class NamedSpanImpl : public SpanImpl<ValueType, Derived> {
const size_type maxSizeInBytes = this->impl().byteSize();

return super_type::getValidationMessage(index, deltaInBytes, mode) +
String::format(" (abs: %ld + %ld > %ld)",
String::format(" (abs: %u + %d > %u)",
this->impl().sourceByteOffset() + indexInBytes,
deltaInBytes,
this->impl().sourceByteOffset() + maxSizeInBytes);
Expand Down
26 changes: 13 additions & 13 deletions test/common/span.h
Expand Up @@ -640,7 +640,7 @@ class SpanTestSuite : public CxxTest::TestSuite {
TS_ASSERT(span.checkInvalidBounds(2, -4)); // negative overflow (-2)
TS_ASSERT(span.checkInvalidBounds(0, 10)); // delta positive overflow

const ptrdiff_t big = 1L << (8 * sizeof(ptrdiff_t) - 1);
const Common::Span<byte>::difference_type big = 1L << (8 * sizeof(Common::Span<byte>::difference_type) - 1);
TS_ASSERT(span.checkInvalidBounds(big, 0));
TS_ASSERT(span.checkInvalidBounds(0, big));
TS_ASSERT(span.checkInvalidBounds(big, big));
Expand All @@ -662,8 +662,8 @@ class SpanTestSuite : public CxxTest::TestSuite {
expected = Common::String::format("Access violation writing %s: 23 + 45 > 1", source.c_str());
TS_ASSERT_EQUALS(actual, expected);

actual = span.getValidationMessage(-34, -56, Common::kValidateSeek);
expected = Common::String::format("Access violation seeking %s: -34 + -56 > 1", source.c_str());
actual = span.getValidationMessage(0, -56, Common::kValidateSeek);
expected = Common::String::format("Access violation seeking %s: 0 + -56 > 1", source.c_str());
TS_ASSERT_EQUALS(actual, expected);
}

Expand Down Expand Up @@ -716,15 +716,15 @@ class SpanTestSuite : public CxxTest::TestSuite {

{
Common::NamedSpan<byte> subspan = span.subspan(2, Common::kSpanMaxSize, "new.data");
expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
expected = "Access violation reading new.data: 0 + -56 > 4 (abs: 2 + -56 > 6)";
actual = subspan.getValidationMessage(0, -56, Common::kValidateRead);
TS_ASSERT_EQUALS(actual, expected);
}

{
Common::NamedSpan<byte> subspan = span.subspan(2, Common::kSpanMaxSize, "new.data", 0);
expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -34 + -56 > 4)";
actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
expected = "Access violation reading new.data: 0 + -56 > 4 (abs: 0 + -56 > 4)";
actual = subspan.getValidationMessage(0, -56, Common::kValidateRead);
TS_ASSERT_EQUALS(actual, expected);
}

Expand Down Expand Up @@ -754,23 +754,23 @@ class SpanTestSuite : public CxxTest::TestSuite {

{
Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data");
expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
expected = "Access violation reading new.data: 0 + -56 > 4 (abs: 2 + -56 > 6)";
actual = subspan.getValidationMessage(0, -56, Common::kValidateRead);
TS_ASSERT_EQUALS(actual, expected);
}

{
Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data", 0);
expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -34 + -56 > 4)";
actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
expected = "Access violation reading new.data: 0 + -56 > 4 (abs: 0 + -56 > 4)";
actual = subspan.getValidationMessage(0, -56, Common::kValidateRead);
TS_ASSERT_EQUALS(actual, expected);
}

{
Common::NamedSpan<const byte> subspan = constSpan.subspan(2, Common::kSpanMaxSize, "new.data", 0);
subspan.sourceByteOffset() = 2;
expected = "Access violation reading new.data: -34 + -56 > 4 (abs: -32 + -56 > 6)";
actual = subspan.getValidationMessage(-34, -56, Common::kValidateRead);
expected = "Access violation reading new.data: 0 + -56 > 4 (abs: 2 + -56 > 6)";
actual = subspan.getValidationMessage(0, -56, Common::kValidateRead);
TS_ASSERT_EQUALS(actual, expected);
}

Expand Down

0 comments on commit 993d83f

Please sign in to comment.