fix: strip trailing spaces and NULs when setting environment variables from COBOL#827
Draft
ytr-sakamoto wants to merge 2 commits intoopensourcecobol:developfrom
Draft
Conversation
…メントを追加 - 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) に、環境変数名側と 値側でトリムの範囲が異なる理由をコメントで説明
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where
DISPLAY ... UPON ENVIRONMENT-VALUEandSET ENVIRONMENT x TO yleaked trailing space padding from the sourcePIC X(n)field into the environment variable value. When such an env var was later resolved as a filename viaSELECT ... 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 callingsetenv. The Java port did not match this behavior.Changes
AbstractCobolField#fieldToString(Charset)— a shared helper that strips trailing spaces/NULs (matching libcob'scob_field_to_string) and decodes with an explicit charset.CobolTerminal#displayEnvValuenow uses the new helper with SJIS, soDISPLAY ... UPON ENVIRONMENT-VALUEstores a trimmed value.CobolUtil#setEnv(AbstractCobolField, AbstractCobolField)(invoked bySET ENVIRONMENT x TO y) now strips trailing spaces/NULs from the value as well. Javadoc and a code comment explain why the name side usesString#trim()while the value side strips only trailing padding.tests/jp-compat.src/special-names.atcovering both theDISPLAY ... UPON ENVIRONMENT-VALUEandSET ENVIRONMENTpaths viaASSIGN-based file open.Reproducer
Before this fix, a COBOL program like the following:
would set the env var
MYPATHto/tmp/out.txtfollowed by ~244 spaces, so anySELECT ... ASSIGN MYPATHfile open created a file whose name literally contained those trailing spaces.Test plan
./gradlew testpasses locally.SET ENVIRONMENTregression 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-VALUEとSET ENVIRONMENTの両方のパスについて、ASSIGN経由のファイルオープンで回帰が起きないことを確認する統合テストを追加。再現コード
本修正前は、以下のようなCOBOLプログラム:
で、環境変数
MYPATHの値が/tmp/out.txtの後に約244文字の空白が続いた文字列となり、SELECT ... ASSIGN MYPATHでのファイルオープンが末尾空白込みのファイル名でファイルを作成していました。テスト計画
./gradlew testローカルでPASSSET ENVIRONMENT回帰テスト・Javadoc追記のフォローアップコミットについてCIグリーン