Skip to content

Commit abd6c3a

Browse files
committed
[GR-14133] Fix relevant issues identified by Fortify report.
Part 1 - the high issues.
1 parent 0698a59 commit abd6c3a

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
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")) {

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

Lines changed: 2 additions & 1 deletion
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,7 +1286,7 @@ 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+
static final String[] shell = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows") ? new String[]{"cmd.exe", "/c"}
12891290
: new String[]{(System.getenv().getOrDefault("SHELL", "sh")), "-c"};
12901291

12911292
static class PipePump extends Thread {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/PZipImporter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ protected String makePackagePath(String fullname) {
200200
*/
201201
@CompilerDirectives.TruffleBoundary
202202
private String getCode(String filenameAndSuffix) {
203+
ZipFile zip = null;
203204
try {
204-
ZipFile zip = new ZipFile(archive);
205+
zip = new ZipFile(archive);
205206
ZipEntry entry = zip.getEntry(filenameAndSuffix);
206207
InputStream in = zip.getInputStream(entry);
207208

@@ -217,7 +218,15 @@ private String getCode(String filenameAndSuffix) {
217218
return code.toString();
218219
} catch (IOException e) {
219220
throw new RuntimeException("Can not read code from " + makePackagePath(filenameAndSuffix), e);
220-
}
221+
} finally {
222+
if (zip != null) {
223+
try {
224+
zip.close();
225+
} catch (IOException e) {
226+
// just ignore it.
227+
}
228+
}
229+
}
221230
}
222231

223232
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ public PBytes doit(PZipImporter self, String pathname) {
324324
if (fileSize < 0) {
325325
throw raise(PythonErrorType.ZipImportError, "negative data size");
326326
}
327+
ZipFile zip = null;
327328
try {
328-
ZipFile zip = new ZipFile(archive);
329+
zip = new ZipFile(archive);
329330
ZipEntry entry = zip.getEntry(key);
330331
InputStream in = zip.getInputStream(entry);
331332
int byteSize = (int) fileSize;
@@ -341,9 +342,16 @@ public PBytes doit(PZipImporter self, String pathname) {
341342
return factory().createBytes(bytes);
342343
} catch (IOException e) {
343344
throw raise(PythonErrorType.ZipImportError, "zipimport: can't read data");
344-
}
345+
} finally {
346+
if (zip != null) {
347+
try {
348+
zip.close();
349+
} catch (IOException e) {
350+
// just ignore it.
351+
}
352+
}
353+
}
345354
}
346-
347355
}
348356

349357
@Builtin(name = "get_filename", minNumOfPositionalArgs = 2)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonTreeTranslator.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -640,35 +640,31 @@ private PNode parseString(String[] strings) {
640640
}
641641
}
642642

643+
if (text.endsWith("'''") || text.endsWith("\"\"\"")) {
644+
strStartIndex += 2;
645+
strEndIndex -= 2;
646+
}
647+
648+
text = text.substring(strStartIndex, strEndIndex);
643649
if (isBytes) {
644650
if (sb != null) {
645651
throw errors.raise(SyntaxError, "cannot mix bytes and nonbytes literals");
646652
}
647653
if (bb == null) {
648654
bb = new BytesBuilder();
649655
}
656+
if (isRaw) {
657+
bb.append(text.getBytes());
658+
} else {
659+
bb.append(BytesUtils.fromString(errors, text));
660+
}
650661
} else {
651662
if (bb != null) {
652663
throw errors.raise(SyntaxError, "cannot mix bytes and nonbytes literals");
653664
}
654665
if (sb == null) {
655666
sb = new StringBuilder();
656667
}
657-
}
658-
659-
if (text.endsWith("'''") || text.endsWith("\"\"\"")) {
660-
strStartIndex += 2;
661-
strEndIndex -= 2;
662-
}
663-
664-
text = text.substring(strStartIndex, strEndIndex);
665-
if (isBytes) {
666-
if (isRaw) {
667-
bb.append(text.getBytes());
668-
} else {
669-
bb.append(BytesUtils.fromString(errors, text));
670-
}
671-
} else {
672668
if (isRaw) {
673669
sb.append(text);
674670
} else {

0 commit comments

Comments
 (0)