Skip to content

Commit e328bca

Browse files
committed
fix the bug
2 parents 7aec300 + 93e4247 commit e328bca

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/native/crt.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ jobject aws_jni_direct_byte_buffer_from_raw_ptr(JNIEnv *env, const void *dst, si
438438
if (jByteBuf) {
439439
aws_jni_byte_buffer_set_limit(env, jByteBuf, (jint)capacity);
440440
aws_jni_byte_buffer_set_position(env, jByteBuf, 0);
441+
} else{
442+
if (aws_jni_check_and_clear_exception(env)) {
443+
result = aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
444+
(*env)->DeleteLocalRef(env, direct_buffer);
445+
}
441446
}
442447

443448
return jByteBuf;

src/native/http_request_utils.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,37 @@ static int s_aws_input_stream_read(struct aws_input_stream *stream, struct aws_b
9494
}
9595

9696
size_t out_remaining = dest->capacity - dest->len;
97+
int result = AWS_OP_SUCCESS;
98+
/* Newer updates allow part sizes up to 5GB. Since number of bytes required for a 5GB part is
99+
greater than INT_MAX, it would cause a bug where the java does not allocate memory and return a null buffer
100+
since Java natively does not support direct allocation of buffers of capacity > Integer.MAX_VALUE. */
101+
while(out_remaining > 0) {
102+
size_t chunk_size = INT_MAX;
103+
if (out_remaining <= chunk_size) {
104+
chunk_size = out_remaining;
105+
}
97106

98-
jobject direct_buffer = aws_jni_direct_byte_buffer_from_raw_ptr(env, dest->buffer + dest->len, out_remaining);
107+
jobject direct_buffer = aws_jni_direct_byte_buffer_from_raw_ptr(env, dest->buffer + dest->len, chunk_size);
99108

100-
impl->body_done = (*env)->CallBooleanMethod(
101-
env, impl->http_request_body_stream, http_request_body_stream_properties.send_outgoing_body, direct_buffer);
109+
impl->body_done = (*env)->CallBooleanMethod(
110+
env, impl->http_request_body_stream, http_request_body_stream_properties.send_outgoing_body, direct_buffer);
111+
112+
if (aws_jni_check_and_clear_exception(env)) {
113+
result = aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
114+
(*env)->DeleteLocalRef(env, direct_buffer);
115+
break;
116+
}
102117

103-
int result = AWS_OP_SUCCESS;
104-
if (aws_jni_check_and_clear_exception(env)) {
105-
result = aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
106-
} else {
107118
size_t amt_written = aws_jni_byte_buffer_get_position(env, direct_buffer);
119+
if(amt_written == 0) {
120+
(*env)->DeleteLocalRef(env, direct_buffer);
121+
break;
122+
}
108123
dest->len += amt_written;
109-
}
124+
out_remaining -= amt_written;
110125

111-
(*env)->DeleteLocalRef(env, direct_buffer);
126+
(*env)->DeleteLocalRef(env, direct_buffer);
127+
}
112128

113129
aws_jni_release_thread_env(impl->jvm, &jvm_env_context);
114130
/********** JNI ENV RELEASE **********/

0 commit comments

Comments
 (0)