Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential inconsistency in BufferRaw::setCapacity method #251

Open
xiegd opened this issue Sep 25, 2024 · 1 comment
Open

Potential inconsistency in BufferRaw::setCapacity method #251

xiegd opened this issue Sep 25, 2024 · 1 comment

Comments

@xiegd
Copy link

xiegd commented Sep 25, 2024

Issue Description

In the BufferRaw::setCapacity method (file: ZLToolKit/src/Network/Buffer.h), there seems to be a potential inconsistency in how the capacity is handled when _data is not null.

Current Behavior

The current implementation sometimes returns early without updating _capacity, which may lead to a mismatch between _capacity and the actual allocated memory size.

Code in Question

    void setCapacity(size_t capacity) {
        if (_data) {
            do {
                if (capacity > _capacity) {
                    // If the requested memory is greater than the current memory, reallocate
                    break;
                }

                if (_capacity < 2 * 1024) {
                    // Less than 2K, do not repeatedly allocate memory, reuse directly
                    return;
                }

                if (2 * capacity > _capacity) {
                    // If the requested memory is greater than half of the current memory, also reuse
                    return;
                }
            } while (false);

            delete[] _data;
        }
        _data = new char[capacity];
        _capacity = capacity;
    }

Issue Description

In the BufferRaw::setCapacity method (file: ZLToolKit/src/Network/Buffer.h), there seems to be a potential inconsistency in how the capacity is handled when _data is not null.

Current Behavior

The current implementation sometimes returns early without updating _capacity, which may lead to a mismatch between _capacity and the actual allocated memory size.

Code in Question

    void setCapacity(size_t capacity) {
        if (_data) {
            do {
                if (capacity > _capacity) {
                    //请求的内存大于当前内存,那么重新分配  [AUTO-TRANSLATED:65306424]
                    //If the requested memory is greater than the current memory, reallocate
                    break;
                }

                if (_capacity < 2 * 1024) {
                    //2K以下,不重复开辟内存,直接复用  [AUTO-TRANSLATED:056416c0]
                    //Less than 2K, do not repeatedly allocate memory, reuse directly
                    return;
                }

                if (2 * capacity > _capacity) {
                    //如果请求的内存大于当前内存的一半,那么也复用  [AUTO-TRANSLATED:c189d660]
                    //If the requested memory is greater than half of the current memory, also reuse
                    return;
                }
            } while (false);

            delete[] _data;
        }
        _data = new char[capacity];
        _capacity = capacity;
    }

TRANS_BY_GITHUB_AI_ASSISTANT

@xia-chu
Copy link
Member

xia-chu commented Sep 25, 2024

setCapacity may indeed return a capacity greater than what was requested, I think this might be a bit counterintuitive, but it doesn't affect the correctness of any program.

setCapacity 确实可能返回比申请的更大的容量,我觉得这个可能是有点脱离一般常识,但是不会影响什么程序正确性。

TRANS_BY_GITHUB_AI_ASSISTANT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants