fix: EgovStringUtil.search 빈 target 무한 루프 방지#245
Open
z3rotig4r wants to merge 1 commit into
Open
Conversation
search(source, target)에서 target이 빈 문자열이면 indexOf("")가
항상 0을 반환하고, 루프 인덱스 i가 0에 고정되며 strCheck도 줄어들지
않아 for 루프가 끝나지 않습니다(source가 비어 있지 않은 경우).
공개 정적 유틸이라 호출 측 입력에 따라 CPU 점유·스레드 행으로
이어질 수 있습니다. 또한 source나 target이 null이면 NullPointerException이
발생합니다.
source 또는 target이 null이거나 빈 문자열이면 셀 대상이 없으므로
0을 반환하도록 진입부 가드를 추가했습니다(commons-lang
StringUtils.countMatches와 동일한 규약).
빈 target 무한 루프 방지(타임아웃), null 입력, 정상 카운트를 포함한
단위 테스트 3건을 추가했습니다.
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.
문제
EgovStringUtil.search(String source, String target)는source안에서target이 나타나는 횟수를 셉니다.target이 빈 문자열("")이면strCheck.indexOf("")가 항상0을 반환합니다. 이때i = loc + target.length()가0으로 고정되고strCheck = strCheck.substring(0)로 문자열이 줄어들지 않아,source가 비어 있지 않은 한for루프가 종료되지 않습니다. 공개 정적 유틸이라 호출 측 입력에 따라 CPU 점유·스레드 행으로 이어질 수 있습니다(CWE-835).또한
source나target이null이면 각각source.length(),indexOf(null)에서NullPointerException이 발생합니다.수정
진입부에 가드를 추가했습니다.
source또는target이null이거나 빈 문자열이면 셀 대상이 없으므로0을 반환합니다. 이는 Apache Commons LangStringUtils.countMatches와 동일한 규약입니다.기존의 정상 카운트 동작은 그대로 유지됩니다.
검증
EgovStringUtilTest에 테스트 3건을 추가했습니다.search("aXbXc", "X") == 2등)target무한 루프 방지(assertTimeoutPreemptively로 2초 내0반환 확인)null/빈 입력이 예외 없이0반환