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

Use exception rather than assertion for parsing data #344

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/bitstream/BitReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class BitReader

inline DWORD Get(DWORD n)
{
assert(n <= 32);
if (n > 32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this, but it means that someone is calling Get() asking for more bits of data than are possible to fit in the DWORD. I.e. Is a bug in the caller really which assert() is theoretically correct for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be that strict. Passing n shouldn't be illegal but just invalid. The argument could be from data not from our manual input. So I don't think calling it with a value more than 32 is a bug. If we use assert, we must check it before passing it to this Get() function, which would be not convenient and I think that is against our intention to use exception.

throw std::invalid_argument("BitReader::Get() n > 32");

//Debug(">BitReader::Get() n:%d cached:%d\n", n, cached);
//BitDump(cache, cached);
Expand Down Expand Up @@ -105,7 +106,8 @@ class BitReader

inline DWORD Peek(DWORD n)
{
assert(n <= 32);
if (n > 32)
throw std::invalid_argument("BitReader::Peek() n > 32");

DWORD ret = 0;

Expand Down
Loading