Skip to content

fix: strip trailing spaces and NULs when setting environment variables from COBOL#827

Draft
ytr-sakamoto wants to merge 2 commits intoopensourcecobol:developfrom
yutaro-sakamoto:fix/trim-space-in-env-var
Draft

fix: strip trailing spaces and NULs when setting environment variables from COBOL#827
ytr-sakamoto wants to merge 2 commits intoopensourcecobol:developfrom
yutaro-sakamoto:fix/trim-space-in-env-var

Conversation

@ytr-sakamoto
Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where DISPLAY ... UPON ENVIRONMENT-VALUE and SET ENVIRONMENT x TO y leaked trailing space padding from the source PIC X(n) field into the environment variable value. When such an env var was later resolved as a filename via SELECT ... ASSIGN, the file was created with trailing spaces embedded in its name.

The C original (opensource-cobol) feeds these values through cob_field_to_string, which strips trailing spaces and NUL bytes before calling setenv. The Java port did not match this behavior.

Changes

  • Added AbstractCobolField#fieldToString(Charset) — a shared helper that strips trailing spaces/NULs (matching libcob's cob_field_to_string) and decodes with an explicit charset.
  • CobolTerminal#displayEnvValue now uses the new helper with SJIS, so DISPLAY ... UPON ENVIRONMENT-VALUE stores a trimmed value.
  • CobolUtil#setEnv(AbstractCobolField, AbstractCobolField) (invoked by SET ENVIRONMENT x TO y) now strips trailing spaces/NULs from the value as well. Javadoc and a code comment explain why the name side uses String#trim() while the value side strips only trailing padding.
  • Added regression tests in tests/jp-compat.src/special-names.at covering both the DISPLAY ... UPON ENVIRONMENT-VALUE and SET ENVIRONMENT paths via ASSIGN-based file open.

Reproducer

Before this fix, a COBOL program like the following:

01 WK-PATH PIC X(256).
...
MOVE SPACE TO WK-PATH.
STRING '/tmp/out.txt' DELIMITED BY SIZE INTO WK-PATH.
DISPLAY 'MYPATH' UPON ENVIRONMENT-NAME.
DISPLAY WK-PATH  UPON ENVIRONMENT-VALUE.

would set the env var MYPATH to /tmp/out.txt followed by ~244 spaces, so any SELECT ... ASSIGN MYPATH file open created a file whose name literally contained those trailing spaces.

Test plan

  • Local reproducer confirmed the bug before the fix and its disappearance after the fix.
  • ./gradlew test passes locally.
  • Integration tests green on CI for the fix commit.
  • CI green for the follow-up commit that adds the SET ENVIRONMENT regression test and Javadoc clarifications.

概要 (日本語訳)

DISPLAY ... UPON ENVIRONMENT-VALUE および SET ENVIRONMENT x TO y において、ソース側の PIC X(n) フィールドの末尾空白がそのまま環境変数値に流れ込んでいたバグの修正です。その結果、SELECT ... ASSIGN 経由で環境変数をファイル名として解決すると、末尾空白を含んだファイル名でファイルが作成されてしまっていました。

本家C実装の opensource-cobol は cob_field_to_string を介して末尾の空白およびNULバイトを削ぎ落としてから setenv を呼び出しています。Java版はこの挙動に追従できていませんでした。

変更内容

  • AbstractCobolField#fieldToString(Charset) を新規追加。末尾の空白/NULを削ぎ落として指定charsetでデコードする共通ヘルパーです (libcob の cob_field_to_string と同等の挙動)。
  • CobolTerminal#displayEnvValue を上記ヘルパー (SJIS指定) 経由に変更。DISPLAY ... UPON ENVIRONMENT-VALUE は末尾トリム済みの値を格納するようになります。
  • CobolUtil#setEnv(AbstractCobolField, AbstractCobolField) (SET ENVIRONMENT x TO y から呼び出される) でも値側の末尾空白/NULをトリムするように変更。環境変数名側が String#trim() (先頭/末尾双方) で値側が末尾のみという非対称性の理由を Javadoc とコード内コメントで明記。
  • tests/jp-compat.src/special-names.at に、DISPLAY ... UPON ENVIRONMENT-VALUESET ENVIRONMENT の両方のパスについて、ASSIGN 経由のファイルオープンで回帰が起きないことを確認する統合テストを追加。

再現コード

本修正前は、以下のようなCOBOLプログラム:

01 WK-PATH PIC X(256).
...
MOVE SPACE TO WK-PATH.
STRING '/tmp/out.txt' DELIMITED BY SIZE INTO WK-PATH.
DISPLAY 'MYPATH' UPON ENVIRONMENT-NAME.
DISPLAY WK-PATH  UPON ENVIRONMENT-VALUE.

で、環境変数 MYPATH の値が /tmp/out.txt の後に約244文字の空白が続いた文字列となり、SELECT ... ASSIGN MYPATH でのファイルオープンが末尾空白込みのファイル名でファイルを作成していました。

テスト計画

  • ローカル再現コードで修正前にバグが発生し、修正後に解消することを確認
  • ./gradlew test ローカルでPASS
  • 修正本体のコミットについて統合テストCIグリーン
  • SET ENVIRONMENT 回帰テスト・Javadoc追記のフォローアップコミットについてCIグリーン

…メントを追加

- tests/jp-compat.src/special-names.at に SET ENVIRONMENT x TO y 経由でも
  PIC X(n) フィールドの末尾空白がトリムされることを検証する統合テストを追加
- CobolTerminal#displayEnvValue のJavadocに、末尾空白/NULが削除される挙動と
  本家CのopensourceCOBOLの cob_display_env_value との対応を明記
- CobolUtil#setEnv(AbstractCobolField, AbstractCobolField) に、環境変数名側と
  値側でトリムの範囲が異なる理由をコメントで説明
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants