Skip to content

Commit 480167b

Browse files
committed
[GR-14133] Fix relevant issues identified by Fortify report.
PullRequest: graalpython/421
2 parents 7a651ba + 195d065 commit 480167b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+275
-318
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
112112
public final ConcurrentHashMap<Class<? extends PythonBuiltinBaseNode>, RootCallTarget> builtinCallTargetCache = new ConcurrentHashMap<>();
113113

114114
private static final Layout objectLayout = Layout.newLayout().build();
115-
private static final Shape freshShape = objectLayout.createShape(new ObjectType());
115+
private static final Shape newShape = objectLayout.createShape(new ObjectType());
116116

117117
public PythonLanguage() {
118118
this.nodeFactory = NodeFactory.create(this);
@@ -556,7 +556,7 @@ public PCode cacheCode(String filename, Supplier<PCode> createCode) {
556556
}
557557

558558
public static Shape freshShape() {
559-
return freshShape;
559+
return newShape;
560560
}
561561

562562
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private static final PythonBuiltins[] initializeBuiltins(Env env) {
371371
*/
372372
private boolean initialized;
373373

374-
private final PythonObjectFactory factory = PythonObjectFactory.create();
374+
private final PythonObjectFactory objectFactory = PythonObjectFactory.create();
375375

376376
public Python3Core(PythonParser parser, Env env) {
377377
this.parser = parser;
@@ -463,9 +463,9 @@ public PythonModule getBuiltins() {
463463
public PException raise(PythonBuiltinClassType type, String format, Object... args) {
464464
PBaseException instance;
465465
if (format != null) {
466-
instance = factory.createBaseException(type, format, args);
466+
instance = objectFactory.createBaseException(type, format, args);
467467
} else {
468-
instance = factory.createBaseException(type);
468+
instance = objectFactory.createBaseException(type);
469469
}
470470
throw PException.fromObject(instance, null);
471471
}
@@ -579,7 +579,7 @@ private void addBuiltinsTo(PythonObject obj, PythonBuiltins builtinsForObj) {
579579
Object value;
580580
assert obj instanceof PythonModule || obj instanceof PythonBuiltinClass : "unexpected object while adding builtins";
581581
if (obj instanceof PythonModule) {
582-
value = factory.createBuiltinMethod(obj, (PBuiltinFunction) entry.getValue());
582+
value = objectFactory.createBuiltinMethod(obj, (PBuiltinFunction) entry.getValue());
583583
} else {
584584
value = entry.getValue().boundToObject(((PythonBuiltinClass) obj).getType(), factory());
585585
}
@@ -630,7 +630,7 @@ private Source getSource(String basename, String prefix) {
630630

631631
private void loadFile(String s, String prefix) {
632632
Source source = getSource(s, prefix);
633-
Supplier<PCode> getCode = () -> factory.createCode(Truffle.getRuntime().createCallTarget((RootNode) getParser().parse(ParserMode.File, this, source, null)));
633+
Supplier<PCode> getCode = () -> objectFactory.createCode(Truffle.getRuntime().createCallTarget((RootNode) getParser().parse(ParserMode.File, this, source, null)));
634634
RootCallTarget callTarget = getLanguage().cacheCode(source.getName(), getCode).getRootCallTarget();
635635
PythonModule mod = lookupBuiltinModule(s);
636636
if (mod == null) {
@@ -641,7 +641,7 @@ private void loadFile(String s, String prefix) {
641641
}
642642

643643
public PythonObjectFactory factory() {
644-
return factory;
644+
return objectFactory;
645645
}
646646

647647
public void setContext(PythonContext context) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private byte[] b64decode(byte[] data) {
134134
@Builtin(name = "a2b_hex", minNumOfPositionalArgs = 2, declaresExplicitSelf = true)
135135
@GenerateNodeFactory
136136
abstract static class A2bHexNode extends PythonBinaryBuiltinNode {
137-
private ReadAttributeFromObjectNode getAttrNode;
137+
private ReadAttributeFromObjectNode readAttrNode;
138138

139139
private PException raise(LazyPythonClass klass, String string) {
140140
return raise(factory().createBaseException(klass, string, new Object[0]));
@@ -167,11 +167,11 @@ private PException nonHexError(PythonModule self) {
167167
}
168168

169169
private ReadAttributeFromObjectNode getAttrNode() {
170-
if (getAttrNode == null) {
170+
if (readAttrNode == null) {
171171
CompilerDirectives.transferToInterpreterAndInvalidate();
172-
getAttrNode = insert(ReadAttributeFromObjectNode.create());
172+
readAttrNode = insert(ReadAttributeFromObjectNode.create());
173173
}
174-
return getAttrNode;
174+
return readAttrNode;
175175
}
176176
}
177177

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
import com.oracle.truffle.api.object.Shape;
182182
import com.oracle.truffle.api.profiles.BranchProfile;
183183
import com.oracle.truffle.api.profiles.ConditionProfile;
184+
import java.util.Locale;
184185

185186
@CoreFunctions(defineModule = "builtins")
186187
public final class BuiltinConstructors extends PythonBuiltins {
@@ -758,7 +759,7 @@ private double convertStringToDouble(String str) {
758759
}
759760
try {
760761
// Double.valueOf allows format specifier ("d" or "f") at the end
761-
String lowSval = sval.toLowerCase();
762+
String lowSval = sval.toLowerCase(Locale.ENGLISH);
762763
if (lowSval.equals("nan") || lowSval.equals("+nan") || lowSval.equals("-nan")) {
763764
return Double.NaN;
764765
} else if (lowSval.equals("inf") || lowSval.equals("+inf") || lowSval.equals("infinity") || lowSval.equals("+infinity")) {
@@ -1343,7 +1344,7 @@ public PList listObject(@SuppressWarnings("unused") Object cls, Object arg) {
13431344
@Builtin(name = OBJECT, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PythonObject)
13441345
@GenerateNodeFactory
13451346
public abstract static class ObjectNode extends PythonVarargsBuiltinNode {
1346-
@Child private PCallCapiFunction callNativeGenericNewNode;
1347+
@Child private PCallCapiFunction callCapiFunction;
13471348
@Children private CExtNodes.ToSulongNode[] toSulongNodes;
13481349
@Child private CExtNodes.AsPythonObjectNode asPythonObjectNode;
13491350
@Child private TypeNodes.GetInstanceShape getInstanceShapeNode;
@@ -1402,9 +1403,9 @@ private static PythonNativeClass findFirstNativeBaseClass(PythonAbstractClass[]
14021403
}
14031404

14041405
private Object callNativeGenericNewNode(PythonNativeClass self, Object[] varargs, PKeyword[] kwargs) {
1405-
if (callNativeGenericNewNode == null) {
1406+
if (callCapiFunction == null) {
14061407
CompilerDirectives.transferToInterpreterAndInvalidate();
1407-
callNativeGenericNewNode = insert(PCallCapiFunction.create(NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_NEW));
1408+
callCapiFunction = insert(PCallCapiFunction.create(NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_NEW));
14081409
}
14091410
if (toSulongNodes == null) {
14101411
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -1421,7 +1422,7 @@ private Object callNativeGenericNewNode(PythonNativeClass self, Object[] varargs
14211422
PTuple targs = factory().createTuple(varargs);
14221423
PDict dkwargs = factory().createDict(kwarr);
14231424
return asPythonObjectNode.execute(
1424-
callNativeGenericNewNode.call(toSulongNodes[0].execute(self), toSulongNodes[1].execute(self), toSulongNodes[2].execute(targs), toSulongNodes[3].execute(dkwargs)));
1425+
callCapiFunction.call(toSulongNodes[0].execute(self), toSulongNodes[1].execute(self), toSulongNodes[2].execute(targs), toSulongNodes[3].execute(dkwargs)));
14251426
}
14261427

14271428
private Shape getInstanceShape(LazyPythonClass clazz) {
@@ -1782,8 +1783,8 @@ public abstract static class TypeNode extends PythonBuiltinNode {
17821783
@Child private CastToListNode castToList;
17831784
@Child private CastToStringNode castToStringNode;
17841785
@Child private SequenceStorageNodes.LenNode slotLenNode;
1785-
@Child private SequenceStorageNodes.GetItemNode getSlotItemNode;
1786-
@Child private SequenceStorageNodes.AppendNode setSlotItemNode;
1786+
@Child private SequenceStorageNodes.GetItemNode getItemNode;
1787+
@Child private SequenceStorageNodes.AppendNode appendNode;
17871788
@Child private HashingStorageNodes.ContainsKeyNode containsKeyNode;
17881789
@Child private HashingStorageNodes.GetItemNode getDictItemNode;
17891790
@Child private CExtNodes.PCallCapiFunction callAddNativeSlotsNode;
@@ -1971,7 +1972,7 @@ private PTuple copySlots(String className, SequenceStorage slotList, int slotlen
19711972
}
19721973

19731974
setSlotItemNode().execute(newSlots, slotName);
1974-
if (containsKeyNode().execute(namespace.getDictStorage(), slotName)) {
1975+
if (getContainsKeyNode().execute(namespace.getDictStorage(), slotName)) {
19751976
throw raise(PythonBuiltinClassType.ValueError, "%s in __slots__ conflicts with class variable", slotName);
19761977
}
19771978
j++;
@@ -2020,7 +2021,7 @@ private String mangle(String privateobj, String ident) {
20202021
return "_" + privateobj.substring(ipriv) + ident;
20212022
}
20222023

2023-
private HashingStorageNodes.ContainsKeyNode containsKeyNode() {
2024+
private HashingStorageNodes.ContainsKeyNode getContainsKeyNode() {
20242025
if (containsKeyNode == null) {
20252026
CompilerDirectives.transferToInterpreterAndInvalidate();
20262027
containsKeyNode = insert(HashingStorageNodes.ContainsKeyNode.create());
@@ -2029,19 +2030,19 @@ private HashingStorageNodes.ContainsKeyNode containsKeyNode() {
20292030
}
20302031

20312032
private SequenceStorageNodes.GetItemNode getSlotItemNode() {
2032-
if (getSlotItemNode == null) {
2033+
if (getItemNode == null) {
20332034
CompilerDirectives.transferToInterpreterAndInvalidate();
2034-
getSlotItemNode = insert(SequenceStorageNodes.GetItemNode.create());
2035+
getItemNode = insert(SequenceStorageNodes.GetItemNode.create());
20352036
}
2036-
return getSlotItemNode;
2037+
return getItemNode;
20372038
}
20382039

20392040
private SequenceStorageNodes.AppendNode setSlotItemNode() {
2040-
if (setSlotItemNode == null) {
2041+
if (appendNode == null) {
20412042
CompilerDirectives.transferToInterpreterAndInvalidate();
2042-
setSlotItemNode = insert(SequenceStorageNodes.AppendNode.create(() -> NoGeneralizationNode.create("")));
2043+
appendNode = insert(SequenceStorageNodes.AppendNode.create(() -> NoGeneralizationNode.create("")));
20432044
}
2044-
return setSlotItemNode;
2045+
return appendNode;
20452046
}
20462047

20472048
private SequenceStorageNodes.LenNode getListLenNode() {

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,6 @@ public PTuple doObject(Object a, Object b,
583583
return factory().createTuple(new Object[]{div, mod});
584584
}
585585

586-
@TruffleBoundary
587-
private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
588-
BigInteger[] result = a.getValue().divideAndRemainder(b.getValue());
589-
assert result.length == 2;
590-
return result;
591-
}
592586
}
593587

594588
// eval(expression, globals=None, locals=None)

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
import java.io.IOException;
4747
import java.nio.ByteBuffer;
48-
import java.nio.channels.FileChannel.MapMode;
4948
import java.nio.channels.SeekableByteChannel;
5049
import java.nio.file.StandardOpenOption;
5150
import java.util.HashSet;
@@ -155,21 +154,6 @@ protected static boolean isIllegal(int fd) {
155154
return fd < -1;
156155
}
157156

158-
@SuppressWarnings("unused")
159-
private MapMode convertAccessToMapMode(int access) {
160-
switch (access) {
161-
case 0:
162-
return MapMode.READ_WRITE;
163-
case 1:
164-
return MapMode.READ_ONLY;
165-
case 2:
166-
return MapMode.READ_WRITE;
167-
case 3:
168-
return MapMode.PRIVATE;
169-
}
170-
throw raise(ValueError, "mmap invalid access parameter.");
171-
}
172-
173157
private void checkLength(int length) {
174158
if (length < 0) {
175159
invalidLengthProfile.enter();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public int ceil(boolean value) {
248248
public Object ceil(PFloat value,
249249
@Cached("create(__CEIL__)") LookupAndCallUnaryNode dispatchCeil) {
250250
Object result = dispatchCeil.executeObject(value);
251-
if (PNone.NO_VALUE.equals(result)) {
251+
if (PNone.NO_VALUE == result) {
252252
if (MathGuards.fitLong(value.getValue())) {
253253
return ceilLong(value.getValue());
254254
} else {
@@ -582,7 +582,7 @@ public Object floor(Object value,
582582
@Cached("create()") CastToDoubleNode castNode,
583583
@Cached("create()") FloorNode recursiveNode) {
584584
Object result = dispatchFloor.executeObject(value);
585-
if (PNone.NO_VALUE.equals(result)) {
585+
if (PNone.NO_VALUE == result) {
586586
return recursiveNode.execute(castNode.execute(value));
587587
}
588588
return result;

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import com.oracle.truffle.api.profiles.ValueProfile;
136136
import com.sun.security.auth.UnixNumericGroupPrincipal;
137137
import com.sun.security.auth.UnixNumericUserPrincipal;
138+
import java.util.Locale;
138139

139140
@CoreFunctions(defineModule = "posix")
140141
public class PosixModuleBuiltins extends PythonBuiltins {
@@ -1285,8 +1286,12 @@ PTuple waitpid(Object pid, Object options) {
12851286
@GenerateNodeFactory
12861287
@TypeSystemReference(PythonArithmeticTypes.class)
12871288
abstract static class SystemNode extends PythonBuiltinNode {
1288-
static final String[] shell = System.getProperty("os.name").toLowerCase().startsWith("windows") ? new String[]{"cmd.exe", "/c"}
1289-
: new String[]{(System.getenv().getOrDefault("SHELL", "sh")), "-c"};
1289+
static final String[] shell;
1290+
static {
1291+
String osProperty = System.getProperty("os.name");
1292+
shell = osProperty != null && osProperty.toLowerCase(Locale.ENGLISH).startsWith("windows") ? new String[]{"cmd.exe", "/c"}
1293+
: new String[]{(System.getenv().getOrDefault("SHELL", "sh")), "-c"};
1294+
}
12901295

12911296
static class PipePump extends Thread {
12921297
private static final int MAX_READ = 8192;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
import java.io.UnsupportedEncodingException;
4848
import java.util.List;
49-
import java.util.regex.Pattern;
5049

5150
import com.oracle.graal.python.builtins.Builtin;
5251
import com.oracle.graal.python.builtins.CoreFunctions;
@@ -64,7 +63,6 @@
6463
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6564
import com.oracle.truffle.api.CompilerAsserts;
6665
import com.oracle.truffle.api.CompilerDirectives;
67-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6866
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6967
import com.oracle.truffle.api.dsl.Cached;
7068
import com.oracle.truffle.api.dsl.Fallback;
@@ -111,8 +109,6 @@ abstract static class ProcessEscapeSequences extends PythonUnaryBuiltinNode {
111109
@Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode;
112110
@Child private BytesNodes.ToBytesNode toBytesNode;
113111

114-
@CompilationFinal private Pattern namedCaptGroupPattern;
115-
116112
@Specialization
117113
Object run(PString str) {
118114
return run(str.getValue());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ public void postInitialize(PythonCore core) {
120120
}
121121

122122
private static class SignalTriggerAction implements AsyncHandler.AsyncAction {
123-
private final Object callable;
123+
private final Object callableObject;
124124
private final int signum;
125125

126126
SignalTriggerAction(Object callable, int signum) {
127-
this.callable = callable;
127+
this.callableObject = callable;
128128
this.signum = signum;
129129
}
130130

131131
public Object callable() {
132-
return callable;
132+
return callableObject;
133133
}
134134

135135
public Object[] arguments() {

0 commit comments

Comments
 (0)