[ 
https://issues.apache.org/jira/browse/FLINK-39687?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

wangzhao updated FLINK-39687:
-----------------------------
    Description: 
h2. Location

{{flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/context/SessionContext.java}}
 line 306
h2. Faulty Code
{code:java}
defaultContext.getDependencies().toArray(new URL[0]) {code}
h2. Root Cause

{{DefaultContext.getDependencies()}} returns {{List<URI>}} 
({{{}java.net.URI{}}}), but the code calls {{{}.toArray(new URL[0]){}}}.

{{java.net.URI}} and {{java.net.URL}} are *completely unrelated classes* (no 
inheritance relationship), so a {{URL[]}} array cannot hold {{URI}} objects.

Java's {{List.toArray(T[])}} generic method does not check element type 
compatibility at compile time, so this compiles successfully. However, it will 
*always throw {{ArrayStoreException}} at runtime* when the {{toArray}} 
implementation attempts to store a {{URI}} into a {{{}URL[]{}}}.
h2. When It Triggers

This bug triggers when {{defaultContext.getDependencies()}} returns a non-empty 
list. It currently goes unnoticed because:
 * {{DefaultContext.load()}} creates a DefaultContext with an empty dependency 
list (the {{dependencies}} parameter is always empty in current callers)
 * {{ScriptRunner}} passes {{Collections.emptyList()}} directly (empty array, 
{{toArray}} never needs to store elements, so no exception)

Once anyone creates a DefaultContext with non-empty dependencies, the exception 
will fire.
h2. Fix

Convert each {{URI}} to {{URL}} individually:
{code:java}
defaultContext.getDependencies().stream()
    .map(uri -> uri.toURL())
    .toArray(URL[]::new) {code}
h2. Introduced In

FLINK 2.2.0 (commit: 5a336892424)

  was:
## Location

`flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/context/SessionContext.java`
 line 306

## Faulty Code

```java
defaultContext.getDependencies().toArray(new URL[0])
```

## Root Cause

`DefaultContext.getDependencies()` returns `List<URI>` (`java.net.URI`), but 
the code calls `.toArray(new URL[0])`.

`java.net.URI` and `java.net.URL` are **completely unrelated classes** (no 
inheritance relationship), so a `URL[]` array cannot hold `URI` objects.

Java's `List.toArray(T[])` generic method does not check element type 
compatibility at compile time, so this compiles successfully. However, it will 
**always throw `ArrayStoreException` at runtime** when the `toArray` 
implementation attempts to store a `URI` into a `URL[]`.

## When It Triggers

This bug triggers when `defaultContext.getDependencies()` returns a non-empty 
list. It currently goes unnoticed because:

- `DefaultContext.load()` creates a DefaultContext with an empty dependency 
list (the `dependencies` parameter is always empty in current callers)
- `ScriptRunner` passes `Collections.emptyList()` directly (empty array, 
`toArray` never needs to store elements, so no exception)

Once anyone creates a DefaultContext with non-empty dependencies, the exception 
will fire.

## Fix

Convert each `URI` to `URL` individually:

```java
defaultContext.getDependencies().stream()
.map(uri -> uri.toURL())
.toArray(URL[]::new)
```

## Introduced In

FLINK 2.2.0 (commit: 5a336892424)


> URI used as URL in SessionContext causes ArrayStoreException
> ------------------------------------------------------------
>
>                 Key: FLINK-39687
>                 URL: https://issues.apache.org/jira/browse/FLINK-39687
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Gateway
>            Reporter: wangzhao
>            Priority: Major
>
> h2. Location
> {{flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/context/SessionContext.java}}
>  line 306
> h2. Faulty Code
> {code:java}
> defaultContext.getDependencies().toArray(new URL[0]) {code}
> h2. Root Cause
> {{DefaultContext.getDependencies()}} returns {{List<URI>}} 
> ({{{}java.net.URI{}}}), but the code calls {{{}.toArray(new URL[0]){}}}.
> {{java.net.URI}} and {{java.net.URL}} are *completely unrelated classes* (no 
> inheritance relationship), so a {{URL[]}} array cannot hold {{URI}} objects.
> Java's {{List.toArray(T[])}} generic method does not check element type 
> compatibility at compile time, so this compiles successfully. However, it 
> will *always throw {{ArrayStoreException}} at runtime* when the {{toArray}} 
> implementation attempts to store a {{URI}} into a {{{}URL[]{}}}.
> h2. When It Triggers
> This bug triggers when {{defaultContext.getDependencies()}} returns a 
> non-empty list. It currently goes unnoticed because:
>  * {{DefaultContext.load()}} creates a DefaultContext with an empty 
> dependency list (the {{dependencies}} parameter is always empty in current 
> callers)
>  * {{ScriptRunner}} passes {{Collections.emptyList()}} directly (empty array, 
> {{toArray}} never needs to store elements, so no exception)
> Once anyone creates a DefaultContext with non-empty dependencies, the 
> exception will fire.
> h2. Fix
> Convert each {{URI}} to {{URL}} individually:
> {code:java}
> defaultContext.getDependencies().stream()
>     .map(uri -> uri.toURL())
>     .toArray(URL[]::new) {code}
> h2. Introduced In
> FLINK 2.2.0 (commit: 5a336892424)



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to