Skip to content

Commit

Permalink
Increase errno cache and guard against IOOBE (#13254)
Browse files Browse the repository at this point in the history
Motivation:

In the past we cached up to 512 errno values and used this cache to lookup the string representation without checking if the value if > 512. This value might be a bit low as more errno values are added over the time.

Modifications:

- Cache up to errno code 2048
- Use JNI call if errno code was not cached and so not throw an IOOBE.

Result:

No more exceptions on un-cached errno codes
  • Loading branch information
normanmaurer committed Feb 28, 2023
1 parent eb3feb4 commit 5d1f996
Showing 1 changed file with 14 additions and 5 deletions.
Expand Up @@ -64,9 +64,10 @@ public final class Errors {
* This eliminates the need to call back into JNI to get the right String message on an exception
* and thus is faster.
*
* The array length of 512 should be more then enough because errno.h only holds < 200 codes.
* Choose an array length which should give us enough space in the future even when more errno codes
* will be added.
*/
private static final String[] ERRORS = new String[512];
private static final String[] ERRORS = new String[2048];

/**
* <strong>Internal usage only!</strong>
Expand All @@ -81,7 +82,7 @@ public NativeIoException(String method, int expectedErr) {
}

public NativeIoException(String method, int expectedErr, boolean fillInStackTrace) {
super(method + "(..) failed: " + ERRORS[-expectedErr]);
super(method + "(..) failed: " + errnoString(-expectedErr));
this.expectedErr = expectedErr;
this.fillInStackTrace = fillInStackTrace;
}
Expand All @@ -103,7 +104,7 @@ static final class NativeConnectException extends ConnectException {
private static final long serialVersionUID = -5532328671712318161L;
private final int expectedErr;
NativeConnectException(String method, int expectedErr) {
super(method + "(..) failed: " + ERRORS[-expectedErr]);
super(method + "(..) failed: " + errnoString(-expectedErr));
this.expectedErr = expectedErr;
}

Expand Down Expand Up @@ -142,6 +143,14 @@ public static void throwConnectException(String method, int err) throws IOExcept
throw newConnectException0(method, err);
}

private static String errnoString(int err) {
// Check first if we had it cached, if not we need to do a JNI call.
if (err < ERRORS.length - 1) {
return ERRORS[err];
}
return strError(err);
}

private static IOException newConnectException0(String method, int err) {
if (err == ERROR_ENETUNREACH_NEGATIVE) {
return new NoRouteToHostException();
Expand All @@ -152,7 +161,7 @@ private static IOException newConnectException0(String method, int err) {
if (err == ERRNO_ENOENT_NEGATIVE) {
return new FileNotFoundException();
}
return new ConnectException(method + "(..) failed: " + ERRORS[-err]);
return new ConnectException(method + "(..) failed: " + errnoString(-err));
}

public static NativeIoException newConnectionResetException(String method, int errnoNegative) {
Expand Down

0 comments on commit 5d1f996

Please sign in to comment.