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

[C++] Why there is no error handling on out-bound skip of InputBuffer? #1158

Open
SleepyBag opened this issue Jul 13, 2022 · 4 comments
Open
Labels

Comments

@SleepyBag
Copy link

Here is the code of InputBuffer::Skip:

    void Skip(uint32_t size)
    {
        if (size > _blob.length() - _pointer)
        {
            return;
        }

        _pointer += size;
    }

It can be seen that when the size is too large, it simply does nothing - neither throw an exception nor return a fail code. Is this behavior by design? Given that the _pointer member is protected, there is no way we can know if the skip success after calling it.

@chwarr
Copy link
Member

chwarr commented Jul 13, 2022

Hmmm... It looks like this is an oversight. I think seeking beyond the end of the buffer should set the position to _blob.length() so that the next read will fail with EOF. Is this a fix you could contribute with a unit test?

void Skip(uint32_t size)
{
    if (size > _blob.length() - _pointer)
    {
        _pointer = _blob.length();
    }
    else
    {
        _pointer += size;
    }
}

@chwarr chwarr added the bug label Jul 13, 2022
@SleepyBag
Copy link
Author

SleepyBag commented Jul 14, 2022

I don't think it the right behavior, either. The right behavior in my opinion should be letting caller know there is an error, by either an error code or an exception, instead of behaving unexpectedly - if there is no further call, the caller would never know such an error.

@chwarr
Copy link
Member

chwarr commented Jul 14, 2022

Throwing EOF after setting _pointer would be fine too. Just throwing wouldn't "poison" the stream for the future.

@SleepyBag
Copy link
Author

SleepyBag commented Jul 15, 2022

Throwing EOF after setting _pointer would be fine too.

Yes, this is a good choice. I'll seek to contribute to it.

@SleepyBag SleepyBag changed the title Why there is no error handling on out-bound skip of InputBuffer? [C++] Why there is no error handling on out-bound skip of InputBuffer? Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants