Skip to content

Commit baecb8c

Browse files
committed
[GR-76776] Fix GitHub fuzzing issues
PullRequest: graalpython/4673
2 parents a6b48c0 + 043381a commit baecb8c

8 files changed

Lines changed: 43 additions & 13 deletions

File tree

graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ def test_from_list():
7272
assert list(b) == ints
7373

7474

75+
def test_bytearray_from_none():
76+
assert_raises(TypeError, bytes, None)
77+
assert_raises(TypeError, bytearray, None)
78+
79+
80+
def test_bytes_with_encoding_does_not_call_dunder_bytes():
81+
class BytesLike:
82+
def __bytes__(self):
83+
return b"bytes"
84+
85+
assert bytes(BytesLike()) == b"bytes"
86+
assert_raises(TypeError, bytes, BytesLike(), None)
87+
assert_raises(TypeError, bytes, encoding="utf-8")
88+
assert_raises(TypeError, bytes, errors="strict")
89+
90+
7591
def test_from_ssize():
7692
assert bytes(0) == b''
7793
assert bytes(1) == b'\x00'

graalpython/com.oracle.graal.python.test/src/tests/test_object.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def set_dict_attr():
8787
assert_raises(AttributeError, set_dict_attr)
8888

8989

90+
def test_lookup_single_underscore_attr():
91+
class MyClass:
92+
pass
93+
94+
assert_raises(AttributeError, lambda: MyClass()._)
95+
96+
9097
def test_set_dict_attr():
9198
class MyClass(object):
9299
def __init__(self):

graalpython/com.oracle.graal.python.test/src/tests/test_super.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def m(self): return ["B"] + self.my_super.m()
4949
assert B().m() == ["B", "A"]
5050

5151

52+
def test_super_requires_type_arg():
53+
try:
54+
super([])
55+
except TypeError:
56+
pass
57+
else:
58+
assert False
59+
60+
5261
def test_super_subclass_descr_get_invokes_subclass_type():
5362
class MySuper(super):
5463
news = []

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2026, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -164,20 +164,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
164164
return ByteArrayBuiltinsClinicProviders.InitNodeClinicProviderGen.INSTANCE;
165165
}
166166

167-
@Specialization(guards = "!isNone(source)")
167+
@Specialization
168168
static PNone doInit(VirtualFrame frame, PByteArray self, Object source, Object encoding, Object errors,
169169
@Bind Node inliningTarget,
170170
@Cached BytesNodes.BytesInitNode toBytesNode) {
171171
self.setSequenceStorage(new ByteSequenceStorage(toBytesNode.execute(frame, inliningTarget, source, encoding, errors)));
172172
return PNone.NONE;
173173
}
174174

175-
@Specialization(guards = "isNone(self)")
176-
static PNone doInit(@SuppressWarnings("unused") PByteArray self, Object source, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
177-
@Bind Node inliningTarget) {
178-
throw PRaiseNode.raiseStatic(inliningTarget, TypeError, ErrorMessages.CANNOT_CONVERT_P_OBJ_TO_S, source, "bytearray");
179-
}
180-
181175
@Specialization(guards = "!isBytes(self)")
182176
static PNone doInit(Object self, @SuppressWarnings("unused") Object source, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
183177
@Bind Node inliningTarget) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
131131
}
132132

133133
@SuppressWarnings("unused")
134-
@Specialization(guards = "isNoValue(source)")
134+
@Specialization(guards = {"isNoValue(source)", "isNoValue(encoding)", "isNoValue(errors)"})
135135
static Object doEmpty(Object cls, PNone source, PNone encoding, PNone errors,
136136
@Bind Node inliningTarget,
137137
@Exclusive @Cached CreateBytes createBytes) {
138138
return createBytes.execute(inliningTarget, cls, PythonUtils.EMPTY_BYTE_ARRAY);
139139
}
140140

141-
@Specialization(guards = "!isNoValue(source)")
141+
@Specialization(guards = {"!isNoValue(source)", "isNoValue(encoding)", "isNoValue(errors)"})
142142
static Object doCallBytes(VirtualFrame frame, Object cls, Object source, PNone encoding, PNone errors,
143143
@Bind Node inliningTarget,
144144
@Cached GetClassNode getClassNode,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ public abstract static class BytesInitNode extends PNodeWithContext {
629629

630630
public abstract byte[] execute(VirtualFrame frame, Node inliningTarget, Object source, Object encoding, Object errors);
631631

632-
@Specialization
633-
static byte[] none(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
632+
@Specialization(guards = "isNoValue(source)")
633+
static byte[] noValue(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
634634
return PythonUtils.EMPTY_BYTE_ARRAY;
635635
}
636636

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/superobject/SuperBuiltins.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ public final Object execute(VirtualFrame frame, Object self, Object[] arguments,
257257
PNone init(VirtualFrame frame, SuperObject self, Object cls, Object obj,
258258
@Bind Node inliningTarget,
259259
@Cached @Exclusive PRaiseNode raiseNode) {
260+
if (!ensureIsTypeNode().executeCached(cls)) {
261+
throw raiseNode.raise(inliningTarget, PythonErrorType.TypeError, ErrorMessages.FIRST_ARGUMENT_MUST_BE_A_TYPE_OBJECT_NOT_P, cls);
262+
}
260263
if (!(obj instanceof PNone)) {
261264
Object type = supercheck(frame, inliningTarget, cls, obj, raiseNode);
262265
self.init(cls, type, obj);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectLookupAttr.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ static Object readAttributeQuickly(Object type, TpSlots slots, Object receiver,
335335
PythonAbstractClass base = bases[0];
336336
if (base instanceof PythonBuiltinClass &&
337337
((PythonBuiltinClass) base).getType() == PythonBuiltinClassType.PythonObject) {
338-
if (!(codePointAtIndexNode.execute(stringName, 0) == '_' && codePointAtIndexNode.execute(stringName, 1) == '_')) {
338+
int length = codePointLengthNode.execute(stringName, TS_ENCODING);
339+
if (!(length >= 2 && codePointAtIndexNode.execute(stringName, 0) == '_' && codePointAtIndexNode.execute(stringName, 1) == '_')) {
339340
// not a special name, so this attribute cannot be inherited, and can
340341
// only be on the type or the object. If it's on the type, return to
341342
// the generic code.

0 commit comments

Comments
 (0)