Skip to content

Commit c9e701a

Browse files
committed
Consider pointers with an unknown size to have a size of LONG_MAX
* Which is the same that FFI on MRI does.
1 parent 294a54f commit c9e701a

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.truffleruby.core.time.RubyTime;
7878
import org.truffleruby.core.tracepoint.RubyTracePoint;
7979
import org.truffleruby.extra.RubyAtomicReference;
80+
import org.truffleruby.extra.ffi.Pointer;
8081
import org.truffleruby.extra.ffi.RubyPointer;
8182
import org.truffleruby.language.Nil;
8283
import org.truffleruby.language.NotProvided;
@@ -808,6 +809,8 @@ private void initializeConstants() {
808809
setConstant(truffleFFIModule, "TYPE_ENUM", NativeTypes.TYPE_ENUM);
809810
setConstant(truffleFFIModule, "TYPE_VARARGS", NativeTypes.TYPE_VARARGS);
810811

812+
setConstant(truffleFFIPointerClass, "UNBOUNDED", Pointer.UNBOUNDED);
813+
811814
setConstant(objectClass, "RUBY_VERSION", frozenUSASCIIString(TruffleRuby.LANGUAGE_VERSION));
812815
setConstant(objectClass, "RUBY_PATCHLEVEL", 0);
813816
setConstant(objectClass, "RUBY_REVISION", frozenUSASCIIString(TruffleRuby.LANGUAGE_REVISION));

src/main/java/org/truffleruby/extra/ffi/Pointer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Pointer implements AutoCloseable {
2626

2727
public static final Pointer NULL = new Pointer(0);
2828
public static final long SIZE = Long.BYTES;
29+
public static final long UNBOUNDED = Long.MAX_VALUE;
2930

3031
public static final Pointer[] EMPTY_ARRAY = new Pointer[0];
3132

@@ -49,7 +50,7 @@ public static Pointer calloc(long size) {
4950
private FinalizerReference finalizerRef = null;
5051

5152
public Pointer(long address) {
52-
this(address, 0);
53+
this(address, UNBOUNDED);
5354
}
5455

5556
public Pointer(long address, long size) {

src/main/ruby/truffleruby/core/truffle/ffi/pointer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def initialize(type = nil, address)
7575

7676
def initialize_copy(from)
7777
total = Primitive.pointer_size(from)
78-
raise RuntimeError, 'cannot duplicate unbounded memory area' unless total > 0
78+
raise RuntimeError, 'cannot duplicate unbounded memory area' unless total != UNBOUNDED
7979
Primitive.pointer_malloc self, total
8080
Primitive.pointer_copy_memory address, from.address, total
8181
self
@@ -87,7 +87,7 @@ def size
8787
alias_method :total, :size
8888

8989
def clear
90-
raise RuntimeError, 'cannot clear unbounded memory area' unless Primitive.pointer_size(self) > 0
90+
raise RuntimeError, 'cannot clear unbounded memory area' unless Primitive.pointer_size(self) != UNBOUNDED
9191
Primitive.pointer_clear self, Primitive.pointer_size(self)
9292
end
9393

@@ -116,7 +116,7 @@ def null?
116116

117117
def +(offset)
118118
ptr = Pointer.new(address + offset)
119-
if Primitive.pointer_size(self) > 0
119+
if Primitive.pointer_size(self) != UNBOUNDED
120120
ptr.total = Primitive.pointer_size(self) - offset
121121
end
122122
ptr
@@ -145,7 +145,7 @@ def network_order(start, size)
145145

146146
private def check_bounds(offset, length)
147147
size = Primitive.pointer_size(self)
148-
if offset < 0 || (size > 0 && offset + length > size)
148+
if offset < 0 || offset + length > size
149149
raise IndexError, "Memory access offset=#{offset} size=#{length} is out of bounds"
150150
end
151151
end

0 commit comments

Comments
 (0)