Skip to content

Commit 376e4d0

Browse files
committed
Assert that Rope#toString() is only called when a Java debugger is attached
1 parent f8c9446 commit 376e4d0

File tree

1 file changed

+15
-0
lines changed
  • src/main/java/org/truffleruby/core/rope

1 file changed

+15
-0
lines changed

src/main/java/org/truffleruby/core/rope/Rope.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
package org.truffleruby.core.rope;
1111

12+
import java.lang.management.ManagementFactory;
1213
import java.util.Arrays;
14+
import java.util.List;
1315

1416
import com.oracle.truffle.api.profiles.ConditionProfile;
1517
import org.jcodings.Encoding;
@@ -158,10 +160,23 @@ public byte get(int index) {
158160
return getByteSlow(index);
159161
}
160162

163+
private static boolean isJavaDebuggerAttached() {
164+
final List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
165+
for (String arg : inputArguments) {
166+
if (arg.contains("jdwp")) {
167+
return true;
168+
}
169+
}
170+
return false;
171+
}
172+
173+
static final boolean JAVA_DEBUGGER = isJavaDebuggerAttached();
174+
161175
/** This is designed to not have any side effects - compare to {@link #getJavaString} - but this makes it
162176
* inefficient - for debugging only */
163177
@Override
164178
public String toString() {
179+
assert JAVA_DEBUGGER : "Rope#toString() should only be called by Java debuggers, use RubyStringLibrary or RopeOperations.decodeRope() instead";
165180
return RopeOperations.decode(encoding, RopeOperations.flattenBytes(this));
166181
}
167182

0 commit comments

Comments
 (0)