Skip to content

Commit 63839d3

Browse files
committed
Thrift: Response might get corrupted with extra data, closes elastic#452.
1 parent c7e6c57 commit 63839d3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

plugins/transport/thrift/src/main/java/org/elasticsearch/thrift/ThriftRestImpl.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ public class ThriftRestImpl extends AbstractComponent implements Rest.Iface {
4949
logger.trace("thrift message {}", request);
5050
}
5151
final CountDownLatch latch = new CountDownLatch(1);
52-
final AtomicReference<RestResponse> ref = new AtomicReference<RestResponse>();
52+
final AtomicReference<org.elasticsearch.thrift.RestResponse> ref = new AtomicReference<org.elasticsearch.thrift.RestResponse>();
5353
restController.dispatchRequest(new ThriftRestRequest(request), new RestChannel() {
5454
@Override public void sendResponse(RestResponse response) {
55-
ref.set(response);
55+
try {
56+
ref.set(convert(response));
57+
} catch (IOException e) {
58+
// ignore, should not happen...
59+
}
5660
latch.countDown();
5761
}
5862
});
5963
try {
6064
latch.await();
61-
return convert(ref.get());
65+
return ref.get();
6266
} catch (Exception e) {
6367
throw new TException("failed to generate response", e);
6468
}
@@ -67,7 +71,14 @@ public class ThriftRestImpl extends AbstractComponent implements Rest.Iface {
6771
private org.elasticsearch.thrift.RestResponse convert(RestResponse response) throws IOException {
6872
org.elasticsearch.thrift.RestResponse tResponse = new org.elasticsearch.thrift.RestResponse(getStatus(response.status()));
6973
if (response.contentLength() > 0) {
70-
tResponse.setBody(ByteBuffer.wrap(response.content(), 0, response.contentLength()));
74+
if (response.contentThreadSafe()) {
75+
tResponse.setBody(ByteBuffer.wrap(response.content(), 0, response.contentLength()));
76+
} else {
77+
// argh!, we need to copy it over since we are not on the same thread...
78+
byte[] body = new byte[response.contentLength()];
79+
System.arraycopy(response.content(), 0, body, 0, response.contentLength());
80+
tResponse.setBody(ByteBuffer.wrap(body));
81+
}
7182
}
7283
return tResponse;
7384
}

0 commit comments

Comments
 (0)