Skip to content

Commit

Permalink
Fix weird/invalid conversion to short before passing to native (#719)
Browse files Browse the repository at this point in the history
Co-authored-by: Bret Ambrose <[email protected]>
  • Loading branch information
bretambrose and Bret Ambrose authored Nov 14, 2023
1 parent b023ab4 commit 977479a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
public class MqttClientConnection extends CrtResource {

private static final int MAX_PORT = 65535;

private MqttConnectionConfig config;

private AsyncCallback connectAck;
Expand Down Expand Up @@ -95,7 +97,7 @@ public MqttClientConnection(MqttConnectionConfig config) throws MqttException {
if (config.getEndpoint() == null) {
throw new MqttException("endpoint must not be null");
}
if (config.getPort() <= 0 || config.getPort() > 65535) {
if (config.getPort() <= 0 || config.getPort() > MAX_PORT) {
throw new MqttException("port must be a positive integer between 1 and 65535");
}

Expand Down Expand Up @@ -136,7 +138,7 @@ public MqttClientConnection(Mqtt5Client mqtt5client, MqttClientConnectionEvents
if (config.getEndpoint() == null) {
throw new MqttException("endpoint must not be null");
}
if (config.getPort() <= 0 || config.getPort() > 65535) {
if (config.getPort() <= 0 || config.getPort() > MAX_PORT) {
throw new MqttException("port must be a positive integer between 1 and 65535");
}

Expand Down Expand Up @@ -310,9 +312,9 @@ public CompletableFuture<Boolean> connect() throws MqttException {
// Just clamp the pingTimeout, no point in throwing
short pingTimeout = (short) Math.max(0, Math.min(config.getPingTimeoutMs(), Short.MAX_VALUE));

short port = (short) config.getPort();
if (port > Short.MAX_VALUE || port <= 0) {
throw new MqttException("Port must be betweeen 0 and " + Short.MAX_VALUE);
int port = config.getPort();
if (port > MAX_PORT || port <= 0) {
throw new MqttException("Port must be betweeen 0 and 65535");
}
CompletableFuture<Boolean> future = new CompletableFuture<>();
connectAck = AsyncCallback.wrapFuture(future, null);
Expand Down Expand Up @@ -487,7 +489,7 @@ private static native long mqttClientConnectionNewFrom5Client(long client, MqttC

private static native void mqttClientConnectionDestroy(long connection);

private static native void mqttClientConnectionConnect(long connection, String endpoint, short port,
private static native void mqttClientConnectionConnect(long connection, String endpoint, int port,
long socketOptions, long tlsContext, String clientId, boolean cleanSession, int keepAliveMs,
short pingTimeoutMs, int protocolOperationTimeoutMs) throws CrtRuntimeException;

Expand Down
4 changes: 2 additions & 2 deletions src/native/mqtt_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ void JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClientConnection_mqttClien
jclass jni_class,
jlong jni_connection,
jstring jni_endpoint,
jshort jni_port,
jint jni_port,
jlong jni_socket_options,
jlong jni_tls_ctx,
jstring jni_client_id,
Expand All @@ -594,7 +594,7 @@ void JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClientConnection_mqttClien
struct aws_byte_cursor client_id;
AWS_ZERO_STRUCT(client_id);
struct aws_byte_cursor endpoint = aws_jni_byte_cursor_from_jstring_acquire(env, jni_endpoint);
uint16_t port = jni_port;
uint16_t port = (uint16_t)jni_port;
if (!port) {
aws_jni_throw_runtime_exception(
env,
Expand Down

0 comments on commit 977479a

Please sign in to comment.