Repository: amzn/ion-java
Pull request #182 (2018-12-10 20:20:10)

Throwing for negative zero in binary
*Issue #, if available:*
https://github.com/amzn/ion-java/issues/138

*Description of changes:*
See title, spec: Spec: http://amzn.github.io/ion-docs/docs/binary.html#2-and-3-int

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.



Patch:
diff --git a/src/software/amazon/ion/impl/IonReaderBinarySystemX.java b/src/software/amazon/ion/impl/IonReaderBinarySystemX.java
index 27b9900d..f28361e7 100644
--- a/src/software/amazon/ion/impl/IonReaderBinarySystemX.java
+++ b/src/software/amazon/ion/impl/IonReaderBinarySystemX.java
@@ -180,14 +180,19 @@ class IonReaderBinarySystemX
             _v.setAuthoritativeType(AS_TYPE.boolean_value);
             break;
         case INT:
+            boolean is_negative = _value_tid == PrivateIonConstants.tidNegInt;
+
             if (_value_len == 0) {
+                if (is_negative) {
+                    throwIllegalNegativeZeroException();
+                }
+
                 int v = 0;
                 _v.setValue(v);
                 _v.setAuthoritativeType(AS_TYPE.int_value);
             }
             else if (_value_len <= Long.BYTES) {
                 long v = readULong(_value_len);
-                boolean is_negative = _value_tid == PrivateIonConstants.tidNegInt;
 
                 if (v < 0) {
                     // we probably can't fit this magnitude properly into a Java long
@@ -205,6 +210,9 @@ class IonReaderBinarySystemX
                 }
                 else {
                     if (is_negative) {
+                        if(v == 0) {
+                            throwIllegalNegativeZeroException();
+                        }
                         v = -v;
                     }
                     if (v < Integer.MIN_VALUE || v > Integer.MAX_VALUE) {
@@ -217,7 +225,6 @@ class IonReaderBinarySystemX
                 }
             }
             else {
-                boolean is_negative = (_value_tid == PrivateIonConstants.tidNegInt);
                 BigInteger v = readBigInteger(_value_len, is_negative);
                 _v.setValue(v);
                 _v.setAuthoritativeType(AS_TYPE.bigInteger_value);
@@ -482,4 +489,8 @@ class IonReaderBinarySystemX
     {
         return null;
     }
+
+    private void throwIllegalNegativeZeroException() {
+        throw newErrorAt("negative zero is illegal in the binary format");
+    }
 }


Test patch:
diff --git a/test/software/amazon/ion/TestUtils.java b/test/software/amazon/ion/TestUtils.java
index ba7cad75..f76b96d7 100644
--- a/test/software/amazon/ion/TestUtils.java
+++ b/test/software/amazon/ion/TestUtils.java
@@ -144,8 +144,6 @@ public class TestUtils
             , "good/utf16.ion"                              // TODO amzn/ion-java#61
             , "good/utf32.ion"                              // TODO amzn/ion-java#61
             , "good/whitespace.ion"                         // TODO amzn/ion-java#104
-            , "bad/negativeIntZero.10n"                     // TODO amzn/ion-java#138
-            , "bad/negativeIntZeroLn.10n"                   // TODO amzn/ion-java#138
             , "good/item1.10n"                              // TODO amzn/ion-java#126 (roundtrip symbols with unknown text)
         );