ICU-13177 Fixing coverity issues in number formatting code.

X-SVN-Rev: 40554
This commit is contained in:
Shane Carr 2017-10-04 22:29:21 +00:00
parent e2f537d42f
commit 302c255931
11 changed files with 25 additions and 18 deletions

View File

@ -216,7 +216,6 @@ CompactHandler::CompactHandler(CompactStyle compactStyle, const Locale &locale,
UErrorCode &status) UErrorCode &status)
: rules(rules), parent(parent) { : rules(rules), parent(parent) {
data.populate(locale, nsName, compactStyle, compactType, status); data.populate(locale, nsName, compactStyle, compactType, status);
if (U_FAILURE(status)) { return; }
if (buildReference != nullptr) { if (buildReference != nullptr) {
// Safe code path // Safe code path
precomputeAllModifiers(*buildReference, status); precomputeAllModifiers(*buildReference, status);
@ -234,6 +233,8 @@ CompactHandler::~CompactHandler() {
} }
void CompactHandler::precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status) { void CompactHandler::precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status) {
if (U_FAILURE(status)) { return; }
// Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T
UVector allPatterns(12, status); UVector allPatterns(12, status);
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
@ -306,8 +307,8 @@ void CompactHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micr
// C++ Note: Use unsafePatternInfo for proper lifecycle. // C++ Note: Use unsafePatternInfo for proper lifecycle.
ParsedPatternInfo &patternInfo = const_cast<CompactHandler *>(this)->unsafePatternInfo; ParsedPatternInfo &patternInfo = const_cast<CompactHandler *>(this)->unsafePatternInfo;
PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status); PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
dynamic_cast<MutablePatternModifier *>(const_cast<Modifier *>(micros.modMiddle))->setPatternInfo( static_cast<MutablePatternModifier*>(const_cast<Modifier*>(micros.modMiddle))
&patternInfo); ->setPatternInfo(&patternInfo);
numDigits = patternInfo.positive.integerTotal; numDigits = patternInfo.positive.integerTotal;
} }

View File

