fix: RailFenceCipher silently drops newline characters - #7572
Merged
DenizAltunkapan merged 1 commit intoAug 15, 2026
Merged
Conversation
SEPURI-SAI-KRISHNA
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
August 15, 2026 16:26
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7572 +/- ##
=========================================
Coverage 80.42% 80.43%
- Complexity 7460 7461 +1
=========================================
Files 815 815
Lines 24056 24057 +1
Branches 4733 4734 +1
=========================================
+ Hits 19348 19351 +3
+ Misses 3945 3944 -1
+ Partials 763 762 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
approved these changes
Aug 15, 2026
DenizAltunkapan
left a comment
Member
There was a problem hiding this comment.
@SEPURI-SAI-KRISHNA thanks
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.
Problem
RailFenceCipher.encryptbuilds arails x lengthmatrix and fills every cell with'\n'to mark it as unused. When reading the matrix back it skips every cell that still holds'\n':The placeholder is indistinguishable from a real newline in the plaintext, so any
'\n'in the input is silently discarded. The ciphertext comes out shorter than the plaintext and the round trip is corrupted:A transposition cipher must be a permutation of its input, so dropping characters is a correctness bug rather than a formatting quirk.
Fix
encryptnow collects each rail into its ownStringBuilderinstead of writing into a matrix that needs a sentinel value. Every character of the input is appended exactly once and read back in rail order, so no character can be mistaken for a blank cell. This also drops the memory use fromO(rails * length)toO(length).Additionally,
encryptanddecryptnow rejectnullinput and non-positive rail counts withIllegalArgumentException. Previouslyencrypt("abc", 0)threwArrayIndexOutOfBoundsExceptionandencrypt("abc", -1)threwNegativeArraySizeException, both leaking internal implementation details.decryptis unchanged: its'*'marker is written and consumed positionally, so a'*'in the ciphertext was never at risk.Tests
The class had no test class at all.
RailFenceCipherTestis added, covering:the classic
WEAREDISCOVEREDFLEEATONCE/ 3 rails vector, in both directionsround trips over several messages and rail counts
control characters (
\n,\r,\t) surviving encryption for 2..5 rails — this fails on the old codedegenerate rail counts (
rails == 1,rails >= length, empty input)the new validation for non-positive rail counts and
nullinputI have read CONTRIBUTING.md.
This pull request is all my own work -- I have not plagiarized it.
All filenames are in PascalCase.
All functions and variable names follow Java naming conventions.
All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
All new algorithms include a corresponding test class that validates their functionality.
All new code is formatted with
clang-format -i --style=file path/to/your/file.java