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

quantile: nil deref #561

Merged
merged 1 commit into from Mar 24, 2015
Merged

quantile: nil deref #561

merged 1 commit into from Mar 24, 2015

Conversation

mreiferson
Copy link
Member

See commit comments. nsqadmin and nsq_stat are the only bundled
apps that end up using this code path and currently panic.

cc @cespare

RFR @jehiah

@mreiferson mreiferson added the bug label Mar 23, 2015
@@ -76,7 +76,7 @@ func (e *E2eProcessingLatencyAggregate) Less(i, j int) bool {

func (e *E2eProcessingLatencyAggregate) Add(e2 *E2eProcessingLatencyAggregate, N int) *E2eProcessingLatencyAggregate {
if e == nil {
*e = *e2
e = &(*e2)
Copy link
Member Author

Choose a reason for hiding this comment

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

This copies and then assigns the (new) address of e2, which is eventually returned by Add and then assigned over the original caller.

Gimme a bit to document that this method is side-effect free (and actually make the other branch in this method act that way instead of overwriting the receiver).

@mreiferson
Copy link
Member Author

recommend reviewing via https://github.com/bitly/nsq/pull/561/files?w=1

@@ -74,36 +74,43 @@ func (e *E2eProcessingLatencyAggregate) Less(i, j int) bool {
return e.Percentiles[i]["percentile"] > e.Percentiles[j]["percentile"]
}

// Add merges two E2eProcessingLatencyAggregate by averaging the percentiles
//
// It is side-effect free and does not modify the receiver or the parameter
Copy link
Contributor

Choose a reason for hiding this comment

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

As written this function does modify the receiver.

Copy link
Contributor

Choose a reason for hiding this comment

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

And actually, why is this desirable? Why not just make this func (e *E2eProcessingLatencyAggregate) Add(e2 *E2eProcessingLatencyAggregate, N int) and have it do an in-place update? From looking at the callers it seems like they're all doing x = x.Add(y) anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would be fine with this approach, too (as it would also now be internally consistent).

... and I thought this would be possible too, even if x is initially nil, and I might be missing something simple and obvious, but I couldn't seem to get it to work the way I would have expected.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, Go is pass-by-value always so that's not possible.

My suggestion is to change the calling code so that the receiver is never nil. I'm happy to send over a counter-PR to fix this bug, if you like.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll update - thanks for the help!

@mreiferson
Copy link
Member Author

@cespare check that latest commit

p = e.Percentiles
p[i]["quantile"] = value["quantile"]
// Add merges the passed E2eProcessingLatencyAggregate into the receiver
// by averaging the percentiles
Copy link
Contributor

Choose a reason for hiding this comment

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

How about

// Add merges e2 into e by averaging the percentiles.

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

@cespare
Copy link
Contributor

cespare commented Mar 24, 2015

LGTM

`Add()` intended to be side-effect free when called on a `nil` receiver.
@mreiferson
Copy link
Member Author

ready @jehiah

jehiah added a commit that referenced this pull request Mar 24, 2015
@jehiah jehiah merged commit 7085a8f into nsqio:master Mar 24, 2015
@mreiferson mreiferson deleted the panic_561 branch March 24, 2015 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants