Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase errno cache and guard against IOOBE #13254

Merged
merged 1 commit into from Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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