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

Poco::DateTime uses assertions for validation #1540

Closed
kimgr opened this issue Jan 4, 2017 · 4 comments
Closed

Poco::DateTime uses assertions for validation #1540

kimgr opened this issue Jan 4, 2017 · 4 comments

Comments

@kimgr
Copy link
Contributor

kimgr commented Jan 4, 2017

We're unit-testing some code based on Poco::DateTime.

When we throw invalid dates at our code, this is printed to the console:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Assertion violation: month >= 1 && month <= 12 [in file "src/DateTime.cpp", line 67]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

It turns out because DateTime uses poco_assert for validation, there doesn't seem to be a clean way to validate date times without double-checking them.

Would you consider patches to throw a documented exception type instead? Any ideas on which type is preferable? Just a Poco::Exception?

@obiltschnig
Copy link
Member

Yes. Throwing Poco::InvalidArgumentException would be appropriate.

@kimgr
Copy link
Contributor Author

kimgr commented Jan 25, 2017

Thanks! Sorry I never got around to this, something more urgent came up.

Meanwhile, I read about narrow vs wide contracts [1] and started to think that maybe the DateTime constructor is better off with a narrow contract with undefined behavior in the presence of obviously bad data.

That said, Poco strikes me as trying to avoid undefined behavior for the most part, so an exception seems more in style. Let me know what you prefer.

[1] http://stackoverflow.com/a/16734278/96963

@PureAbstract
Copy link

For what little it's worth, I'd prefer that the constructor validates and throws on error.
Other alternatives might be have an 'IsValid()' member that could be called by those of us who care about avoiding undefined behaviour - another option might be for callers to pass an error_code by reference (as in std::filesystem).

Whichever way it's done, in a library like Poco, there should be some way for the caller to verify that DateTime is doing what they expect it to do.

@SmileGobo
Copy link

The bug is still in 1.13.3

@aleks-f aleks-f self-assigned this Aug 17, 2024
@aleks-f aleks-f added this to 1.14 Aug 17, 2024
@aleks-f aleks-f added this to the Release 1.14.0 milestone Aug 17, 2024
@aleks-f aleks-f moved this to In Progress in 1.14 Aug 17, 2024
@aleks-f aleks-f reopened this Aug 17, 2024
aleks-f added a commit that referenced this issue Oct 3, 2024
* Poco::DateTime uses assertions for validation #1540

* fix(DateTime): fix ASAN buf overflow

* fix(DateTime): allow negative year (TODO: 0 Julian day Gregorian year value)
@aleks-f aleks-f moved this from In Progress to Done in 1.14 Oct 3, 2024
@aleks-f aleks-f added the fixed label Oct 3, 2024
@matejk matejk closed this as completed Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

6 participants