-
Notifications
You must be signed in to change notification settings - Fork 816
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 Proto encoding of negative integers #889
Conversation
src/converter/one/proto/Proto.java
Outdated
@@ -71,7 +71,7 @@ public void writeInt(int n) { | |||
int length = n == 0 ? 1 : (38 - Integer.numberOfLeadingZeros(n)) / 7; | |||
ensureCapacity(length); | |||
|
|||
while (n > 0x7f) { | |||
while (n & 0x7f != n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can a number be negative? In what cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case I hit was nanosSinceLastSample
in jfr2pprof.java
being negative.
I don't know if that is supposed to happen; all I can tell you is that it happened, with async-profiler 3.0 and the default clock source.
src/converter/one/proto/Proto.java
Outdated
@@ -82,7 +82,7 @@ public void writeLong(long n) { | |||
int length = n == 0 ? 1 : (70 - Long.numberOfLeadingZeros(n)) / 7; | |||
ensureCapacity(length); | |||
|
|||
while (n > 0x7f) { | |||
while (n & 0x7f != n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A canonical way to check if an integer has higher bits set is
(n & ~0x7f) != 0
or
(n >>> 7) != 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the check based on your suggestion.
5763d1a
to
76c1da4
Compare
Thank you for the fix. Merged. |
Currently the
Proto
class determines whether to emit a continuing byte for the integer valuen
this way:The intention of the comparison is to check whether
n
has any bit set below the lowest 7 bits. Unfortunately it does not work ifn
is negative, which causes the loop to be skipped entirely. When that happens, thejfr2pprof
produces a protobuf profile that is rejected bypprof
.This PR fixes the bug so that
Proto
encodes negative integer values correctly.