Skip to content

Commit 41837f5

Browse files
linusgawesomekling
authored andcommitted
LibJS: Don't create "valid" PropertyName from null string
When value.to_string() throws an exception it returns a null string in which case we must not construct a valid PropertyName. Also ASSERT in PropertyName(String) and PropertyName(FlyString) to prevent this from happening in the future. Fixes #3941.
1 parent 8afe1c8 commit 41837f5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Libraries/LibJS/Runtime/PropertyName.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ class PropertyName {
4848
return &value.as_symbol();
4949
if (value.is_integer() && value.as_i32() >= 0)
5050
return value.as_i32();
51-
return value.to_string(global_object);
51+
auto string = value.to_string(global_object);
52+
if (string.is_null())
53+
return {};
54+
return string;
5255
}
5356

5457
PropertyName() { }
@@ -70,18 +73,21 @@ class PropertyName {
7073
: m_type(Type::String)
7174
, m_string(FlyString(string))
7275
{
76+
ASSERT(!string.is_null());
7377
}
7478

7579
PropertyName(const FlyString& string)
7680
: m_type(Type::String)
7781
, m_string(string)
7882
{
83+
ASSERT(!string.is_null());
7984
}
8085

8186
PropertyName(Symbol* symbol)
8287
: m_type(Type::Symbol)
8388
, m_symbol(symbol)
8489
{
90+
ASSERT(symbol);
8591
}
8692

8793
PropertyName(const StringOrSymbol& string_or_symbol)

Libraries/LibJS/Tests/computed-property-throws.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@ test("Issue #3459, exception in computed property expression", () => {
66
"foo"[bar]();
77
}).toThrow(ReferenceError);
88
});
9+
10+
test("Issue #3941, exception in computed property's toString()", () => {
11+
expect(() => {
12+
const o = {
13+
toString() {
14+
throw Error();
15+
},
16+
};
17+
"foo"[o];
18+
}).toThrow(Error);
19+
});

0 commit comments

Comments
 (0)