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

sclang: fix hang on floating point arguments to Integer:forBy #3804

Merged
merged 2 commits into from
Jun 24, 2018
Merged

sclang: fix hang on floating point arguments to Integer:forBy #3804

merged 2 commits into from
Jun 24, 2018

Conversation

lnihlen
Copy link
Member

@lnihlen lnihlen commented Jun 22, 2018

Regarding #2690.

Previously Integer:forBy would truncate floating point arguments, to integers,
which could potentially result in an infinite loop by the Integer:forBy
optimized special operation. This modification checks for any of the arguments
to be floating point, and if detecting that casts all of the arguments to
floating point. It also adds appropriate testing coverage.

Previously Integer:forBy would truncate floating point arguments, to integers,
which could potentially result in an infinite loop by the Integer:forBy
optimized special operation. This modification checks for any of the arguments
to be floating point, and if detecting that casts all of the arguments to
floating point. It also adds appropriate testing coverage.
@nhthn nhthn self-requested a review June 22, 2018 21:16
@nhthn nhthn added the comp: sclang sclang C++ implementation (primitives, etc.). for changes to class lib use "comp: class library" label Jun 22, 2018
Copy link
Contributor

@nhthn nhthn left a comment

Choose a reason for hiding this comment

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

thanks! something i just realized -- there's still an infinite loop if you set the step to float or int 0. i think it's reasonable to guard against such cases.

Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

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

Thanks for this @lnihlen ! Sorry it took awhile to get attention on it. Comments below, just style points

dispatch_opcode;
}
case 8 : {
PyrSlot * vars = g->frame->vars;
if ((slotRawInt(&vars[2]) >= 0 && slotRawInt(&vars[4]) <= slotRawInt(&vars[1]))
|| (slotRawInt(&vars[2]) < 0 && slotRawInt(&vars[4]) >= slotRawInt(&vars[1]))) {
bool continue_for_by = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

the codebase isn't super consistent but I'd say we generally prefer camelCase naming for local variables.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

error("Integer-forBy : endval or stepval not an Integer.\n");
// If any argument is floating point we cast all arguments
// to floating point, including the accumulator. This avoids
// potential infinite loops due to integer truncation.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,29 @@
TestInteger : UnitTest {
test_forby_with_positive_integer_arguments {
var forby_arguments = List.new;
Copy link
Contributor

Choose a reason for hiding this comment

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

and in sclang code we definitely prefer camelCase

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1,29 @@
TestInteger : UnitTest {
test_forby_with_positive_integer_arguments {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not 100% standardized yet, but I've got a WIP document for the project test suite here. The naming convention I'm trying to adopt is test_methodName_inputConditions[_expectedResult], so following that this test should be named something like test_forBy_positiveIntegerArguments. Also, you can omit the messages if you want, it's nice to have but IMO they're not super helpful beyond the names of the test functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thanks for the pointer to the guide.

@nhthn nhthn added this to the 3.10 milestone Jun 24, 2018
@lnihlen
Copy link
Member Author

lnihlen commented Jun 24, 2018

Thanks for the feedback. I added error detection for the case of the zero stepval. There's still a possibility of infinite loop by underflow but with 64-bit doubles this seems less troublesome.

@lnihlen
Copy link
Member Author

lnihlen commented Jun 24, 2018

Oh, on the testing - I looked but didn't see any way to assert in testing that a particular operation results in a language error. Something like this.assertAsserts(function, expectedError, ...)
would allow me to test the infinite loop error case as well as the non-integer or float argument error case.

@mossheim
Copy link
Contributor

I added error detection for the case of the zero stepval. There's still a possibility of infinite loop by underflow but with 64-bit doubles this seems less troublesome.

Thanks for that!

Oh, on the testing - I looked but didn't see any way to assert in testing that a particular operation results in a language error.

We have an open PR for this actually - #3808 . If you don't mind waiting a few days, it should be merged soon and then we rebase this PR and use it!

@lnihlen
Copy link
Member Author

lnihlen commented Jun 24, 2018

In the interest of expedience would it be reasonable for me to accept an open issue to add test coverage for the two failure cases (non-numeric arguments and zero stepval), with the agreement that I would put that up for review once #3808 is in?

@lnihlen
Copy link
Member Author

lnihlen commented Jun 24, 2018

I mean, and then merge this PR in the meantime?

@mossheim
Copy link
Contributor

In the interest of expedience would it be reasonable for me to accept an open issue to add test coverage for the two failure cases (non-numeric arguments and zero stepval), with the agreement that I would put that up for review once #3808 is in?

Not sure. In general our workflow has been to make things like this blocking, which I think helps to cut down on bookkeeping (and because people sometimes forget or fail to follow through on promises). But you've been staying on top of this one and it already has decent tests so let's give it a try. :)

Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

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

Approved as discussed :)

21.forBy(14.5, -3.125, { | i, j | forByArguments.add([i, j]); });
this.assertEquals(forByArguments, List[[21.0, 0], [17.875, 1], [14.75, 2]]);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

An unfortunate bug that hasn't been addressed - the IDE doesn't append a newline to the end of the final line of a document, so most files edited with it will appear to be missing a final newline in git. I looked into it a couple months ago but didn't really come up with anything, and ended up moving on to other issues. Something to keep in mind for the future

Copy link
Contributor

@nhthn nhthn left a comment

Choose a reason for hiding this comment

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

thanks! looks good

@nhthn nhthn merged commit bdc18cf into supercollider:develop Jun 24, 2018
@lnihlen
Copy link
Member Author

lnihlen commented Jun 24, 2018

Thanks for the review. In general my inclination is to decouple PRs as much as possible, to avoid possible propagation of delay through the PR dependency chain. Very hand-wavy, and I can't come up with a more concrete reason, so I appreciate the accommodation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: sclang sclang C++ implementation (primitives, etc.). for changes to class lib use "comp: class library"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants