Skip to content

XContentBuilder.field : StackOverflowError with Integer[] parameters #1324

@mstein

Description

@mstein

After upgrading to 0.17.x from a 0.15.x version in a grails project, I've encountered a StackOverflowError when trying to add a Integer array (Integer[]) using the field() method.

Code sample to reproduce (groovy) :

  def json = jsonBuilder().startObject()
  Integer[] listofInteger = [1, 2, 3, 4]
  json.field("test2", listofInteger)
  json.endObject()

Error returned :

Caused by: java.lang.StackOverflowError
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:568)
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:601)
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:601)
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:601)
   (...)

It will also occured when using a Float[] or Double[].
Building the array manually with the array() method will not trigger the error :

  def json = jsonBuilder().startObject()
  Integer[] listofInteger = [1, 2, 3, 4]
  json.startArray("test2")
  for (Object o : listofInteger) {
      json.value(o);
  }
  json.endArray()
  json.endObject()

The line https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L631 is probably the reason of the issue since an Integer[] will be recognized as a Object[], and will just infinitely call the field(String, Object) method.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions