apache / apache/arrow-java

Byte-array elements leak in `FromSchemaByteArray()`

Aberta Para iniciantes
#1,205 0 comentários 0 reações 0 responsáveis Ver no GitHub
Type: bug
Linguagem predominante
Java
Estrelas
94
Forks
152
Merge médio
3d 16h
PRs com merge (30d)
11

Descrição

### Describe the bug, including details regarding any error messages, version, and platform.

I found a possible JNI array leak in `FromSchemaByteArray()` when the serialized schema cannot be parsed.

File: `dataset/src/main/cpp/jni_util.cc`

Function: `FromSchemaByteArray`

Relevant code:

```cpp
jbyte* schemaBytes_data =
env->GetByteArrayElements(schemaBytes, nullptr);
auto serialized_schema = std::make_shared(
reinterpret_cast(schemaBytes_data),
schemaBytes_len);
arrow::io::BufferReader buf_reader(serialized_schema);

ARROW_ASSIGN_OR_RAISE(
std::shared_ptr schema,
arrow::ipc::ReadSchema(&buf_reader, &in_memo))

env->ReleaseByteArrayElements(
schemaBytes, schemaBytes_data, JNI_ABORT);
return schema;
```

`GetByteArrayElements()` returns a pointer that must be paired with
`ReleaseByteArrayElements()`.

`ARROW_ASSIGN_OR_RAISE` returns immediately when `ReadSchema()` returns an
error. On that path, the release below the macro is skipped, so the acquired
array elements remain unreleased:

```text
GetByteArrayElements succeeds
-> ReadSchema returns an error
-> ARROW_ASSIGN_OR_RAISE returns
-> ReleaseByteArrayElements is skipped
```

The function is used by the public native `createDataset()` method:

```cpp
schema = JniGetOrThrow(
FromSchemaByteArray(env, schema_bytes));
```

Malformed, corrupted, or incompatible serialized schema bytes can therefore
reach this path. Repeated failed calls can retain copied array buffers or keep
Java arrays pinned, depending on the JVM implementation.

Suggested fix: release the elements before propagating the parse result, for
example:

```cpp
auto schema_result =
arrow::ipc::ReadSchema(&buf_reader, &in_memo);

env->ReleaseByteArrayElements(
schemaBytes, schemaBytes_data, JNI_ABORT);

return schema_result;
```

An RAII guard for `schemaBytes_data` would also ensure release if additional
early returns are introduced later.

Guia de contribuição

Abrir o guia de contribuição

Direção de pesquisa

Comece em dataset/src/main/cpp/jni_util.cc, em FromSchemaByteArray(), e inspecione o fluxo de GetByteArrayElements, ReadSchema e ReleaseByteArrayElements. Verifique se a liberação também ocorre quando o parsing falha e, em seguida, exercite o caminho de um schema malformado ou incompatível por meio de createDataset(); está concluído quando os elementos do array JNI adquiridos são liberados nos caminhos de sucesso e de erro.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
cpp, java
Domínio
backend
Tipo de issue
Bug
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Pouca atividade
Clareza
Claramente especificada
Facilidade para iniciantes
85/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.