Skip to content

Commit a70eec5

Browse files
committed
[GR-23835] Fix: os.readlink should raise when the file is not a symlink.
PullRequest: graalpython/996
2 parents bb2a6bc + 3fdf9bc commit a70eec5

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
@@ -1911,7 +1911,12 @@ String readlink(VirtualFrame frame, Object str, @SuppressWarnings("unused") PNon
19111911
@CachedLibrary("str") PythonObjectLibrary lib) {
19121912
String path = lib.asPath(str);
19131913
try {
1914-
return getContext().getEnv().getPublicTruffleFile(path).getCanonicalFile().getPath();
1914+
TruffleFile original = getContext().getEnv().getPublicTruffleFile(path);
1915+
TruffleFile canonicalFile = original.getCanonicalFile();
1916+
if (original.equals(canonicalFile)) {
1917+
throw raiseOSError(frame, OSErrorEnum.EINVAL, path);
1918+
}
1919+
return canonicalFile.getPath();
19151920
} catch (Exception e) {
19161921
throw raiseOSError(frame, e, path);
19171922
}

0 commit comments

Comments
 (0)