Skip to content

Commit 8ed65b9

Browse files
addaleaxCommit bot
authored andcommitted
Make FieldType::None() non-nullptr value to avoid undefined behaviour
When FieldType::None() returns a cast Smi::FromInt(0), which translates as nullptr, the FieldType::IsNone() check becomes equivalent to `this == nullptr` which is not allowed by the standard and therefore optimized away as a false constant by GCC 6. This has lead to crashes when invoking methods on FieldType::None(). Using a different Smi constant for FieldType::None() makes the compiler always include a comparison against that value. The choice of these constants has no effect as they are effectively arbitrary. BUG=nodejs/node#8310 Review-Url: https://codereview.chromium.org/2292953002 Cr-Commit-Position: refs/heads/master@{#39023}
1 parent 9f747be commit 8ed65b9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/field-type.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace internal {
1313

1414
// static
1515
FieldType* FieldType::None() {
16-
return reinterpret_cast<FieldType*>(Smi::FromInt(0));
16+
// Do not Smi::FromInt(0) here or for Any(), as that may translate
17+
// as `nullptr` which is not a valid value for `this`.
18+
return reinterpret_cast<FieldType*>(Smi::FromInt(2));
1719
}
1820

1921
// static

test/cctest/test-field-type-tracking.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/global-handles.h"
1818
#include "src/ic/stub-cache.h"
1919
#include "src/macro-assembler.h"
20+
#include "src/types.h"
2021

2122
using namespace v8::internal;
2223

@@ -2410,6 +2411,16 @@ TEST(TransitionAccessorConstantToSameAccessorConstant) {
24102411
TestTransitionTo(transition_op, transition_op, checker);
24112412
}
24122413

2414+
TEST(FieldTypeConvertSimple) {
2415+
CcTest::InitializeVM();
2416+
v8::HandleScope scope(CcTest::isolate());
2417+
Isolate* isolate = CcTest::i_isolate();
2418+
2419+
Zone zone(isolate->allocator());
2420+
2421+
CHECK_EQ(FieldType::Any()->Convert(&zone), Type::NonInternal());
2422+
CHECK_EQ(FieldType::None()->Convert(&zone), Type::None());
2423+
}
24132424

24142425
// TODO(ishell): add this test once IS_ACCESSOR_FIELD_SUPPORTED is supported.
24152426
// TEST(TransitionAccessorConstantToAnotherAccessorConstant)

0 commit comments

Comments
 (0)