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

fix toShortSafely: yes we can safely convert -1 to short :) #168

Merged
merged 6 commits into from
Apr 3, 2024

Conversation

mpollmeier
Copy link
Contributor

also: fix array kind bounds check

@mpollmeier mpollmeier requested a review from bbrehm April 1, 2024 16:05
@@ -52,7 +52,7 @@ class Graph(val schema: Schema, val storagePathMaybe: Option[Path] = None) exten

/** Note: this included `deleted` nodes! You might want to use `livingNodeCountByKind` instead. */
private[flatgraph] def nodeCountByKind(kind: Int): Int =
if (nodesArray.length <= kind) 0
if (nodesArray.length < kind || kind < 0) 0
Copy link
Contributor

Choose a reason for hiding this comment

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

The kind == nodesArray.length case is off-by-one and should be included in this check -- this ain't julia ;)

I would remove this check entirely: it is clearly an error and an immediate array-oob exception is best for debugging. But if we have the check then we should do it right.

Copy link
Contributor Author

@mpollmeier mpollmeier Apr 3, 2024

Choose a reason for hiding this comment

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

whoopsie, thanks for catching

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated - I also change the semantics: we now throw an exception if someone asks for an invalid kind, previously that would just return 0. I'm still undecided which way to go tbh... do you have a preference?

@@ -3,7 +3,10 @@ package flatgraph.misc
object Conversions {
extension (i: Int) {
def toShortSafely: Short = {
assert(0 <= i && i <= Short.MaxValue, throw new ConversionException(s"cannot safely downcast int with value=$i to short"))
assert(
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, I would not include such checks. But if you want to include it, then I guess i == i.toShort.toInt is more to the point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for introducing this check is to provide a more specific error than an array-oob exception which is quite generic.
The .toShort.toInt check is equivalent but more clear, so yes good suggestion, thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason for introducing this check is to provide a more specific error than an array-oob exception which is quite generic.

Yeah, but this is not an error condition that anyone is supposed to specifically catch and recover from.

It is an exception where somebody is supposed to stare at the stack-trace in an error log, look at the line numbers, and also look at the relevant code path.

Given that the person handling the exception already has that info, most manual throw error strings are pretty superfluous: For such exceptions, a source-code comment around the throw is equivalent to the static string part of an error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and also look at the relevant code path

From your (and mine) perspective that's true, but that's one additional step that many downstream users simply won't go ¯_(ツ)_/¯

And in this specific case we do need the check, simply to avoid bugs that root in things like

scala> 3333333.toShort
val res0: Short = -9003

@mpollmeier mpollmeier merged commit ba07b9d into master Apr 3, 2024
1 check passed
@max-leuthaeuser max-leuthaeuser deleted the michael/fix-int-short-conversion branch August 5, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants