修复 tcp/ 和 http/ 中 buffer/ringbuffer 相关问题 - #2
Open
FluxMind-O wants to merge 4 commits into
Open
Conversation
buffer_ 由 new char[capacity_] 分配,析构却用 delete,属未定义行为,可能导致内存损坏。改为 delete[]。
当 writerIndex_ < readerIndex_(环形回绕)时,memcpy 后未更新 writerIndex_,新写入数据被后续写入覆盖且索引停滞,导致数据丢失。在 memcpy 后补 writerIndex_ += len。
writeFd 始终返回 0 而非实际写入字节数 n,调用方 TcpConnection::handleWrite 依赖返回值判断写入进度,导致误判已写完。改为返回 n,并在 n < 0 时设置 *savedErrno = errno。
原 RINGBUFFER 模式 parseRequest 通过 retrieveAsString 将整个缓冲区 O(n) 拷贝成 std::string 再解析,丧失零拷贝优势;且 if(crlf) 判断 std::search 返回值永远为真(data()+size() 非空指针),存在解析 bug。线性 Buffer 与环形 RingBuffer 暴露一致的 findCRLF/peek/retrieveUntil 接口,合并两套 parseRequest 为一份零拷贝实现。
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.
修复
tcp/和http/中几个 buffer/ringbuffer 相关问题:KRingBuffer析构时使用delete[]释放new[]分配的内存。RingBuffer::append()回绕时没有推进writerIndex_的问题。writeFd()始终返回0的问题,并在写失败时保存errno。Buffer/RingBuffer的 HTTP 请求解析路径,避免 RINGBUFFER 模式下把整个 buffer 拷贝到std::string。每个改动对应一个 commit。
Tests
已测试:
USE_RINGBUFFER模式编译通过append():readableBytes()从修复前的 100 变为预期的 200writeFd()返回实际写入长度(测试返回 4)GET /hello?name=flux HTTP/1.1编译环境:g++ 11.4,
-O2 -std=c++11 -DNDEBUG没有搞 API 签名变化,也没有新增依赖。