Skip to content

Commit

Permalink
Fix painless casting bug causing opensearch to crash (#8315) (#8607)
Browse files Browse the repository at this point in the history
* Created a failing test to reproduce painless bug

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>

* Removed unused import

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>

* Throw exception when trying to cast def to void

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>

* Removed update context change

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>

* Created a different test

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>

---------

Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>
(cherry picked from commit a39f60f)
  • Loading branch information
shiv0408 committed Jul 11, 2023
1 parent 7c12f80 commit bab8cca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Refactor] OpenSearchException streamables to a registry ([#7646](https://github.com/opensearch-project/OpenSearch/pull/7646))
- [Refactor] Serverless common classes to libraries ([#8065](https://github.com/opensearch-project/OpenSearch/pull/8065))
- [Refactor] StreamIO and OpenSearchException foundation to core library ([#8035](https://github.com/opensearch-project/OpenSearch/pull/8035))
- Fix painless casting bug, which crashes the OpenSearch process ([#8315](https://github.com/opensearch-project/OpenSearch/pull/8315))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static PainlessCast getLegalCast(Location location, Class<?> actual, Clas
}
}

if (actual == def.class
if ((actual == def.class && expected != void.class)
|| (actual != void.class && expected == def.class)
|| expected.isAssignableFrom(actual)
|| (actual.isAssignableFrom(expected) && explicit)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ public void testVoidReturn() {
assertEquals(iae.getMessage(), "not a statement: result not used from addition operation [+]");
}

public void testDefToVoidReturnThrowsException() {
ClassCastException exception = expectScriptThrows(
ClassCastException.class,
() -> getEngine().compile("def_return_in_void", "def x=1;return x;", VoidReturnTestScript.CONTEXT, Collections.emptyMap())
);
assertEquals(exception.getMessage(), "Cannot cast from [def] to [void].");
}

public abstract static class FactoryTestConverterScript {
private final Map<String, Object> params;

Expand Down

0 comments on commit bab8cca

Please sign in to comment.