Skip to content

Commit 4759c74

Browse files
committed
fix: make number parsing more strict
1 parent 180e505 commit 4759c74

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

android-core/src/androidTest/kotlin/com.mparticle/MPUserTest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class MPUserTest : BaseCleanStartedEachTest() {
6161
) {
6262
assertEquals(6, userAttributes.size)
6363
assertEquals("bar", userAttributes["foo"])
64-
assertEquals(123L, userAttributes["fooInt"])
65-
assertEquals(12345L, userAttributes["fooLong"])
64+
assertEquals(123, userAttributes["fooInt"])
65+
assertEquals(12345, userAttributes["fooLong"])
6666
assertEquals(10.15, userAttributes["fooDouble"])
67-
assertEquals(-10L, userAttributes["fooNegInt"])
68-
assertEquals(-1010L, userAttributes["fooNegLong"])
67+
assertEquals(-10, userAttributes["fooNegInt"])
68+
assertEquals(-1010, userAttributes["fooNegLong"])
6969
assertEquals(null, userAttributes["fooNull"])
7070
}
7171
})
@@ -83,12 +83,12 @@ class MPUserTest : BaseCleanStartedEachTest() {
8383
incrementUserAttribute("foo", 3)
8484

8585
android_test_hack()
86-
assertEquals(4L, userAttributes["foo"])
86+
assertEquals(4, userAttributes["foo"])
8787

8888
// test negative increment
8989
incrementUserAttribute("foo", -2)
9090
android_test_hack()
91-
assertEquals(2L, userAttributes["foo"])
91+
assertEquals(2, userAttributes["foo"])
9292

9393
// test remove incremented attribute
9494
removeUserAttribute("foo")
@@ -130,20 +130,20 @@ class MPUserTest : BaseCleanStartedEachTest() {
130130
fun testIncrementLongAttribute() {
131131
MParticle.getInstance()!!.Identity().currentUser!!.apply {
132132
assertTrue { getUserAttributes().isEmpty() }
133-
setUserAttribute("foo", 10L)
133+
setUserAttribute("foo", 10)
134134

135135
android_test_hack()
136136
assertEquals(1, userAttributes.size)
137-
assertEquals(10L, userAttributes["foo"])
138-
incrementUserAttribute("foo", 37L)
137+
assertEquals(10, userAttributes["foo"])
138+
incrementUserAttribute("foo", 37)
139139

140140
android_test_hack()
141-
assertEquals(47L, userAttributes["foo"])
141+
assertEquals(47, userAttributes["foo"])
142142

143143
// test negative increment
144-
incrementUserAttribute("foo", -21L)
144+
incrementUserAttribute("foo", -21)
145145
android_test_hack()
146-
assertEquals(26L, userAttributes["foo"])
146+
assertEquals(26, userAttributes["foo"])
147147

148148
// test remove incremented attribute
149149
removeUserAttribute("foo")

android-core/src/androidTest/kotlin/com.mparticle/MParticleTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,6 @@ class MParticleTest : BaseCleanStartedEachTest() {
169169
}
170170
}
171171

172-
@OrchestratorOnly
173-
@Test
174-
@Throws(JSONException::class, InterruptedException::class)
175-
fun testResetSync() {
176-
testReset { MParticle.reset(mContext) }
177-
}
178-
179172
@OrchestratorOnly
180173
@Test
181174
@Throws(JSONException::class, InterruptedException::class)

android-core/src/main/java/com/mparticle/internal/MPUtility.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,17 +833,20 @@ public static Number addNumbers(Number number1, Number number2) {
833833

834834
public static Object toNumberOrString(String stringValue) {
835835
if (stringValue == null) {
836-
return stringValue;
836+
return null;
837837
}
838838
for (Character c : stringValue.toCharArray()) {
839839
if (!Character.isDigit(c) && c != '.' && c != '-') {
840840
return stringValue;
841841
}
842842
}
843843
try {
844-
return NumberFormat.getInstance().parse(stringValue);
845-
} catch (ParseException e) {
846-
}
844+
return Integer.parseInt(stringValue);
845+
} catch (NumberFormatException ignored){}
846+
try {
847+
return Double.parseDouble(stringValue);
848+
} catch (NumberFormatException ignored){}
849+
847850
return stringValue;
848851
}
849852

android-core/src/test/kotlin/com/mparticle/internal/MPUtilityTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ class MPUtilityTest {
236236

237237
@Test
238238
fun testNumberDetection() {
239-
Assert.assertEquals(12L, MPUtility.toNumberOrString("12"))
239+
Assert.assertEquals(12, MPUtility.toNumberOrString("12"))
240240
Assert.assertEquals(1.5, MPUtility.toNumberOrString("1.5"))
241241
Assert.assertEquals(-1.5, MPUtility.toNumberOrString("-1.5"))
242-
Assert.assertEquals(0L, MPUtility.toNumberOrString("0"))
242+
Assert.assertEquals(0, MPUtility.toNumberOrString("0"))
243243
// too big for a Long, should return a String
244244
Assert.assertEquals(
245245
3.245987293478593E47,

0 commit comments

Comments
 (0)