fix the file scan error related to CRLF format#6472
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a parsing issue in DataReaderFromAndroidAsset::scan() when configuration files use CRLF (Windows-style) line endings. When scanning data with CRLF line endings, the function would fail to properly skip over the \r\n sequence between lines, causing the parser to point to the leftover \r character and fail on subsequent scans.
Key Changes:
- Added CRLF detection and handling logic to skip
\r\nsequences at the beginning of a line, mirroring the existing logic for LF-only line endings
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/datareader.cpp
Outdated
| // however, it is fine to create "\nXYZ 123 abc" as sscanf will skip the leading newline silently | ||
| newline_pos = (const char*)memchr((const char*)d->mem + 1, '\n', remain_length - 1); | ||
| } | ||
| else if(remain_length > 2 && ((const char*)d->mem)[0] == '\r' && ((const char*)d->mem)[1] == '\n'){ |
There was a problem hiding this comment.
The condition 'remain_length > 2' should be 'remain_length >= 2' since you only need to access indices 0 and 1, which requires at least 2 bytes, not more than 2 bytes.
| else if(remain_length > 2 && ((const char*)d->mem)[0] == '\r' && ((const char*)d->mem)[1] == '\n'){ | |
| else if (remain_length >= 2 && ((const char*)d->mem)[0] == '\r' && ((const char*)d->mem)[1] == '\n') | |
| { |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6472 +/- ##
=======================================
Coverage 95.90% 95.90%
=======================================
Files 844 844
Lines 265298 265299 +1
=======================================
+ Hits 254431 254432 +1
Misses 10867 10867 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Thanks for your contribution ! |
目的
修复若使用CRLF格式换行的配置文件,调用int DataReaderFromAndroidAsset::scan(const char* format, void* p)函数导致解析异常的问题
问题说明
当采用Windows开发Android项目,由于git core.autocrlf 功能,会将配置文件换行转为CRLF风格的文件,导致执行DataReaderFromAndroidAsset::scan解析数据出现问题
问题举例
假如输入数据是
"123\r\n456"
第一次调用
dr.scan("%d", &a);
走else处理
得 line = "123\r"
得到
nconsumed = 3
p = 123
此刻 d->mem 指向的是 '\r' 后续数据就是 "\r\n456"
当第二次调用
dr.scan("%d", &a);
由于'\r'才是第一个字符,还是走的else
得 line = "\r"
到这里执行解析就不正确了,这并不是我们预期的,正确处理应该是类似\n处理,跳过\r\n读后续的数据