ICU-20916 UBSan & ErrorProne fixes

This commit is contained in:
Markus Scherer 2019-12-20 14:28:01 -08:00
parent ad638c274e
commit cb1d4f5903
4 changed files with 22 additions and 13 deletions

View file

@ -103,7 +103,7 @@ int32_t LSR::indexForRegion(const char *region) {
LSR &LSR::setHashCode() {
if (hashCode == 0) {
int32_t h = ustr_hashCharsN(language, static_cast<int32_t>(uprv_strlen(language)));
uint32_t h = ustr_hashCharsN(language, static_cast<int32_t>(uprv_strlen(language)));
h = h * 37 + ustr_hashCharsN(script, static_cast<int32_t>(uprv_strlen(script)));
h = h * 37 + regionIndex;
hashCode = h * 37 + flags;

View file

@ -173,7 +173,7 @@ public class LocaleDistance {
@Override
public boolean equals(Object other) {
if (this == other) { return true; }
if (!getClass().equals(other.getClass())) { return false; }
if (other == null || !getClass().equals(other.getClass())) { return false; }
Data od = (Data)other;
return Arrays.equals(trie, od.trie) &&
Arrays.equals(regionToPartitionsIndex, od.regionToPartitionsIndex) &&
@ -181,6 +181,11 @@ public class LocaleDistance {
paradigmLSRs.equals(od.paradigmLSRs) &&
Arrays.equals(distances, od.distances);
}
@Override
public int hashCode() { // unused; silence ErrorProne
return 1;
}
}
// VisibleForTesting

View file

@ -97,7 +97,7 @@ public final class XLikelySubtags {
@Override
public boolean equals(Object other) {
if (this == other) { return true; }
if (!getClass().equals(other.getClass())) { return false; }
if (other == null || !getClass().equals(other.getClass())) { return false; }
Data od = (Data)other;
return
languageAliases.equals(od.languageAliases) &&
@ -105,6 +105,11 @@ public final class XLikelySubtags {
Arrays.equals(trie, od.trie) &&
Arrays.equals(lsrs, od.lsrs);
}
@Override
public int hashCode() { // unused; silence ErrorProne
return 1;
}
}
// VisibleForTesting

View file

@ -15,7 +15,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import com.ibm.icu.impl.locale.LSR;
import com.ibm.icu.impl.locale.LocaleDistance;
@ -522,19 +521,19 @@ public final class LocaleMatcher {
public String toString() {
StringBuilder s = new StringBuilder().append("{LocaleMatcher.Builder");
if (supportedLocales != null && !supportedLocales.isEmpty()) {
s.append(" supported={").append(supportedLocales.toString()).append('}');
s.append(" supported={").append(supportedLocales).append('}');
}
if (defaultLocale != null) {
s.append(" default=").append(defaultLocale.toString());
s.append(" default=").append(defaultLocale);
}
if (favor != null) {
s.append(" distance=").append(favor.toString());
s.append(" distance=").append(favor);
}
if (thresholdDistance >= 0) {
s.append(String.format(" threshold=%d", thresholdDistance));
}
if (demotion != null) {
s.append(" demotion=").append(demotion.toString());
s.append(" demotion=").append(demotion);
}
return s.append('}').toString();
}
@ -1018,7 +1017,7 @@ public final class LocaleMatcher {
double distance = LocaleDistance.getDistanceDouble(indexAndDistance);
if (TRACE_MATCHER) {
System.err.printf("LocaleMatcher distance(desired=%s, supported=%s)=%g\n",
Objects.toString(desired), Objects.toString(supported), distance);
String.valueOf(desired), String.valueOf(supported), distance);
}
return (100.0 - distance) / 100.0;
}
@ -1050,15 +1049,15 @@ public final class LocaleMatcher {
StringBuilder s = new StringBuilder().append("{LocaleMatcher");
// Supported languages in the order that we try to match them.
if (supportedLSRs.length > 0) {
s.append(" supportedLSRs={").append(supportedLSRs[0].toString());
s.append(" supportedLSRs={").append(supportedLSRs[0]);
for (int i = 1; i < supportedLSRs.length; ++i) {
s.append(", ").append(supportedLSRs[i].toString());
s.append(", ").append(supportedLSRs[i]);
}
s.append('}');
}
s.append(" default=").append(Objects.toString(defaultULocale));
s.append(" default=").append(defaultULocale);
if (favorSubtag != null) {
s.append(" favor=").append(favorSubtag.toString());
s.append(" favor=").append(favorSubtag);
}
if (thresholdDistance >= 0) {
s.append(String.format(" threshold=%d", thresholdDistance));