Repository: asterixdb Updated Branches: refs/heads/master 689fb185c -> beb5dc746
Fix for numeric overflow in integer constructors Change-Id: I0cb3411bf9a808ee87f4938c60804a8d267c36d0 Reviewed-on: https://asterix-gerrit.ics.uci.edu/1509 Sonar-Qube: Jenkins <[email protected]> Tested-by: Jenkins <[email protected]> Reviewed-by: Ian Maxon <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/asterixdb/repo Commit: http://git-wip-us.apache.org/repos/asf/asterixdb/commit/beb5dc74 Tree: http://git-wip-us.apache.org/repos/asf/asterixdb/tree/beb5dc74 Diff: http://git-wip-us.apache.org/repos/asf/asterixdb/diff/beb5dc74 Branch: refs/heads/master Commit: beb5dc7468322ebb216d46e1744b9bff37d2dfa6 Parents: 689fb18 Author: Ildar Absalyamov <[email protected]> Authored: Tue Feb 28 10:34:43 2017 -0800 Committer: Ildar Absalyamov <[email protected]> Committed: Tue Feb 28 15:17:16 2017 -0800 ---------------------------------------------------------------------- .../domain_boundaries.1.ddl.aql | 32 +++++++++++++ .../domain_boundaries.2.ddl.aql | 37 +++++++++++++++ .../domain_boundaries.3.query.aql | 19 ++++++++ .../domain_boundaries_error.1.ddl.aql | 29 ++++++++++++ .../domain_boundaries_error.2.ddl.aql | 46 +++++++++++++++++++ .../domain_boundaries_error.3.ddl.aql | 46 +++++++++++++++++++ .../domain_boundaries_error.4.ddl.aql | 47 +++++++++++++++++++ .../domain_boundaries_error.5.ddl.aql | 48 ++++++++++++++++++++ .../domain_boundaries/domain_boundaries.1.adm | 2 + .../src/test/resources/runtimets/testsuite.xml | 14 ++++++ .../AInt16ConstructorDescriptor.java | 17 +++++-- .../AInt32ConstructorDescriptor.java | 17 +++++-- .../AInt64ConstructorDescriptor.java | 17 +++++-- .../AInt8ConstructorDescriptor.java | 17 +++++-- 14 files changed, 376 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.1.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.1.ddl.aql new file mode 100644 index 0000000..0885da1 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.1.ddl.aql @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64, + int8: int8, + int16: int16, + int32: int32, + int64: int64 +} + +create dataset TestDS(Type) primary key id; http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.2.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.2.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.2.ddl.aql new file mode 100644 index 0000000..551f658 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.2.ddl.aql @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +use dataverse test; + +insert into dataset TestDS( +{ + "id": 1, + "int8": int8("-128"), + "int16": int16("-32768"), + "int32": int32("-2147483648"), + "int64": int64("-9223372036854775808") +}) + +insert into dataset TestDS( +{ + "id": 2, + "int8": int8("127"), + "int16": int16("32767"), + "int32": int32("2147483647"), + "int64": int64("9223372036854775807") +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.3.query.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.3.query.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.3.query.aql new file mode 100644 index 0000000..1b11312 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries/domain_boundaries.3.query.aql @@ -0,0 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + for $x in dataset test.TestDS return $x; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.1.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.1.ddl.aql new file mode 100644 index 0000000..3aacd8f --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.1.ddl.aql @@ -0,0 +1,29 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64 +} + +create dataset TestDS(Type) primary key id; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.2.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.2.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.2.ddl.aql new file mode 100644 index 0000000..8ae6438 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.2.ddl.aql @@ -0,0 +1,46 @@ + + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64, + int8: int8, + int16: int16, + int32: int32, + int64: int64 +} + +create dataset TestDS(Type) primary key id; + +insert into dataset TestDS( +{ + "id": 1, + "int8": int8("-129") +}) + +insert into dataset TestDS( +{ + "id": 2, + "int8": int8("128") +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.3.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.3.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.3.ddl.aql new file mode 100644 index 0000000..1aaf98c --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.3.ddl.aql @@ -0,0 +1,46 @@ + + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64, + int8: int8, + int16: int16, + int32: int32, + int64: int64 +} + +create dataset TestDS(Type) primary key id; + +insert into dataset TestDS( +{ + "id": 1, + "int16": int16("-32769") +}) + +insert into dataset TestDS( +{ + "id": 2, + "int16": int16("32768") +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.4.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.4.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.4.ddl.aql new file mode 100644 index 0000000..40d85c01b --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.4.ddl.aql @@ -0,0 +1,47 @@ + + + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64, + int8: int8, + int16: int16, + int32: int32, + int64: int64 +} + +create dataset TestDS(Type) primary key id; + +insert into dataset TestDS( +{ + "id": 1, + "int32": int32("-2147483649") +}) + +insert into dataset TestDS( +{ + "id": 2, + "int32": int32("2147483648") +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.5.ddl.aql ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.5.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.5.ddl.aql new file mode 100644 index 0000000..411a78c --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/types/domain_boundaries_error/domain_boundaries_error.5.ddl.aql @@ -0,0 +1,48 @@ + + + + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +drop dataverse test if exists; +create dataverse test; + +use dataverse test; + +create type Type as open { + id: int64, + int8: int8, + int16: int16, + int32: int32, + int64: int64 +} + +create dataset TestDS(Type) primary key id; + +insert into dataset TestDS( +{ + "id": 1, + "int64": int64("-9223372036854775809") +}) + +insert into dataset TestDS( +{ + "id": 2, + "int64": int64("9223372036854775808") +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/results/types/domain_boundaries/domain_boundaries.1.adm ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/types/domain_boundaries/domain_boundaries.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/types/domain_boundaries/domain_boundaries.1.adm new file mode 100644 index 0000000..81a383d --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/types/domain_boundaries/domain_boundaries.1.adm @@ -0,0 +1,2 @@ +{ "id": 1, "int8": -128, "int16": -32768, "int32": -2147483648, "int64": -9223372036854775808 } +{ "id": 2, "int8": 127, "int16": 32767, "int32": 2147483647, "int64": 9223372036854775807 } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml index a4bd36f..a651833 100644 --- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml +++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml @@ -6966,6 +6966,20 @@ </compilation-unit> </test-case> <test-case FilePath="types"> + <compilation-unit name="domain_boundaries"> + <output-dir compare="Text">domain_boundaries</output-dir> + </compilation-unit> + </test-case> + <test-case FilePath="types"> + <compilation-unit name="domain_boundaries_error"> + <output-dir compare="Text">domain_boundaries_error</output-dir> + <expected-error>ASX0006: Invalid format for int8 in int8</expected-error> + <expected-error>ASX0006: Invalid format for int16 in int16</expected-error> + <expected-error>ASX0006: Invalid format for int32 in int32</expected-error> + <expected-error>ASX0006: Invalid format for int64 in int64</expected-error> + </compilation-unit> + </test-case> + <test-case FilePath="types"> <compilation-unit name="record01"> <output-dir compare="Text">record01</output-dir> </compilation-unit> http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt16ConstructorDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt16ConstructorDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt16ConstructorDescriptor.java index 26fe3d8..9f7dbc3 100644 --- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt16ConstructorDescriptor.java +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt16ConstructorDescriptor.java @@ -87,18 +87,24 @@ public class AInt16ConstructorDescriptor extends AbstractScalarFunctionDynamicDe if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { utf8Ptr.set(serString, startOffset + 1, len - 1); offset = utf8Ptr.getCharStartOffset(); + //accumulating value in negative domain + //otherwise Short.MIN_VALUE = -(Short.MAX_VALUE + 1) would have caused overflow value = 0; positive = true; + short limit = -Short.MAX_VALUE; if (serString[offset] == '+') { offset++; } else if (serString[offset] == '-') { offset++; positive = false; + limit = Short.MIN_VALUE; } int end = startOffset + len; for (; offset < end; offset++) { + int digit; if (serString[offset] >= '0' && serString[offset] <= '9') { - value = (short) (value * 10 + serString[offset] - '0'); + value = (short) (value * 10); + digit = serString[offset] - '0'; } else if (serString[offset] == 'i' && serString[offset + 1] == '1' && serString[offset + 2] == '6' && offset + 3 == end) { break; @@ -106,12 +112,17 @@ public class AInt16ConstructorDescriptor extends AbstractScalarFunctionDynamicDe throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG); } + if (value < limit + digit) { + throw new InvalidDataFormatException(getIdentifier(), + ATypeTag.SERIALIZED_INT16_TYPE_TAG); + } + value = (short) (value - digit); } - if (value < 0) { + if (value > 0) { throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG); } - if (value > 0 && !positive) { + if (value < 0 && positive) { value *= -1; } http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt32ConstructorDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt32ConstructorDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt32ConstructorDescriptor.java index a186d4b..bc74b94 100644 --- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt32ConstructorDescriptor.java +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt32ConstructorDescriptor.java @@ -86,18 +86,24 @@ public class AInt32ConstructorDescriptor extends AbstractScalarFunctionDynamicDe if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { utf8Ptr.set(serString, startOffset + 1, len - 1); offset = utf8Ptr.getCharStartOffset(); + //accumulating value in negative domain + //otherwise Integer.MIN_VALUE = -(Integer.MAX_VALUE + 1) would have caused overflow value = 0; positive = true; + int limit = -Integer.MAX_VALUE; if (serString[offset] == '+') { offset++; } else if (serString[offset] == '-') { offset++; positive = false; + limit = Integer.MIN_VALUE; } int end = startOffset + len; for (; offset < end; offset++) { + int digit; if (serString[offset] >= '0' && serString[offset] <= '9') { - value = value * 10 + serString[offset] - '0'; + value *= 10; + digit = serString[offset] - '0'; } else if (serString[offset] == 'i' && serString[offset + 1] == '3' && serString[offset + 2] == '2' && offset + 3 == end) { break; @@ -105,12 +111,17 @@ public class AInt32ConstructorDescriptor extends AbstractScalarFunctionDynamicDe throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT32_TYPE_TAG); } + if (value < limit + digit) { + throw new InvalidDataFormatException(getIdentifier(), + ATypeTag.SERIALIZED_INT32_TYPE_TAG); + } + value -= digit; } - if (value < 0) { + if (value > 0) { throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT32_TYPE_TAG); } - if (value > 0 && !positive) { + if (value < 0 && positive) { value *= -1; } http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt64ConstructorDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt64ConstructorDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt64ConstructorDescriptor.java index aed0fe0..0870f73 100644 --- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt64ConstructorDescriptor.java +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt64ConstructorDescriptor.java @@ -86,18 +86,24 @@ public class AInt64ConstructorDescriptor extends AbstractScalarFunctionDynamicDe if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { utf8Ptr.set(serString, startOffset + 1, len - 1); offset = utf8Ptr.getCharStartOffset(); + //accumulating value in negative domain + //otherwise Long.MIN_VALUE = -(Long.MAX_VALUE + 1) would have caused overflow value = 0; positive = true; + long limit = -Long.MAX_VALUE; if (serString[offset] == '+') { offset++; } else if (serString[offset] == '-') { offset++; positive = false; + limit = Long.MIN_VALUE; } int end = startOffset + len; for (; offset < end; offset++) { + int digit; if (serString[offset] >= '0' && serString[offset] <= '9') { - value = value * 10 + serString[offset] - '0'; + value *= 10; + digit = serString[offset] - '0'; } else if (serString[offset] == 'i' && serString[offset + 1] == '6' && serString[offset + 2] == '4' && offset + 3 == end) { break; @@ -105,12 +111,17 @@ public class AInt64ConstructorDescriptor extends AbstractScalarFunctionDynamicDe throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT64_TYPE_TAG); } + if (value < limit + digit) { + throw new InvalidDataFormatException(getIdentifier(), + ATypeTag.SERIALIZED_INT64_TYPE_TAG); + } + value -= digit; } - if (value < 0 && value != Long.MIN_VALUE) { + if (value > 0) { throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT64_TYPE_TAG); } - if (value > 0 && !positive) { + if (value < 0 && positive) { value *= -1; } http://git-wip-us.apache.org/repos/asf/asterixdb/blob/beb5dc74/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt8ConstructorDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt8ConstructorDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt8ConstructorDescriptor.java index 753b026..c0d9fd4 100644 --- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt8ConstructorDescriptor.java +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AInt8ConstructorDescriptor.java @@ -86,18 +86,24 @@ public class AInt8ConstructorDescriptor extends AbstractScalarFunctionDynamicDes if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { utf8Ptr.set(serString, startOffset + 1, len - 1); offset = utf8Ptr.getCharStartOffset(); + //accumulating value in negative domain + //otherwise Byte.MIN_VALUE = -(Byte.MAX_VALUE + 1) would have caused overflow value = 0; positive = true; + byte limit = -Byte.MAX_VALUE; if (serString[offset] == '+') { offset++; } else if (serString[offset] == '-') { offset++; positive = false; + limit = Byte.MIN_VALUE; } int end = startOffset + len; for (; offset < end; offset++) { + int digit; if (serString[offset] >= '0' && serString[offset] <= '9') { - value = (byte) (value * 10 + serString[offset] - '0'); + value = (byte) (value * 10); + digit = serString[offset] - '0'; } else if (serString[offset] == 'i' && serString[offset + 1] == '8' && offset + 2 == end) { break; @@ -105,12 +111,17 @@ public class AInt8ConstructorDescriptor extends AbstractScalarFunctionDynamicDes throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT8_TYPE_TAG); } + if (value < limit + digit) { + throw new InvalidDataFormatException(getIdentifier(), + ATypeTag.SERIALIZED_INT8_TYPE_TAG); + } + value = (byte) (value - digit); } - if (value < 0) { + if (value > 0) { throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT8_TYPE_TAG); } - if (value > 0 && !positive) { + if (value < 0 && positive) { value *= -1; }
