Skip to content

Fix integer overflow in decrypt() and align CLI/docs with constants#11

Merged
T3pp31 merged 2 commits into
mainfrom
claude/code-review-4z0kO
Apr 16, 2026
Merged

Fix integer overflow in decrypt() and align CLI/docs with constants#11
T3pp31 merged 2 commits into
mainfrom
claude/code-review-4z0kO

Conversation

@T3pp31
Copy link
Copy Markdown
Owner

@T3pp31 T3pp31 commented Apr 16, 2026

Summary

This PR addresses three code review findings:

  1. Integer overflow in decrypt(): The function now safely handles shift == i16::MIN by widening to i32 before negation
  2. CLI default shift: Replaced hardcoded "3" with DEFAULT_SHIFT constant reference
  3. Documentation alignment: Updated docstrings to accurately reflect that whitespace-only input is rejected as EmptyText

Key Changes

  • src/caesar_cipher.rs:

    • Modified decrypt() to widen shift to i32 before negation, preventing overflow when shift == i16::MIN
    • Normalized the result using rem_euclid() to ensure the value stays within 0..ALPHABET_SIZE
    • Updated docstrings for encrypt_safe() and decrypt_safe() to document that whitespace-only text is rejected
    • Added implementation note explaining the overflow prevention technique
  • src/cli.rs:

    • Changed #[arg(short, long, default_value = "3")] to #[arg(short, long, default_value_t = DEFAULT_SHIFT)]
    • Ensures CLI default is derived from the constant, eliminating hardcoded magic numbers
  • tests/code_review_fixes_tests.rs (new file):

    • Added 15 comprehensive regression tests covering all three fixes
    • Tests use Given/When/Then comment style for clarity
    • Validates edge cases: i16::MIN, i16::MAX, large negative shifts, and whitespace-only input handling

Implementation Details

The decrypt() fix uses Euclidean remainder to safely normalize the negated shift:

let negated = -(shift as i32);  // Safe: i32 can hold -i16::MIN
let normalized = negated.rem_euclid(ALPHABET_SIZE as i32) as i16;

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

claude added 2 commits April 16, 2026 15:29
- 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 に合わせた。
@T3pp31 T3pp31 merged commit 0eceacb into main Apr 16, 2026
1 check passed
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