Skip to content

Commit 3fdf9bc

Browse files
committed
Fix: os.readlink should raise when the file is not a symlink
1 parent c42d934 commit 3fdf9bc

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/module/PosixTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
*/
2626
package com.oracle.graal.python.test.module;
2727

28-
import static com.oracle.graal.python.test.PythonTests.assertPrints;
2928
import static com.oracle.graal.python.test.PythonTests.assertLastLineError;
29+
import static com.oracle.graal.python.test.PythonTests.assertLastLineErrorContains;
30+
import static com.oracle.graal.python.test.PythonTests.assertPrints;
3031
import static org.junit.Assert.assertEquals;
3132
import static org.junit.Assert.assertTrue;
32-
import static com.oracle.graal.python.test.PythonTests.assertLastLineErrorContains;
3333

3434
import java.io.IOException;
3535
import java.nio.file.Files;
@@ -184,7 +184,7 @@ public void printToFile() throws IOException {
184184
}
185185

186186
@Test
187-
public void readlink() throws IOException {
187+
public void readlinkWithSymlink() throws IOException {
188188
Path realPath = tmpfile.toRealPath();
189189
Path symlinkPath = realPath.getParent().resolve(tmpfile.getFileName() + "__symlink");
190190
try {
@@ -196,6 +196,13 @@ public void readlink() throws IOException {
196196
}
197197
}
198198

199+
@Test
200+
public void readlinkWithOriginalFile() throws IOException {
201+
Path realPath = tmpfile.toRealPath();
202+
assertLastLineErrorContains("OSError", "import posix\n" +
203+
"print(posix.readlink('" + realPath.toString() + "'))\n");
204+
}
205+
199206
@Test
200207
public void sysExcInfo0() {
201208
assertPrints("42\n", "import sys\n" +

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,12 @@ String readlink(VirtualFrame frame, Object str, @SuppressWarnings("unused") PNon
19001900
@CachedLibrary("str") PythonObjectLibrary lib) {
19011901
String path = lib.asPath(str);
19021902
try {
1903-
return getContext().getEnv().getPublicTruffleFile(path).getCanonicalFile().getPath();
1903+
TruffleFile original = getContext().getEnv().getPublicTruffleFile(path);
1904+
TruffleFile canonicalFile = original.getCanonicalFile();
1905+
if (original.equals(canonicalFile)) {
1906+
throw raiseOSError(frame, OSErrorEnum.EINVAL, path);
1907+
}
1908+
return canonicalFile.getPath();
19041909
} catch (Exception e) {
19051910
throw raiseOSError(frame, e, path);
19061911
}

0 commit comments

Comments
 (0)