Skip to content

Commit 2027581

Browse files
committed
[feat] stack-trace debugging with unexpected catches
1 parent ec60013 commit 2027581

File tree

9 files changed

+25
-16
lines changed

9 files changed

+25
-16
lines changed

src/main/java/org/jruby/ext/openssl/OpenSSL.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ public static void debug(final Ruby runtime, final CharSequence msg, final Throw
243243
if ( isDebug(runtime) ) runtime.getOut().println(msg.toString() + ' ' + e);
244244
}
245245

246+
public static void debugStackTrace(final Ruby runtime, final CharSequence msg, final Throwable e) {
247+
if ( isDebug(runtime) ) {
248+
synchronized (runtime.getOut()) {
249+
runtime.getOut().print(msg.toString() + ' ');
250+
e.printStackTrace(runtime.getOut());
251+
}
252+
}
253+
}
254+
246255
static void warn(final ThreadContext context, final CharSequence msg) {
247256
if ( warn ) warn(context, RubyString.newString(context.runtime, msg));
248257
}

src/main/java/org/jruby/ext/openssl/PKeyDSA.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
241241
}
242242
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
243243
catch (InvalidKeySpecException e) { debug(runtime, "PKeyDSA could not read private key", e); }
244-
catch (IOException e) { debug(runtime, "PKeyDSA could not read private key", e); }
244+
catch (IOException e) { debugStackTrace(runtime, "PKeyDSA could not read private key", e); }
245245
catch (RuntimeException e) {
246246
if ( isKeyGenerationFailure(e) ) debug(runtime, "PKeyDSA could not read private key", e);
247247
else debugStackTrace(runtime, e);
@@ -253,7 +253,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
253253
}
254254
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
255255
catch (InvalidKeySpecException e) { debug(runtime, "PKeyDSA could not read public key", e); }
256-
catch (IOException e) { debug(runtime, "PKeyDSA could not read public key", e); }
256+
catch (IOException e) { debugStackTrace(runtime, "PKeyDSA could not read public key", e); }
257257
catch (RuntimeException e) {
258258
if ( isKeyGenerationFailure(e) ) debug(runtime, "PKeyDSA could not read public key", e);
259259
else debugStackTrace(runtime, e);

src/main/java/org/jruby/ext/openssl/PKeyEC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
297297
}
298298
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
299299
catch (InvalidKeySpecException e) { debug(runtime, "PKeyEC could not read private key", e); }
300-
catch (IOException e) { debug(runtime, "PKeyEC could not read private key", e); }
300+
catch (IOException e) { debugStackTrace(runtime, "PKeyEC could not read private key", e); }
301301
catch (RuntimeException e) {
302302
if ( isKeyGenerationFailure(e) ) debug(runtime, "PKeyEC could not read private key", e);
303303
else debugStackTrace(runtime, e);

src/main/java/org/jruby/ext/openssl/PKeyRSA.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
287287
try { key = readRSAPrivateKey(rsaFactory, str.getBytes()); }
288288
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
289289
catch (InvalidKeySpecException e) { debug(runtime, "PKeyRSA could not read private key", e); }
290-
catch (IOException e) { debug(runtime, "PKeyRSA could not read private key", e); }
290+
catch (IOException e) { debugStackTrace(runtime, "PKeyRSA could not read private key", e); }
291291
catch (RuntimeException e) {
292292
if ( isKeyGenerationFailure(e) ) debug(runtime, "PKeyRSA could not read private key", e);
293293
else debugStackTrace(runtime, e);
@@ -297,7 +297,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
297297
try { key = readRSAPublicKey(rsaFactory, str.getBytes()); }
298298
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
299299
catch (InvalidKeySpecException e) { debug(runtime, "PKeyRSA could not read public key", e); }
300-
catch (IOException e) { debug(runtime, "PKeyRSA could not read public key", e); }
300+
catch (IOException e) { debugStackTrace(runtime, "PKeyRSA could not read public key", e); }
301301
catch (RuntimeException e) {
302302
if ( isKeyGenerationFailure(e) ) debug(runtime, "PKeyRSA could not read public key", e);
303303
else debugStackTrace(runtime, e);

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ private void doShutdown() throws IOException {
766766
return;
767767
}
768768
catch (RuntimeException e) {
769-
debugStackTrace(getRuntime(), e);
769+
debugStackTrace(getRuntime(), "SSLSocket.doShutdown", e);
770770
return;
771771
}
772772
netData.flip();
@@ -830,7 +830,7 @@ private IRubyObject sysreadImpl(final ThreadContext context,
830830
return buffStr;
831831
}
832832
catch (IOException ex) {
833-
debug(runtime, "SSLSocket.sysreadImpl failed", ex);
833+
debugStackTrace(runtime, "SSLSocket.sysreadImpl", ex);
834834
throw Utils.newError(runtime, runtime.getIOError(), ex);
835835
}
836836
}
@@ -914,7 +914,7 @@ private IRubyObject syswriteImpl(final ThreadContext context,
914914
return runtime.newFixnum(written);
915915
}
916916
catch (IOException ex) {
917-
debug(runtime, "SSLSocket.syswriteImpl failed", ex);
917+
debugStackTrace(runtime, "SSLSocket.syswriteImpl", ex);
918918
throw Utils.newError(runtime, runtime.getIOError(), ex);
919919
}
920920
}

src/main/java/org/jruby/ext/openssl/X509Cert.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ public RubyBoolean verify(final IRubyObject key) {
667667
return runtime.getTrue();
668668
}
669669
catch (CertificateException e) {
670-
debug(runtime, "Certificate#verify failed: ", e);
670+
debugStackTrace(runtime, "Certificate#verify failed", e);
671671
throw newCertificateError(runtime, e);
672672
}
673673
catch (NoSuchAlgorithmException e) {
@@ -679,11 +679,11 @@ public RubyBoolean verify(final IRubyObject key) {
679679
throw newCertificateError(runtime, e);
680680
}
681681
catch (SignatureException e) {
682-
debug(runtime, "Certificate#verify failed: ", e);
682+
debug(runtime, "Certificate#verify failed", e);
683683
return runtime.getFalse();
684684
}
685685
catch (InvalidKeyException e) {
686-
debug(runtime, "Certificate#verify failed: ", e);
686+
debug(runtime, "Certificate#verify failed", e);
687687
return runtime.getFalse();
688688
}
689689
}

src/main/java/org/jruby/ext/openssl/X509Request.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public IRubyObject verify(final ThreadContext context, IRubyObject key) {
351351
return runtime.newBoolean( getRequest().verify(publicKey) );
352352
}
353353
catch (InvalidKeyException e) {
354-
debug(runtime, "X509::Request.verify invalid key", e);
354+
debug(runtime, "X509::Request#verify invalid key", e);
355355
throw newRequestError(runtime, "invalid key supplied", e);
356356
}
357357
//catch (IOException e) {

src/main/java/org/jruby/ext/openssl/x509store/Lookup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public int loadDefaultJavaCACertsFile(String certsFile) throws IOException, Gene
336336
try {
337337
// hardcode the keystore type, as we expect cacerts to be a java keystore
338338
// especially needed since Java 9 (getDefaultType on 11/13 is "pkcs12")
339-
KeyStore keystore = SecurityHelper.getKeyStore("jks");
339+
KeyStore keystore = SecurityHelper.getKeyStore("JKS");
340340
// null password - as the cacerts file isn't password protected
341341
keystore.load(fin, null);
342342
PKIXParameters params = new PKIXParameters(keystore);
@@ -470,7 +470,7 @@ public int call(final Lookup ctx, final Integer cmd, final String argp, final Nu
470470
file = ctx.envEntry(X509_CERT_FILE_EVP); // ENV['SSL_CERT_FILE']
471471
}
472472
catch (RuntimeException e) {
473-
OpenSSL.debug(ctx.runtime, "failed to read env " + X509_CERT_FILE_EVP, e);
473+
OpenSSL.debugStackTrace(ctx.runtime, "failed to read env " + X509_CERT_FILE_EVP, e);
474474
}
475475
if (file == null) {
476476
file = X509_CERT_FILE.replace('/', File.separatorChar);

src/main/java/org/jruby/ext/openssl/x509store/Store.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public int setDefaultPaths(Ruby runtime) throws Exception {
331331
if (!e.getClass().getSimpleName().equals("NotFound")) {
332332
throw e;
333333
}
334-
OpenSSL.debug(runtime, "add X509_CERT_FILER_CTX (to default paths)", e);
334+
OpenSSL.debugStackTrace(runtime, "add X509_CERT_FILER_CTX (to default paths)", e);
335335
}
336336

337337
lookup = addLookup(runtime, Lookup.hashDirLookup());
@@ -346,7 +346,7 @@ public int setDefaultPaths(Ruby runtime) throws Exception {
346346
if (!e.getClass().getSimpleName().equals("NotFound")) {
347347
throw e;
348348
}
349-
OpenSSL.debug(runtime, "add X509_HASH_DIR_CTX (to default paths)", e);
349+
OpenSSL.debugStackTrace(runtime, "add X509_HASH_DIR_CTX (to default paths)", e);
350350
}
351351

352352
X509Error.clearErrors();

0 commit comments

Comments
 (0)