Skip to content

Simple for-loop throws null_pointer_exception in painless #28501

@MrOrz

Description

@MrOrz

Elasticsearch version (bin/elasticsearch --version):
6.1.0 (build hash: c0c1ba0)

Plugins installed:
Using official docker image docker.elastic.co/elasticsearch/elasticsearch:6.1.0

JVM version (java -version):
Using official docker image docker.elastic.co/elasticsearch/elasticsearch:6.1.0

OS version (uname -a if on a Unix-like system):
Docker host machine: OSX 10.11.6

Description of the problem including expected versus actual behavior:

When a painless script contains a for-loop that contains an variable assignment in its first clause, it would cause a compile error and null_pointer_exception.

I.e. This causes null_pointer_exception:

int i; for(i=0; i<3; i+=1) {}

But this works:

for(int i=0; i<3; i+=1) {}

This also works:

int i=0; for(; i<3; i+=1) {}

Steps to reproduce:

  1. Start a blank elasticsearch DB and index an arbitrary document (just to provide a doc to manipulate. Mappings are irrelevant.)
curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
 "foo": "bar"
}
'
  1. Invoke a update query with for-loop
curl -POST 'localhost:9200/my_index/my_type/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "int i; for(i=0; i<3; i+=1) {}\n ctx.op = \"none\"",
    "lang": "painless"
  }
}
'

Expected

Successful response with no-op like:

{
  "_index" : "my_index",
  "_type" : "my_type",
  "_id" : "1",
  "_version" : 1,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}

Actual
Status-400 response with "compile error" and "null_pointer_exception":

{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[hbJSTB8][172.17.0.2:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [ ],
      "script" : "int i; for(i=0; i<3; i+=1) {}\n ctx.op = \"none\"",
      "lang" : "painless",
      "caused_by" : {
        "type" : "null_pointer_exception",
        "reason" : null
      }
    }
  },
  "status" : 400
}

Possibly relates to #24695

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions