[fix] EgovFlatFileByteReader 짧은 마지막 레코드에서 이전 레코드 잔여 바이트 혼입 수정#274
Open
dasomel wants to merge 1 commit into
Open
[fix] EgovFlatFileByteReader 짧은 마지막 레코드에서 이전 레코드 잔여 바이트 혼입 수정#274dasomel wants to merge 1 commit into
dasomel wants to merge 1 commit into
Conversation
readLine()은 재사용되는 고정 크기 버퍼(b)에 InputStream.read()로 읽어들인 뒤 실제로 읽은 바이트 수(line)를 계산만 하고 버퍼 전체를 그대로 반환하고 있었다. InputStream.read(byte[], int, int)는 요청한 길이만큼 채운다는 보장이 없어, 파일 크기가 고정 레코드 길이의 배수가 아닌 경우 마지막 레코드에서 실제로 더 적은 바이트만 읽히면 버퍼의 나머지 부분에 이전 레코드의 잔여 바이트가 그대로 남아 lineMapper에 오염된 데이터가 전달됐다. 실제로 읽은 만큼만 잘라 반환하도록 수정하고, 회귀 테스트를 추가했다.
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.
변경 사유
EgovFlatFileByteReader.readLine()은 고정 크기로 재사용되는 버퍼(b)에InputStream.read(byte[], int, int)로 읽어들인 뒤, 실제로 읽은 바이트 수(line)는 계산만 하고 사용하지 않은 채 버퍼 전체(b)를 그대로 반환하고 있었습니다.InputStream.read(byte[], int, int)는 요청한 길이만큼 반드시 채운다는 보장이 없습니다. 파일 크기가 고정 레코드 길이(length+ CRLF)의 배수가 아닌 경우, 마지막 레코드에서 실제로는 더 적은 바이트만 읽히는데도 재사용 버퍼의 나머지 부분에는 이전 레코드의 잔여 바이트가 그대로 남아 있어,lineMapper.mapLine()에 오염된(길이만 맞고 내용은 섞인) 데이터가 전달됩니다.변경 내용
readLine()이 실제로 읽은 바이트 수(line)만큼만 잘라서 반환하도록 수정했습니다. 전체 버퍼를 다 채운 경우(line == b.length)는 기존과 동일하게b를 그대로 반환해 불필요한 배열 복사를 피했습니다.테스트 방법 / 영향 범위
EgovFlatFileByteReaderTest를 신설해, 7바이트 고정 레코드 뒤에 3바이트짜리 짧은 마지막 레코드가 오는 상황을 재현했습니다. 수정 전 코드로 테스트를 실행하면 실패하고(이전 레코드의 잔여 바이트가 섞여 나옴), 수정 후에는 통과함을 직접 확인했습니다.mvn test로 모듈 전체 15개 테스트가 모두 통과합니다.