Fix Two Common Zero Len Array Instantiations (#58944) (#58993)

Two spots I found in which we commonly instatiate a non-trivial number of zero length arrays.
This commit is contained in:
Armin Braun 2020-07-03 09:18:14 +02:00 committed by GitHub
parent df555bb470
commit d22dd437f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -87,9 +87,11 @@ public class ReplicationResponse extends ActionResponse {
total = in.readVInt();
successful = in.readVInt();
int size = in.readVInt();
failures = new Failure[size];
for (int i = 0; i < size; i++) {
failures[i] = new Failure(in);
if (size > 0) {
failures = new Failure[size];
for (int i = 0; i < size; i++) {
failures[i] = new Failure(in);
}
}
}
@ -223,9 +225,9 @@ public class ReplicationResponse extends ActionResponse {
private static final String STATUS = "status";
private static final String PRIMARY = "primary";
private ShardId shardId;
private String nodeId;
private boolean primary;
private final ShardId shardId;
private final String nodeId;
private final boolean primary;
public Failure(StreamInput in) throws IOException {
shardId = new ShardId(in);
@ -244,9 +246,6 @@ public class ReplicationResponse extends ActionResponse {
this.primary = primary;
}
Failure() {
}
public ShardId fullShardId() {
return shardId;
}

View File

@ -306,9 +306,15 @@ public class Lucene {
TotalHits totalHits = readTotalHits(in);
float maxScore = in.readFloat();
ScoreDoc[] scoreDocs = new ScoreDoc[in.readVInt()];
for (int i = 0; i < scoreDocs.length; i++) {
scoreDocs[i] = new ScoreDoc(in.readVInt(), in.readFloat());
final int scoreDocCount = in.readVInt();
final ScoreDoc[] scoreDocs;
if (scoreDocCount == 0) {
scoreDocs = EMPTY_SCORE_DOCS;
} else {
scoreDocs = new ScoreDoc[scoreDocCount];
for (int i = 0; i < scoreDocs.length; i++) {
scoreDocs[i] = new ScoreDoc(in.readVInt(), in.readFloat());
}
}
return new TopDocsAndMaxScore(new TopDocs(totalHits, scoreDocs), maxScore);
} else if (type == 1) {