]> granicus.if.org Git - icu/commitdiff
ICU-13177 Fixing coverity issues in number formatting code.
authorShane Carr <shane@unicode.org>
Wed, 4 Oct 2017 22:29:21 +0000 (22:29 +0000)
committerShane Carr <shane@unicode.org>
Wed, 4 Oct 2017 22:29:21 +0000 (22:29 +0000)
X-SVN-Rev: 40554

icu4c/source/i18n/number_compact.cpp
icu4c/source/i18n/number_decimalquantity.cpp
icu4c/source/i18n/number_longnames.cpp
icu4c/source/i18n/number_modifiers.cpp
icu4c/source/i18n/number_patternmodifier.h
icu4c/source/i18n/number_patternstring.cpp
icu4c/source/i18n/number_patternstring.h
icu4j/main/classes/core/src/com/ibm/icu/impl/number/MacroProps.java
icu4j/main/classes/core/src/com/ibm/icu/impl/number/MultiplierImpl.java
icu4j/main/classes/core/src/com/ibm/icu/impl/number/SimpleModifier.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatSymbols.java

index 12fa956a39496fc2dff97eaf95b311aaf6774b75..8943ebbf194029f4e8efef5518d1cbd6d6c15603 100644 (file)
@@ -216,7 +216,6 @@ CompactHandler::CompactHandler(CompactStyle compactStyle, const Locale &locale,
                                UErrorCode &status)
         : rules(rules), parent(parent) {
     data.populate(locale, nsName, compactStyle, compactType, status);
-    if (U_FAILURE(status)) { return; }
     if (buildReference != nullptr) {
         // Safe code path
         precomputeAllModifiers(*buildReference, status);
@@ -234,6 +233,8 @@ CompactHandler::~CompactHandler() {
 }
 
 void CompactHandler::precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status) {
+    if (U_FAILURE(status)) { return; }
+
     // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T
     UVector allPatterns(12, status);
     if (U_FAILURE(status)) { return; }
@@ -306,8 +307,8 @@ void CompactHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micr
         // C++ Note: Use unsafePatternInfo for proper lifecycle.
         ParsedPatternInfo &patternInfo = const_cast<CompactHandler *>(this)->unsafePatternInfo;
         PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
-        dynamic_cast<MutablePatternModifier *>(const_cast<Modifier *>(micros.modMiddle))->setPatternInfo(
-                &patternInfo);
+        static_cast<MutablePatternModifier*>(const_cast<Modifier*>(micros.modMiddle))
+            ->setPatternInfo(&patternInfo);
         numDigits = patternInfo.positive.integerTotal;
     }
 
index 15e0bda090d0cdf46c2526853bb34fea7e33f24e..1582e6e27be147a228d5c5bd5f143bd277b09386 100644 (file)
@@ -80,6 +80,7 @@ static double DOUBLE_MULTIPLIERS[] = {
 
 DecimalQuantity::DecimalQuantity() {
     setBcdToZero();
+    flags = 0;
 }
 
 DecimalQuantity::~DecimalQuantity() {
index 761cd2c45225707ffe9cfbb76d076f60eeb98c58..1ef7e5798a4b3b920675a4c80c8cf9bdb32d69f5 100644 (file)
@@ -30,7 +30,7 @@ class PluralTableSink : public ResourceSink {
         }
     }
 
-    void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) override {
+    void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE {
         ResourceTable pluralsTable = value.getTable(status);
         if (U_FAILURE(status)) { return; }
         for (int i = 0; pluralsTable.getKeyAndValue(i, key, value); ++i) {
index baea2d9940857a9a80dfc2a51721c1228b754cd5..26f0a86f2c95fc55d62d6e1f8b34a068be2d31e0 100644 (file)
@@ -87,7 +87,8 @@ SimpleModifier::SimpleModifier(const SimpleFormatter &simpleFormatter, Field fie
     }
 }
 
-SimpleModifier::SimpleModifier() : fStrong(false), fPrefixLength(0), fSuffixLength(0) {
+SimpleModifier::SimpleModifier()
+        : fField(UNUM_FIELD_COUNT), fStrong(false), fPrefixLength(0), fSuffixLength(0) {
 }
 
 int32_t SimpleModifier::apply(NumberStringBuilder &output, int leftIndex, int rightIndex,
index a04be4e9f9ae03078e8c3aa0702c5e48dfe73dde..05580d3de3f9eb76e7922f8795db81fe631d16db 100644 (file)
@@ -189,28 +189,28 @@ class U_I18N_API MutablePatternModifier
     UnicodeString toUnicodeString() const U_OVERRIDE;
 
   private:
-    // Modifier details
+    // Modifier details (initialized in constructor)
     const bool fStrong;
 
-    // Pattern details
+    // Pattern details (initialized in setPatternInfo and setPatternAttributes)
     const AffixPatternProvider *patternInfo;
     UNumberSignDisplay signDisplay;
     bool perMilleReplacesPercent;
 
-    // Symbol details
+    // Symbol details (initialized in setSymbols)
     const DecimalFormatSymbols *symbols;
     UNumberUnitWidth unitWidth;
     char16_t currencyCode[4];
     const PluralRules *rules;
 
-    // Number details
+    // Number details (initialized in setNumberProperties)
     bool isNegative;
     StandardPlural::Form plural;
 
-    // QuantityChain details
+    // QuantityChain details (initialized in addToChain)
     const MicroPropsGenerator *parent;
 
-    // Transient CharSequence fields
+    // Transient CharSequence fields (initialized in enterCharSequenceMode)
     bool inCharSequenceMode = false;
     int32_t fFlags;
     int32_t fLength;
index 3a9e1c4c801f572c6c8032eaf4b57fb3a65ff730..dd6707e4f285c632c05705cde8a076d872633f1a 100644 (file)
@@ -23,7 +23,7 @@ PatternParser::parseToProperties(const UnicodeString& pattern, IgnoreRounding ig
     return properties;
 }
 
-void PatternParser::parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties properties,
+void PatternParser::parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties& properties,
                                               IgnoreRounding ignoreRounding, UErrorCode &status) {
     parseToExistingPropertiesImpl(pattern, properties, ignoreRounding, status);
 }
@@ -447,7 +447,7 @@ PatternParser::parseToExistingPropertiesImpl(const UnicodeString& pattern, Decim
 }
 
 void PatternParser::patternInfoToProperties(DecimalFormatProperties &properties,
-                                            ParsedPatternInfo patternInfo,
+                                            ParsedPatternInfo& patternInfo,
                                             IgnoreRounding _ignoreRounding, UErrorCode &status) {
     // Translate from PatternParseResult to Properties.
     // Note that most data from "negative" is ignored per the specification of DecimalFormat.
index b47de4f964d1bfa9514e4428cf57f6c5c6b08934..5fbff027344ecb85aa2dff9b9a60445afde8adb0 100644 (file)
@@ -186,7 +186,7 @@ class U_I18N_API PatternParser {
      * @throws IllegalArgumentException
      *             If there was a syntax error in the pattern string.
      */
-    static void parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties properties,
+    static void parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties& properties,
                                           IgnoreRounding ignoreRounding, UErrorCode &status);
 
   private:
@@ -196,7 +196,7 @@ class U_I18N_API PatternParser {
 
     /** Finalizes the temporary data stored in the ParsedPatternInfo to the Properties. */
     static void
-    patternInfoToProperties(DecimalFormatProperties &properties, ParsedPatternInfo patternInfo,
+    patternInfoToProperties(DecimalFormatProperties &properties, ParsedPatternInfo& patternInfo,
                             IgnoreRounding _ignoreRounding, UErrorCode &status);
 };
 
index 55f5a8fdd65f4c92d84255cda0d6afeec5c26adb..711b4e2f0a1f54b691d377f1cd8b1a80c07fbfdc 100644 (file)
@@ -74,6 +74,9 @@ public class MacroProps implements Cloneable {
 
   @Override
   public boolean equals(Object _other) {
+    if (_other == null) return false;
+    if (this == _other) return true;
+    if (!(_other instanceof MacroProps)) return false;
     MacroProps other = (MacroProps) _other;
     return Utility.equals(notation, other.notation)
         && Utility.equals(unit, other.unit)
index ca1b8ad59e1842ac4e318ebbe9129f5f4b4a3030..0533ebf4e98f7a8c52ce98a505142a244f884d31 100644 (file)
@@ -4,7 +4,7 @@ package com.ibm.icu.impl.number;
 
 import java.math.BigDecimal;
 
-public class MultiplierImpl implements MicroPropsGenerator, Cloneable {
+public class MultiplierImpl implements MicroPropsGenerator {
   final int magnitudeMultiplier;
   final BigDecimal bigDecimalMultiplier;
   final MicroPropsGenerator parent;
index 6de0284e6f242b1e0fe285682a9a38c8adc68859..94b64be1ada514c7760dd546d3898a579be6082a 100644 (file)
@@ -22,7 +22,8 @@ public class SimpleModifier implements Modifier {
 
     /** Creates a modifier that uses the SimpleFormatter string formats. */
     public SimpleModifier(String compiledPattern, Field field, boolean strong) {
-        this.compiledPattern = (compiledPattern == null) ? "\u0001\u0000" : compiledPattern;
+        assert compiledPattern != null;
+        this.compiledPattern = compiledPattern;
         this.field = field;
         this.strong = strong;
 
index fa060faf2dccb221d685e29aea242a6b4eb01ef8..1f89eb7e69c4e9146201dd687f8ee52355cc9f12 100644 (file)
@@ -293,7 +293,7 @@ public class IntlTestDecimalFormatSymbols extends TestFmwk
             "\uD801\uDCA0", "\uD801\uDCA1", "\uD801\uDCA2", "\uD801\uDCA3", "\uD801\uDCA4",
             "\uD801\uDCA5", "\uD801\uDCA6", "\uD801\uDCA7", "\uD801\uDCA8", "\uD801\uDCA9"
         };
-        final String[] differentDigitStrings = {"0", "b", "3", "d", "5", "f", "7", "h", "9", "j"};
+        final String[] differentDigitStrings = {"0", "b", "3", "d", "5", "ff", "7", "h", "9", "j"};
 
         DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);