Fix integer overflow in decrypt() and align CLI/docs with constants#11
Merged
Conversation
- decrypt() で shift=i16::MIN 時に -shift がオーバーフローする問題を修正
i32 に widen してから rem_euclid で正規化する実装に変更
- CLI --shift の default_value ハードコード ("3") を解消し
config::DEFAULT_SHIFT を default_value_t で参照するよう変更
- encrypt_safe / decrypt_safe のドキュメントを実装と整合させ
空白のみ入力も EmptyText エラーになることを明記
tests/code_review_fixes_tests.rs を追加:
- 境界値 (i16::MIN, i16::MIN+1, i16::MAX, 大きな負値) の panic 非発生
- CLI デフォルト shift が DEFAULT_SHIFT 定数と一致
- encrypt_safe / decrypt_safe の空白のみ拒否挙動
CI の cargo fmt --check 失敗を修正。 use 宣言のアルファベット順整列と長い行の改行位置を rustfmt に合わせた。
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
This PR addresses three code review findings:
decrypt(): The function now safely handlesshift == i16::MINby widening toi32before negation"3"withDEFAULT_SHIFTconstant referenceEmptyTextKey Changes
src/caesar_cipher.rs:decrypt()to widen shift toi32before negation, preventing overflow whenshift == i16::MINrem_euclid()to ensure the value stays within0..ALPHABET_SIZEencrypt_safe()anddecrypt_safe()to document that whitespace-only text is rejectedsrc/cli.rs:#[arg(short, long, default_value = "3")]to#[arg(short, long, default_value_t = DEFAULT_SHIFT)]tests/code_review_fixes_tests.rs(new file):i16::MIN,i16::MAX, large negative shifts, and whitespace-only input handlingImplementation Details
The
decrypt()fix uses Euclidean remainder to safely normalize the negated shift:This ensures the final value is always in
0..26, matching the behavior of the original implementation for all valid inputs while preventing panics on boundary values.https://claude.ai/code/session_012FR4YMg4EWGZ8Nom1p3Sd4