Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Passing same buffer as two method arguments does not read back modified content

オープン
#1,272 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
java
領域
tooling

調査の方向性

エントリポイント do_something(ByteBuffer out, ByteBuffer in) に対する JNA の呼び出し経路と、ヒープによりバッキングされた ByteBuffer 引数の処理から始めます。JNA 5.5.0 で同じバッファを両方のパラメータに渡して呼び出しを再現し、ネイティブ関数から戻った後も変更された出力が可視であることを確認します。

索引モデルが issue の本文から書いたものです。

説明

feature request

Assume the following native library function, which reads from in and writes to out:

int do_something(void *out, void *in);

mapped to the following interface in Jave:

public interface mylibrary extends Library {
	public int do_something(ByteBuffer out, ByteBuffer in);
}

When calling this function with the same buffer backed by a heap-allocated array as both output and input argument, i.e.:

public void process(ByteBuf buffer) {
    // buffer is backed by heap-allocated array
    mylibraryInstance.do_something(buffer, buffer);
}

then the buffer will not be updated to reflect the content that was written to out within the native function implementation.

I assume this is because each argument will be processed individually, the buffer will be copied two times into native memory, both instances will be read back after the function call and the second argument (in, unmodified) replaces the first argument (out, modified & expected content).

Workaround: Create a new buffer before calling the native function (and write it back afterwards):

public void process(ByteBuf buffer) {
    ByteBuffer outputBuffer = ByteBuffer.allocate(buffer.remaining());
    outputBuffer.limit(buffer.remaining());
    mylibraryInstance.do_something(outputBuffer, buffer);
    // here, outputBuffer has the expected (modified) content - write back to original buffer
}

Version of JNA and related jars: jna 5.5.0 and jna-platform 5.5.0
Version and vendor of the java virtual machine: Oracle JDK 12.0.2

主要言語
Java
スター
8.9k
フォーク
1.7k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

java-native-access/jna のほかの issue

java-native-access/jna の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。