@ -80,6 +80,7 @@ static double DOUBLE_MULTIPLIERS[] = {
DecimalQuantity::DecimalQuantity() { DecimalQuantity::DecimalQuantity() {
setBcdToZero(); setBcdToZero();
flags = 0;
} }
DecimalQuantity::~DecimalQuantity() { DecimalQuantity::~DecimalQuantity() {

View 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); ResourceTable pluralsTable = value.getTable(status);
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
for (int i = 0; pluralsTable.getKeyAndValue(i, key, value); ++i) { for (int i = 0; pluralsTable.getKeyAndValue(i, key, value); ++i) {

View 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, int32_t SimpleModifier::apply(NumberStringBuilder &output, int leftIndex, int rightIndex,

View File

@ -189,28 +189,28 @@ class U_I18N_API MutablePatternModifier
UnicodeString toUnicodeString() const U_OVERRIDE; UnicodeString toUnicodeString() const U_OVERRIDE;
private: private:
// Modifier details // Modifier details (initialized in constructor)
const bool fStrong; const bool fStrong;
// Pattern details // Pattern details (initialized in setPatternInfo and setPatternAttributes)
const AffixPatternProvider *patternInfo; const AffixPatternProvider *patternInfo;
UNumberSignDisplay signDisplay; UNumberSignDisplay signDisplay;
bool perMilleReplacesPercent; bool perMilleReplacesPercent;
// Symbol details // Symbol details (initialized in setSymbols)
const DecimalFormatSymbols *symbols; const DecimalFormatSymbols *symbols;
UNumberUnitWidth unitWidth; UNumberUnitWidth unitWidth;
char16_t currencyCode[4]; char16_t currencyCode[4];
const PluralRules *rules; const PluralRules *rules;
// Number details // Number details (initialized in setNumberProperties)
bool isNegative; bool isNegative;
StandardPlural::Form plural; StandardPlural::Form plural;
// QuantityChain details // QuantityChain details (initialized in addToChain)
const MicroPropsGenerator *parent; const MicroPropsGenerator *parent;
// Transient CharSequence fields // Transient CharSequence fields (initialized in enterCharSequenceMode)
bool inCharSequenceMode = false; bool inCharSequenceMode = false;
int32_t fFlags; int32_t fFlags;
int32_t fLength; int32_t fLength;

View File

@ -23,7 +23,7 @@ PatternParser::parseToProperties(const UnicodeString& pattern, IgnoreRounding ig
return properties; return properties;
} }
void PatternParser::parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties properties, void PatternParser::parseToExistingProperties(const UnicodeString& pattern, DecimalFormatProperties& properties,
IgnoreRounding ignoreRounding, UErrorCode &status) { IgnoreRounding ignoreRounding, UErrorCode &status) {
parseToExistingPropertiesImpl(pattern, properties, ignoreRounding, status); parseToExistingPropertiesImpl(pattern, properties, ignoreRounding, status);
} }
@ -447,7 +447,7 @@ PatternParser::parseToExistingPropertiesImpl(const UnicodeString& pattern, Decim
} }
void PatternParser::patternInfoToProperties(DecimalFormatProperties &properties, void PatternParser::patternInfoToProperties(DecimalFormatProperties &properties,
ParsedPatternInfo patternInfo, ParsedPatternInfo& patternInfo,
IgnoreRounding _ignoreRounding, UErrorCode &status) { IgnoreRounding _ignoreRounding, UErrorCode &status) {
// Translate from PatternParseResult to Properties. // Translate from PatternParseResult to Properties.
// Note that most data from "negative" is ignored per the specification of DecimalFormat. // Note that most data from "negative" is ignored per the specification of DecimalFormat.

View File

@ -186,7 +186,7 @@ class U_I18N_API PatternParser {
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If there was a syntax error in the pattern string. * 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); IgnoreRounding ignoreRounding, UErrorCode &status);
private: private:
@ -196,7 +196,7 @@ class U_I18N_API PatternParser {
/** Finalizes the temporary data stored in the ParsedPatternInfo to the Properties. */ /** Finalizes the temporary data stored in the ParsedPatternInfo to the Properties. */
static void static void
patternInfoToProperties(DecimalFormatProperties &properties, ParsedPatternInfo patternInfo, patternInfoToProperties(DecimalFormatProperties &properties, ParsedPatternInfo& patternInfo,
IgnoreRounding _ignoreRounding, UErrorCode &status); IgnoreRounding _ignoreRounding, UErrorCode &status);
}; };

View File

@ -74,6 +74,9 @@ public class MacroProps implements Cloneable {
@Override @Override
public boolean equals(Object _other) { 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; MacroProps other = (MacroProps) _other;
return Utility.equals(notation, other.notation) return Utility.equals(notation, other.notation)
&& Utility.equals(unit, other.unit) && Utility.equals(unit, other.unit)

View File

@ -4,7 +4,7 @@ package com.ibm.icu.impl.number;
import java.math.BigDecimal; import java.math.BigDecimal;
public class MultiplierImpl implements MicroPropsGenerator, Cloneable { public class MultiplierImpl implements MicroPropsGenerator {
final int magnitudeMultiplier; final int magnitudeMultiplier;
final BigDecimal bigDecimalMultiplier; final BigDecimal bigDecimalMultiplier;
final MicroPropsGenerator parent; final MicroPropsGenerator parent;

View File

@ -22,7 +22,8 @@ public class SimpleModifier implements Modifier {
/** Creates a modifier that uses the SimpleFormatter string formats. */ /** Creates a modifier that uses the SimpleFormatter string formats. */
public SimpleModifier(String compiledPattern, Field field, boolean strong) { 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.field = field;
this.strong = strong; this.strong = strong;

View File

@ -293,7 +293,7 @@ public class IntlTestDecimalFormatSymbols extends TestFmwk
"\uD801\uDCA0", "\uD801\uDCA1", "\uD801\uDCA2", "\uD801\uDCA3", "\uD801\uDCA4", "\uD801\uDCA0", "\uD801\uDCA1", "\uD801\uDCA2", "\uD801\uDCA3", "\uD801\uDCA4",
"\uD801\uDCA5", "\uD801\uDCA6", "\uD801\uDCA7", "\uD801\uDCA8", "\uD801\uDCA9" "\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); DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);