Skip to content

Commit 13aacf0

Browse files
committed
fork_exec: do not use ASCII encoding for env vars and paths
1 parent b2eea70 commit 13aacf0

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.nio.channels.Channel;
4848
import java.nio.channels.Channels;
4949
import java.nio.channels.WritableByteChannel;
50-
import java.nio.charset.StandardCharsets;
5150
import java.util.ArrayList;
5251
import java.util.HashMap;
5352
import java.util.List;
@@ -164,10 +163,10 @@ private synchronized int forkExec(PList args, PList execList, @SuppressWarnings(
164163
if (keyValue instanceof PBytes) {
165164
// NOTE: passing 'null' frame means we took care of the global state in the
166165
// callers
167-
byte[] bytes = checkNullBytes(toBytes.execute(null, keyValue));
168-
String[] string = new String(bytes, StandardCharsets.US_ASCII).split("=", 2);
169-
if (string.length == 2) {
170-
envMap.put(string[0], string[1]);
166+
String str = checkNullBytesAndEncode(toBytes.execute(null, keyValue));
167+
String[] strings = str.split("=", 2);
168+
if (strings.length == 2) {
169+
envMap.put(strings[0], strings[1]);
171170
}
172171
}
173172
}
@@ -199,8 +198,7 @@ private synchronized int forkExec(PList args, PList execList, @SuppressWarnings(
199198
if (!(item instanceof PBytes)) {
200199
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.EXPECTED_BYTES_P_FOUND, item);
201200
}
202-
byte[] bytes = checkNullBytes(toBytes.execute(null, item));
203-
String path = new String(bytes, StandardCharsets.US_ASCII);
201+
String path = checkNullBytesAndEncode(toBytes.execute(null, item));
204202
int executableListLen = 0;
205203
if (path.equals(context.getOption(PythonOptions.Executable))) {
206204
// In case someone passed to us sys.executable that happens to be java command
@@ -222,8 +220,8 @@ private synchronized int forkExec(PList args, PList execList, @SuppressWarnings(
222220
firstError = ex;
223221
}
224222
}
225-
for (int j = 0; j < executableListLen - 1; j++) {
226-
argStrings.remove(j + 1);
223+
for (int j = 1; j < executableListLen; j++) {
224+
argStrings.remove(1);
227225
}
228226
}
229227
assert firstError != null;
@@ -291,13 +289,17 @@ private int exec(ArrayList<String> argStrings, File cwd, Map<String, String> env
291289
return resources.registerChild(process);
292290
}
293291

294-
private byte[] checkNullBytes(byte[] bytes) {
292+
private String checkNullBytesAndEncode(byte[] bytes) {
295293
for (byte b : bytes) {
296294
if (b == 0) {
297295
throw raise(PythonBuiltinClassType.ValueError, ErrorMessages.EMBEDDED_NULL_BYTE);
298296
}
299297
}
300-
return bytes;
298+
// Note: we use intentionally the default encoding for the bytes. We're most likely
299+
// getting bytes that the Python wrapper encoded from strings passed to it by the user
300+
// and we should support non-ascii characters supported by the current FS. See
301+
// test_warnings.test_nonascii
302+
return new String(bytes);
301303
}
302304

303305
@TruffleBoundary(allowInlining = true)

0 commit comments

Comments
 (0)