-
Notifications
You must be signed in to change notification settings - Fork 258
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
Issue #144 Fix - JBIG2 - Changed integer variables types #148
Conversation
Codecov Report
@@ Coverage Diff @@
## development #148 +/- ##
==============================================
Coverage ? 63.01%
==============================================
Files ? 187
Lines ? 33705
Branches ? 0
==============================================
Hits ? 21239
Misses ? 11968
Partials ? 498
Continue to review full report at Codecov.
|
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 are those specific integers needed, i.e. like int32 etc, why not just int?
We don't have a convention of using int32s over int in the codebase overall
Any specific reason why it would be needed in jbig2 over other parts of the code? What do other go projects do ?
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 changes look correct to me. It should fix issue #144. However, there seem to be a lot of modifications. I'm not sure why all the int -> int32
and int -> uint32
changes. Maybe only a subset of the modifications is needed in order to solve the issue? The problem seems to be the usage of 0xffffffff
which is basically the value of math.MaxUint32
, with int
variables..
From what I can tell, an int is at least 32 bits on all of the platforms/architectures that Go supports.
In my opinion, the changes in types should be done depending on the context. I usually follow these guidelines for integers:
- If the maximum value of an entity is known, then the smallest type that can hold that maximum value should be used, in order to be memory efficient. For example, in the
image/color
package,color.RGBA
holds components of typeuint8
as the values cannot be larger than 255. SimilarlyRGBA64
has color components of typeuint16
. - If the value of an entity cannot be negative, then an unsigned type should be used.
- When not sure, use
int
or a larger type if large values are to be expected (likeint64
). I tend to preferint
in most cases, especially for indices of loops or lengths of collections. Thelen
builtin function returns anint
.
An advantage of using fixed types like int32
is that the behavior is the same on all platforms.
However, when used for lengths of slices and loop indices, a lot of casts have to be made, and the code does not look that pretty:
for curLen := int32(1); curLen <= int32(len(lenCount)); curLen++ {}
There also seem to be places in the code where an int
is used, not an int32
and then the values are clamped to the maximum of int32
like this:
r.XLocation = int(temp & math.MaxInt32)
Why not use an int32
if the maximum allowed value is the maximum of int32
?
As an alternative, you could use something like:
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
The value of MaxInt
should be math.MaxInt64
on platforms where int
is 64 bits and math.MaxInt32
on platforms where int
is 32 bits. Then, MaxInt
could be used instead of math.MaxInt32
.
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.
Current PR not only fixes #144 but also prevent unwanted behavior of the huffman table and arithmetic decoder where the int32
variables mustn't overflow on 64bit platforms .
The most recent revision contains already a subset of the modifications that are required for the jbig2 decoder to work properly.
The changes of int -> uint32
sets the unsigned
restriction.
In initial revision the int -> int32
change were applied to most of the jbig2 packages. The JBIG2 standard ISO/IEC 14992 defines segments and document variable size precisely. As discussed with gunnsth
setting int32
instead of int
is against the convention. Variables size constraint in jbig2 segments and document doesn't change the logic. What's more casting into int32
makes the code less readable.
That's why only the decoders parts have an int32
type constraint, while the other were left with int
.
Reviewable status: 0 of 24 files reviewed, all discussions resolved
@kucjac Can you sign the CLA again? There was a problem with it, just fixed it. |
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.
@gunnsth Sure, I've already signed it.
Ok, I'm going to change all the specification defined 32-bit integers in the segment definitions from int -> int32
.
Reviewable status: 0 of 24 files reviewed, all discussions resolved
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.
LGTM
This PR fixes #144.
There were problem with non-sized integers for non amd64 platforms. A lot of structures in the JBIG2 encoding required to have their integer variable sizes set on.
The problem no longer occurs on emulated environment.
This PR were tested in an emulated ARMv7 environment as well as on amd64 platform.
This change is![Reviewable](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)