From 22196980f4bb0a6c08d77659555549039d72f2c2 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Sun, 11 Apr 2021 12:00:51 +0700 Subject: [PATCH] SWEEP: Reviewed all catch blocks where Lucene uses NumberFormatException and converted them to use our IsNumberFormatException() extension method (see #446). --- .../ByTask/Tasks/AnalyzerFactoryTask.cs | 6 +-- .../Quality/QualityQuery.cs | 2 +- .../Xml/Builders/NumericRangeFilterBuilder.cs | 2 +- .../Xml/Builders/NumericRangeQueryBuilder.cs | 2 +- .../Suggest/FileDictionary.cs | 2 +- .../Search/CheckHits.cs | 16 +++--- src/Lucene.Net.Tests/Util/TestArrayUtil.cs | 20 +++---- src/Lucene.Net.Tests/Util/TestNumericUtils.cs | 8 +-- src/Lucene.Net/Search/FieldCacheImpl.cs | 8 +-- src/Lucene.Net/Util/Automaton/RegExp.cs | 52 +++++++++---------- 10 files changed, 53 insertions(+), 65 deletions(-) diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/AnalyzerFactoryTask.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/AnalyzerFactoryTask.cs index b35e39e390..35028c05e8 100644 --- a/src/Lucene.Net.Benchmark/ByTask/Tasks/AnalyzerFactoryTask.cs +++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/AnalyzerFactoryTask.cs @@ -172,7 +172,7 @@ public override void SetParams(string @params) { intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture); } - catch (Exception e) when (e.IsParseException()) + catch (Exception e) when (e.IsNumberFormatException()) { throw new Exception ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e); @@ -204,7 +204,7 @@ public override void SetParams(string @params) { intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture); } - catch (Exception e) when (e.IsParseException()) + catch (Exception e) when (e.IsNumberFormatException()) { throw new Exception ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e); @@ -257,7 +257,7 @@ public override void SetParams(string @params) { intArgValue = int.Parse(stok.StringValue.Trim(), CultureInfo.InvariantCulture); } - catch (Exception e) when (e.IsParseException()) + catch (Exception e) when (e.IsNumberFormatException()) { throw new Exception ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + stok.StringValue + "'", e); diff --git a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs index 1074d1a2ff..cc45910d14 100644 --- a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs +++ b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs @@ -93,7 +93,7 @@ public virtual int CompareTo(QualityQuery other) int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture); return n - nOther; } - catch (Exception e) when (e.IsParseException()) + catch (Exception e) when (e.IsNumberFormatException()) { // fall back to string comparison return queryID.CompareToOrdinal(other.queryID); diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs index 443291c67f..51363580da 100644 --- a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs +++ b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs @@ -150,7 +150,7 @@ public virtual Filter GetFilter(XmlElement e) } return filter; } - catch (FormatException nfe) + catch (Exception nfe) when (nfe.IsNumberFormatException()) { if (strictMode) { diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs index 80f1bae71f..de221ee41b 100644 --- a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs +++ b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs @@ -128,7 +128,7 @@ public virtual Query GetQuery(XmlElement e) } return filter; } - catch (FormatException nfe) + catch (Exception nfe) when (nfe.IsNumberFormatException()) { throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe); } diff --git a/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs b/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs index 6ab979ce6c..4987123731 100644 --- a/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs +++ b/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs @@ -253,7 +253,7 @@ internal void ReadWeight(string weight) // keep reading floats for bw compat curWeight = (long)double.Parse(weight, NumberStyles.Float, CultureInfo.InvariantCulture); } - catch (FormatException e) + catch (Exception e) when (e.IsNumberFormatException()) { // LUCENENET: This is just so we can see what string and what culture was being tested when parsing failed, // to try to reproduce the conditions of the failure. diff --git a/src/Lucene.Net.TestFramework/Search/CheckHits.cs b/src/Lucene.Net.TestFramework/Search/CheckHits.cs index 1a72af3fbe..9f4e19b137 100644 --- a/src/Lucene.Net.TestFramework/Search/CheckHits.cs +++ b/src/Lucene.Net.TestFramework/Search/CheckHits.cs @@ -403,17 +403,18 @@ public static void VerifyExplanation(string q, int doc, float score, bool deep, // LUCENENET NOTE: Using current culture here is intentional because // we are parsing from text that was made using the current culture. - if (float.TryParse(descr.Substring(k1, k2 - k1).Trim(), out x)) + if (float.TryParse(descr.Substring(k1, k2 - k1).Trim(), out x) && + descr.Substring(k2).Trim().Equals("times others of:", StringComparison.Ordinal)) { - if (descr.Substring(k2).Trim().Equals("times others of:", StringComparison.Ordinal)) - { - maxTimesOthers = true; - } + maxTimesOthers = true; } } } // TODO: this is a TERRIBLE assertion!!!! - Assert.IsTrue(productOf || sumOf || maxOf || maxTimesOthers, q + ": multi valued explanation description=\"" + descr + "\" must be 'max of plus x times others' or end with 'product of'" + " or 'sum of:' or 'max of:' - " + expl); + Assert.IsTrue(productOf || sumOf || maxOf || maxTimesOthers, + q + ": multi valued explanation description=\"" + descr + + "\" must be 'max of plus x times others' or end with 'product of'" + + " or 'sum of:' or 'max of:' - " + expl); float sum = 0; float product = 1; float max = 0; @@ -446,7 +447,8 @@ public static void VerifyExplanation(string q, int doc, float score, bool deep, { Assert.IsTrue(false, "should never get here!"); } - Assert.AreEqual(combined, value, ExplainToleranceDelta(combined, value), q + ": actual subDetails combined==" + combined + " != value=" + value + " Explanation: " + expl); + Assert.AreEqual(combined, value, ExplainToleranceDelta(combined, value), + q + ": actual subDetails combined==" + combined + " != value=" + value + " Explanation: " + expl); } } } diff --git a/src/Lucene.Net.Tests/Util/TestArrayUtil.cs b/src/Lucene.Net.Tests/Util/TestArrayUtil.cs index 31eeef9e9d..4e1126ebbe 100644 --- a/src/Lucene.Net.Tests/Util/TestArrayUtil.cs +++ b/src/Lucene.Net.Tests/Util/TestArrayUtil.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support; +using Lucene.Net.Support; using NUnit.Framework; using System; using Assert = Lucene.Net.TestFramework.Assert; @@ -82,9 +82,7 @@ public virtual void TestParseInt() test = ArrayUtil.ParseInt32("".ToCharArray()); Assert.IsTrue(false); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { //expected } @@ -93,9 +91,7 @@ public virtual void TestParseInt() test = ArrayUtil.ParseInt32("foo".ToCharArray()); Assert.IsTrue(false); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { //expected } @@ -104,9 +100,7 @@ public virtual void TestParseInt() test = ArrayUtil.ParseInt32(Convert.ToString(long.MaxValue).ToCharArray()); Assert.IsTrue(false); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { //expected } @@ -115,9 +109,7 @@ public virtual void TestParseInt() test = ArrayUtil.ParseInt32("0.34".ToCharArray()); Assert.IsTrue(false); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { //expected } @@ -135,7 +127,7 @@ public virtual void TestParseInt() test = ArrayUtil.ParseInt32("foo 1923 bar".ToCharArray(), 4, 4); Assert.IsTrue(test == 1923, test + " does not equal: " + 1923); } - catch (FormatException e) + catch (Exception e) when (e.IsNumberFormatException()) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); diff --git a/src/Lucene.Net.Tests/Util/TestNumericUtils.cs b/src/Lucene.Net.Tests/Util/TestNumericUtils.cs index a654c9728b..326eeb6c0d 100644 --- a/src/Lucene.Net.Tests/Util/TestNumericUtils.cs +++ b/src/Lucene.Net.Tests/Util/TestNumericUtils.cs @@ -94,9 +94,7 @@ public virtual void TestLongSpecialValues() NumericUtils.PrefixCodedToInt32(prefixVals[i]); Assert.Fail("decoding a prefix coded long value as int should fail"); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { // worked } @@ -142,9 +140,7 @@ public virtual void TestIntSpecialValues() NumericUtils.PrefixCodedToInt64(prefixVals[i]); Assert.Fail("decoding a prefix coded int value as long should fail"); } -#pragma warning disable 168 - catch (FormatException e) -#pragma warning restore 168 + catch (Exception e) when (e.IsNumberFormatException()) { // worked } diff --git a/src/Lucene.Net/Search/FieldCacheImpl.cs b/src/Lucene.Net/Search/FieldCacheImpl.cs index 31df6cca8f..0bb93b0ce3 100644 --- a/src/Lucene.Net/Search/FieldCacheImpl.cs +++ b/src/Lucene.Net/Search/FieldCacheImpl.cs @@ -1043,7 +1043,7 @@ protected override FieldCache.Int32s CreateValue(AtomicReader reader, CacheKey imax) - { - int t = imin; - imin = imax; - imax = t; - } - return MakeInterval(imin, imax, digits); + throw new ArgumentException("interval syntax error at position " + (pos - 1)); + } + string smin = s.Substring(0, i); + string smax = s.Substring(i + 1, s.Length - (i + 1)); + + if (!int.TryParse(smin, NumberStyles.Integer, CultureInfo.InvariantCulture, out int imin) || + !int.TryParse(smax, NumberStyles.Integer, CultureInfo.InvariantCulture, out int imax)) + throw new ArgumentException("interval syntax error at position " + (pos - 1)); + + int digits; + if (smin.Length == smax.Length) + { + digits = smin.Length; + } + else + { + digits = 0; } - catch (FormatException e) + if (imin > imax) { - throw new ArgumentException("interval syntax error at position " + (pos - 1), e); + int t = imin; + imin = imax; + imax = t; } + return MakeInterval(imin, imax, digits); } } else