Skip to content

Commit 19c0a0c

Browse files
committed
Prevent backtrace generation for SystemCallError when initializing
1 parent 53b07fd commit 19c0a0c

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bug fixes:
4141
* Make `String#concat` work with no or multiple arguments (#1519).
4242
* Make `Array#concat` work with no or multiple arguments (#1519).
4343
* Fixed BigDecimal coerce initial argument using `to_str` (#1826).
44+
* Fixed issue with `SystemCallError.new` setting a backtrace.
4445

4546
Compatibility:
4647

spec/ruby/core/exception/system_call_error_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,9 @@ def initialize
128128
SystemCallError.new("XXX").message.should =~ /XXX/
129129
end
130130
end
131+
132+
describe "SystemCallError#backtrace" do
133+
it "is nil if not raised" do
134+
SystemCallError.new("message", 42).backtrace.should == nil
135+
end
136+
end

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.truffleruby.language.RubyRootNode;
6666
import org.truffleruby.language.Visibility;
6767
import org.truffleruby.language.arguments.RubyArguments;
68+
import org.truffleruby.language.backtrace.Backtrace;
6869
import org.truffleruby.language.constants.GetConstantNode;
6970
import org.truffleruby.language.constants.LookupConstantNode;
7071
import org.truffleruby.language.control.BreakException;
@@ -926,14 +927,16 @@ public abstract static class RbSysErrFail extends CoreMethodArrayArgumentsNode {
926927

927928
@Specialization(guards = "isNil(message)")
928929
protected Object rbSysErrFailNoMessage(int errno, DynamicObject message) {
929-
throw new RaiseException(getContext(), coreExceptions().errnoError(errno, "", this));
930+
final Backtrace backtrace = getContext().getCallStack().getBacktrace(this);
931+
throw new RaiseException(getContext(), coreExceptions().errnoError(errno, "", backtrace));
930932
}
931933

932934
@Specialization(guards = "isRubyString(message)")
933935
protected Object rbSysErrFail(int errno, DynamicObject message) {
936+
final Backtrace backtrace = getContext().getCallStack().getBacktrace(this);
934937
throw new RaiseException(
935938
getContext(),
936-
coreExceptions().errnoError(errno, StringOperations.getString(message), this));
939+
coreExceptions().errnoError(errno, StringOperations.getString(message), backtrace));
937940
}
938941

939942
}

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,27 +352,30 @@ public DynamicObject mathDomainError(String method, Node currentNode) {
352352
StringUtils.format("Numerical argument is out of domain - \"%s\"", method),
353353
UTF8Encoding.INSTANCE);
354354
DynamicObject errorMessage = StringOperations.createString(context, rope);
355-
return ExceptionOperations.createSystemCallError(
356-
context,
357-
exceptionClass,
358-
errorMessage,
359-
currentNode,
360-
context.getCoreLibrary().getErrnoValue("EDOM"));
355+
final Backtrace backtrace = context.getCallStack().getBacktrace(currentNode);
356+
return ExceptionOperations
357+
.createSystemCallError(
358+
context,
359+
exceptionClass,
360+
errorMessage,
361+
context.getCoreLibrary().getErrnoValue("EDOM"),
362+
backtrace);
361363
}
362364

363365
@TruffleBoundary
364-
public DynamicObject errnoError(int errno, String extraMessage, Node currentNode) {
366+
public DynamicObject errnoError(int errno, String extraMessage, Backtrace backtrace) {
365367
final String errnoName = context.getCoreLibrary().getErrnoName(errno);
366368
if (errnoName == null) {
367-
return systemCallError(StringUtils.format("Unknown Error (%s)%s", errno, extraMessage), errno, currentNode);
369+
return systemCallError(StringUtils.format("Unknown Error (%s)%s", errno, extraMessage), errno, backtrace);
368370
}
369371

370372
final DynamicObject errnoClass = context.getCoreLibrary().getErrnoClass(errnoName);
371373
final String fullMessage = ErrnoDescriptions.getDescription(errnoName) + extraMessage;
372374
final DynamicObject errorMessage = StringOperations
373375
.createString(context, StringOperations.encodeRope(fullMessage, UTF8Encoding.INSTANCE));
374376

375-
return ExceptionOperations.createSystemCallError(context, errnoClass, errorMessage, currentNode, errno);
377+
return ExceptionOperations
378+
.createSystemCallError(context, errnoClass, errorMessage, errno, backtrace);
376379
}
377380

378381
// IndexError
@@ -1081,7 +1084,7 @@ public DynamicObject securityError(String message, Node currentNode) {
10811084
// SystemCallError
10821085

10831086
@TruffleBoundary
1084-
public DynamicObject systemCallError(String message, int errno, Node currentNode) {
1087+
public DynamicObject systemCallError(String message, int errno, Backtrace backtrace) {
10851088
DynamicObject exceptionClass = context.getCoreLibrary().getSystemCallErrorClass();
10861089
DynamicObject errorMessage;
10871090
if (message == null) {
@@ -1090,7 +1093,8 @@ public DynamicObject systemCallError(String message, int errno, Node currentNode
10901093
errorMessage = StringOperations
10911094
.createString(context, StringOperations.encodeRope(message, UTF8Encoding.INSTANCE));
10921095
}
1093-
return ExceptionOperations.createSystemCallError(context, exceptionClass, errorMessage, currentNode, errno);
1096+
return ExceptionOperations
1097+
.createSystemCallError(context, exceptionClass, errorMessage, errno, backtrace);
10941098
}
10951099

10961100
// FFI::NullPointerError

src/main/java/org/truffleruby/core/exception/ExceptionNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public static abstract class ExceptionErrnoErrorPrimitiveNode extends PrimitiveA
257257
@TruffleBoundary
258258
@Specialization
259259
protected DynamicObject exceptionErrnoError(DynamicObject message, int errno) {
260-
return coreExceptions().errnoError(errno, StringOperations.getString(message), this);
260+
return coreExceptions().errnoError(errno, StringOperations.getString(message), null);
261261
}
262262

263263
}

src/main/java/org/truffleruby/core/exception/ExceptionOperations.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public static DynamicObject createRubyException(RubyContext context, DynamicObje
6969
return createRubyException(context, rubyClass, message, backtrace);
7070
}
7171

72-
public static DynamicObject createSystemCallError(RubyContext context, DynamicObject rubyClass, Object message,
73-
Node node, int errno) {
74-
final Backtrace backtrace = context.getCallStack().getBacktrace(node);
75-
return createSystemCallError(context, rubyClass, message, errno, backtrace);
76-
}
77-
7872
// because the factory is not constant
7973
@TruffleBoundary
8074
public static DynamicObject createRubyException(RubyContext context, DynamicObject rubyClass, Object message,
@@ -88,7 +82,8 @@ public static DynamicObject createRubyException(RubyContext context, DynamicObje
8882

8983
// because the factory is not constant
9084
@TruffleBoundary
91-
private static DynamicObject createSystemCallError(RubyContext context, DynamicObject rubyClass, Object message,
85+
public static DynamicObject createSystemCallError(RubyContext context, DynamicObject rubyClass,
86+
Object message,
9287
int errno, Backtrace backtrace) {
9388
final DynamicObject cause = ThreadGetExceptionNode.getLastException(context);
9489
context.getCoreExceptions().showExceptionIfDebug(rubyClass, message, backtrace);

src/main/java/org/truffleruby/core/time/RubyDateFormatter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.truffleruby.core.rope.RopeOperations;
6464
import org.truffleruby.core.string.StringOperations;
6565
import org.truffleruby.language.RubyGuards;
66+
import org.truffleruby.language.backtrace.Backtrace;
6667
import org.truffleruby.language.control.RaiseException;
6768

6869
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -522,12 +523,13 @@ public static RopeBuilder formatToRopeBuilder(List<Token> compiledPattern, Zoned
522523
try {
523524
output = formatter.format(output, value, type);
524525
} catch (IndexOutOfBoundsException ioobe) {
526+
final Backtrace backtrace = context.getCallStack().getBacktrace(currentNode);
525527
throw new RaiseException(
526528
context,
527529
context.getCoreExceptions().errnoError(
528530
context.getCoreLibrary().getErrnoValue("ERANGE"),
529531
"strftime",
530-
currentNode));
532+
backtrace));
531533
}
532534

533535
// reset formatter

0 commit comments

Comments
 (0)