Skip to content

Commit 82ff5e7

Browse files
committed
[GR-13524] Fix access to uninitialized slot.
PullRequest: graalpython/379
2 parents e2bf75c + 3704992 commit 82ff5e7

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
def test_pathlib():
41+
from pathlib import Path
42+
p = Path("/usr")
43+
assert str(p) == "/usr"
44+
assert str(p.absolute()) == "/usr"
45+
assert str(p / "local") == "/usr/local"
46+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import unittest
41+
42+
43+
class A:
44+
__slots__ = ("hello", "world")
45+
46+
def __init__(self):
47+
self.hello = "hello"
48+
49+
50+
class TestSlots(unittest.TestCase):
51+
def test_uninitialized_slot(self):
52+
obj = A()
53+
self.assertEqual(obj.hello, "hello")
54+
with self.assertRaises(AttributeError):
55+
obj.world
56+
obj.world = "world"
57+
self.assertEqual(obj.world, "world")
58+
59+
60+
if __name__ == "__main__":
61+
unittest.main()

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/GetSetDescriptorTypeBuiltins.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.builtins.CoreFunctions;
5353
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5454
import com.oracle.graal.python.builtins.PythonBuiltins;
55+
import com.oracle.graal.python.builtins.objects.PNone;
5556
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
5657
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5758
import com.oracle.graal.python.nodes.PGuards;
@@ -144,11 +145,16 @@ Object get(GetSetDescriptor descr, Object obj, PythonClass type,
144145

145146
@Specialization
146147
Object getSlot(HiddenKeyDescriptor descr, Object obj, PythonClass type,
147-
@Cached("create()") ReadAttributeFromObjectNode readNode) {
148+
@Cached("create()") ReadAttributeFromObjectNode readNode,
149+
@Cached("createBinaryProfile()") ConditionProfile profile) {
148150
if (descr_check(descr.getType(), descr.getKey().getName(), obj, type)) {
149151
return descr;
150152
}
151-
return readNode.execute(obj, descr.getKey());
153+
Object val = readNode.execute(obj, descr.getKey());
154+
if (profile.profile(val != PNone.NO_VALUE)) {
155+
return val;
156+
}
157+
throw raise(AttributeError, descr.getKey().getName());
152158
}
153159
}
154160

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/MethodBuiltins.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -30,6 +30,7 @@
3030
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DEFAULTS__;
3131
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FUNC__;
3232
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__KWDEFAULTS__;
33+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
3334
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__;
3435
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
3536
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -47,6 +48,7 @@
4748
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
4849
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4950
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
51+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
5052
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5153
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
5254
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -133,4 +135,16 @@ Object doGeneric(@SuppressWarnings("unused") Object obj) {
133135
throw raise(TypeError, "can't pickle function objects");
134136
}
135137
}
138+
139+
@Builtin(name = __GET__, fixedNumOfPositionalArgs = 1)
140+
@GenerateNodeFactory
141+
public abstract static class GetNode extends PythonTernaryBuiltinNode {
142+
@Specialization
143+
PMethod doGeneric(@SuppressWarnings("unused") PMethod self, Object obj, @SuppressWarnings("unused") Object cls) {
144+
if (self.getSelf() != null) {
145+
return self;
146+
}
147+
return factory().createMethod(obj, self.getFunction());
148+
}
149+
}
136150
}

0 commit comments

Comments
 (0)