Skip to content

Commit e281479

Browse files
committed
Fix memory leak in RECheckInputTypeNode
1 parent 31c9860 commit e281479

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,29 @@
5151

5252
import org.graalvm.collections.EconomicMap;
5353

54-
import com.oracle.graal.python.PythonLanguage;
5554
import com.oracle.graal.python.builtins.Builtin;
5655
import com.oracle.graal.python.builtins.CoreFunctions;
5756
import com.oracle.graal.python.builtins.Python3Core;
58-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5957
import com.oracle.graal.python.builtins.PythonBuiltins;
6058
import com.oracle.graal.python.builtins.objects.PNone;
59+
import com.oracle.graal.python.builtins.objects.array.PArray;
6160
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
6261
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
62+
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
6363
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
6464
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
65+
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
66+
import com.oracle.graal.python.builtins.objects.mmap.PMMap;
6567
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6668
import com.oracle.graal.python.builtins.objects.object.PythonObject;
6769
import com.oracle.graal.python.builtins.objects.slice.SliceNodes;
68-
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6970
import com.oracle.graal.python.lib.PyLongAsIntNode;
7071
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
7172
import com.oracle.graal.python.lib.PyNumberIndexNode;
7273
import com.oracle.graal.python.lib.PyObjectGetItem;
7374
import com.oracle.graal.python.lib.PyObjectLookupAttr;
7475
import com.oracle.graal.python.lib.PyObjectSizeNode;
76+
import com.oracle.graal.python.lib.PyUnicodeCheckNode;
7577
import com.oracle.graal.python.nodes.BuiltinNames;
7678
import com.oracle.graal.python.nodes.ErrorMessages;
7779
import com.oracle.graal.python.nodes.HiddenAttr;
@@ -89,7 +91,6 @@
8991
import com.oracle.graal.python.runtime.PythonContext;
9092
import com.oracle.graal.python.runtime.PythonOptions;
9193
import com.oracle.graal.python.runtime.exception.PException;
92-
import com.oracle.graal.python.runtime.object.PFactory;
9394
import com.oracle.truffle.api.CompilerDirectives;
9495
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
9596
import com.oracle.truffle.api.dsl.Bind;
@@ -556,32 +557,26 @@ abstract static class RECheckInputTypeNode extends Node {
556557
public abstract void execute(VirtualFrame frame, Object input, boolean expectBytes);
557558

558559
@Specialization
559-
static void check(VirtualFrame frame, Object input, boolean expectBytes,
560+
static void check(Object input, boolean expectBytes,
560561
@Bind("this") Node inliningTarget,
561-
@Cached("getSupportedBinaryInputTypes()") PTuple supportedBinaryInputTypes,
562-
@Cached BuiltinFunctions.IsInstanceNode isStringNode,
563-
@Cached BuiltinFunctions.IsInstanceNode isBytesNode,
564-
@Cached InlinedConditionProfile unsupportedInputTypeProfile,
565-
@Cached InlinedConditionProfile unexpectedInputTypeProfile,
566-
@Cached PRaiseNode raiseNode) {
567-
boolean isString = (boolean) isStringNode.execute(frame, input, PythonBuiltinClassType.PString);
568-
boolean isBytes = !isString && (boolean) isBytesNode.execute(frame, input, supportedBinaryInputTypes);
569-
if (unsupportedInputTypeProfile.profile(inliningTarget, !isString && !isBytes)) {
570-
throw raiseNode.raise(inliningTarget, TypeError, T_UNSUPPORTED_INPUT_TYPE);
571-
}
572-
if (unexpectedInputTypeProfile.profile(inliningTarget, expectBytes != isBytes)) {
562+
@Cached PyUnicodeCheckNode unicodeCheckNode,
563+
@Cached BytesNodes.BytesLikeCheck bytesLikeCheck,
564+
@Cached PRaiseNode unexpectedStrRaise,
565+
@Cached PRaiseNode unexpectedBytesRaise,
566+
@Cached PRaiseNode unexpectedTypeRaise) {
567+
if (unicodeCheckNode.execute(inliningTarget, input)) {
573568
if (expectBytes) {
574-
throw raiseNode.raise(inliningTarget, TypeError, T_UNEXPECTED_STR);
575-
} else {
576-
throw raiseNode.raise(inliningTarget, TypeError, T_UNEXPECTED_BYTES);
569+
throw unexpectedStrRaise.raise(inliningTarget, TypeError, T_UNEXPECTED_STR);
577570
}
571+
return;
578572
}
579-
}
580-
581-
@NeverDefault
582-
protected PTuple getSupportedBinaryInputTypes() {
583-
return PFactory.createTuple(PythonLanguage.get(null), new Object[]{PythonBuiltinClassType.PBytes, PythonBuiltinClassType.PByteArray, PythonBuiltinClassType.PMMap,
584-
PythonBuiltinClassType.PMemoryView, PythonBuiltinClassType.PArray});
573+
if (bytesLikeCheck.execute(inliningTarget, input) || input instanceof PMMap || input instanceof PMemoryView || input instanceof PArray) {
574+
if (!expectBytes) {
575+
throw unexpectedBytesRaise.raise(inliningTarget, TypeError, T_UNEXPECTED_BYTES);
576+
}
577+
return;
578+
}
579+
throw unexpectedTypeRaise.raise(inliningTarget, TypeError, T_UNSUPPORTED_INPUT_TYPE);
585580
}
586581
}
587582

0 commit comments

Comments
 (0)