Skip to content

Commit da9496e

Browse files
committed
Fix asFileDescriptorWithState: raise if the result is negative
1 parent 8042c33 commit da9496e

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,13 +2407,13 @@ public static OffsetConversionNode create() {
24072407
}
24082408

24092409
/**
2410-
* Equivalent of CPython's {@code fildes_converter()}. Always returns an {@code int}.
2410+
* Equivalent of CPython's {@code fildes_converter()}, which in turn delegates to
2411+
* {@code PyObject_AsFileDescriptor}. Always returns an {@code int}.
24112412
*/
24122413
public abstract static class FileDescriptorConversionNode extends ArgumentCastNodeWithRaise {
2413-
24142414
@Specialization
24152415
int doFdInt(int value) {
2416-
return value;
2416+
return PInt.asFileDescriptor(value, getRaiseNode());
24172417
}
24182418

24192419
@Specialization(guards = "!isInt(value)", limit = "3")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ public int asFileDescriptorWithState(ThreadState state,
863863
Object result = methodLib.callObjectWithState(filenoFunc, state);
864864
if (isIntProfile.profileObject(result, PythonBuiltinClassType.PInt)) {
865865
try {
866-
return castToJavaIntNode.execute(result);
866+
return PInt.asFileDescriptor(castToJavaIntNode.execute(result), raiseNode);
867867
} catch (PException e) {
868868
e.expect(PythonBuiltinClassType.TypeError, isAttrError);
869869
throw raiseNode.raise(PythonBuiltinClassType.OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "int");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,21 @@ public Object asIndexWithState(@SuppressWarnings("unused") ThreadState threadSta
251251
public int asFileDescriptorWithState(@SuppressWarnings("unused") ThreadState state,
252252
@Exclusive @Cached PRaiseNode raiseNode,
253253
@Exclusive @Cached CastToJavaIntExactNode castToJavaIntNode) {
254+
int result;
254255
try {
255-
return castToJavaIntNode.execute(this);
256+
result = castToJavaIntNode.execute(this);
256257
} catch (PException e) {
257258
throw raiseNode.raise(PythonBuiltinClassType.OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "int");
258259
}
260+
return asFileDescriptor(result, raiseNode);
261+
}
262+
263+
@Ignore
264+
public static int asFileDescriptor(int value, PRaiseNode raiseNode) {
265+
if (value < 0) {
266+
raiseNode.raise(PythonBuiltinClassType.ValueError, ErrorMessages.S_CANNOT_BE_NEGATIVE_INTEGER_D, "file descriptor", value);
267+
}
268+
return value;
259269
}
260270

261271
@SuppressWarnings("static-method")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonIntegerExports.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
4848
import com.oracle.graal.python.builtins.objects.ints.PInt;
4949
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
50+
import com.oracle.graal.python.nodes.PRaiseNode;
5051
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
5152
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5253
import com.oracle.graal.python.runtime.PythonOptions;
@@ -257,8 +258,9 @@ static String asPStringWithState(Integer receiver, @SuppressWarnings("unused") T
257258
}
258259

259260
@ExportMessage
260-
static int asFileDescriptorWithState(Integer x, @SuppressWarnings("unused") ThreadState state) {
261-
return x;
261+
static int asFileDescriptorWithState(Integer x, @SuppressWarnings("unused") ThreadState state,
262+
@Cached PRaiseNode raiseNode) {
263+
return PInt.asFileDescriptor(x, raiseNode);
262264
}
263265

264266
@SuppressWarnings("static-method")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonLongExports.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static int asFileDescriptorWithState(Long x, @SuppressWarnings("unused") ThreadS
285285
@Exclusive @Cached CastToJavaIntExactNode castToJavaIntNode,
286286
@Exclusive @Cached IsBuiltinClassProfile errorProfile) {
287287
try {
288-
return castToJavaIntNode.execute(x);
288+
return PInt.asFileDescriptor(castToJavaIntNode.execute(x), raiseNode);
289289
} catch (PException e) {
290290
e.expect(PythonBuiltinClassType.TypeError, errorProfile);
291291
// we need to convert the TypeError to an OverflowError

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public abstract class ErrorMessages {
120120
public static final String CANNOT_ASSIGN_TO_COMPREHENSION = "cannot assign to %s comprehension";
121121
public static final String CANNOT_BE_INTEPRETED_AS_LONG = "%s cannot be interpreted as long (type %p)";
122122
public static final String CANNOT_BE_NEGATIVE = "%s cannot be negative";
123+
public static final String S_CANNOT_BE_NEGATIVE_INTEGER_D = "%s cannot be negative integer (%d)";
123124
public static final String CANNOT_CALL_CTOR_OF = "cannot call constructor of %s";
124125
public static final String CANNOT_CLOSE_EXPORTED_PTRS_EXIST = "cannot close exported pointers exist";
125126
public static final String CANNOT_CONVERT_DICT_UPDATE_SEQ = "cannot convert dictionary update sequence element #%d to a sequence";

0 commit comments

Comments
 (0)