From: Robert Varga Date: Sun, 7 Feb 2021 11:04:23 +0000 (+0100) Subject: Do not use BigInteger in 'range' argument X-Git-Tag: v7.0.0~199 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F56%2F95056%2F1;p=yangtools.git Do not use BigInteger in 'range' argument The only production allowed here is 'integer-value', which is logically clamped to int64/uint64 range. Use Long/Uint64 to represent these, so that we lower our memory footprint. JIRA: YANGTOOLS-1206 Change-Id: I1d186d0ed0211ac0fa996fe93d6cd7aa28442520 Signed-off-by: Robert Varga --- diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/range/RangeStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/range/RangeStatementSupport.java index 845571043b..f68caef7f5 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/range/RangeStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/range/RangeStatementSupport.java @@ -10,10 +10,11 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.range; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.math.BigDecimal; -import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.yangtools.yang.common.Uint64; import org.opendaylight.yangtools.yang.model.api.YangStmtMapping; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; @@ -102,18 +103,30 @@ public final class RangeStatementSupport return SUBSTATEMENT_VALIDATOR; } - private static Number parseDecimalConstraintValue(final StmtContext ctx, final String value) { + private static @NonNull Number parseDecimalConstraintValue(final @NonNull StmtContext ctx, + final @NonNull String value) { if ("max".equals(value)) { return UnresolvedNumber.max(); } if ("min".equals(value)) { return UnresolvedNumber.min(); } + // Deal with decimal64, i.e. 'decimal-value' production of the RFC6020 ABNF + if (value.indexOf('.') != -1) { + try { + // FIXME: YANGTOOLS-556: Use Decimal64 here + return new BigDecimal(value); + } catch (NumberFormatException e) { + throw new SourceException(ctx, e, "Value %s is not a valid decimal number", value); + } + } + // This has to be an 'integer-value' production of the RFC6020 ABNF. We also clamp allowed range to Long/Uint, + // as that is the effectively-valid range of allowed values. For Uint64 we also try to intern the value. try { - return value.indexOf('.') != -1 ? new BigDecimal(value) : new BigInteger(value); - } catch (final NumberFormatException e) { - throw new SourceException(ctx, e, "Value %s is not a valid decimal number", value); + return value.startsWith("-") ? Long.valueOf(value) : Uint64.valueOf(value).intern(); + } catch (IllegalArgumentException e) { + throw new SourceException(ctx, e, "Value %s is not a valid integral range number", value); } } } \ No newline at end of file