Skip to content

Commit

Permalink
fix: make number parsing more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Dozor committed Mar 2, 2024
1 parent 180e505 commit 4759c74
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
24 changes: 12 additions & 12 deletions android-core/src/androidTest/kotlin/com.mparticle/MPUserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class MPUserTest : BaseCleanStartedEachTest() {
) {
assertEquals(6, userAttributes.size)
assertEquals("bar", userAttributes["foo"])
assertEquals(123L, userAttributes["fooInt"])
assertEquals(12345L, userAttributes["fooLong"])
assertEquals(123, userAttributes["fooInt"])
assertEquals(12345, userAttributes["fooLong"])
assertEquals(10.15, userAttributes["fooDouble"])
assertEquals(-10L, userAttributes["fooNegInt"])
assertEquals(-1010L, userAttributes["fooNegLong"])
assertEquals(-10, userAttributes["fooNegInt"])
assertEquals(-1010, userAttributes["fooNegLong"])
assertEquals(null, userAttributes["fooNull"])
}
})
Expand All @@ -83,12 +83,12 @@ class MPUserTest : BaseCleanStartedEachTest() {
incrementUserAttribute("foo", 3)

android_test_hack()
assertEquals(4L, userAttributes["foo"])
assertEquals(4, userAttributes["foo"])

// test negative increment
incrementUserAttribute("foo", -2)
android_test_hack()
assertEquals(2L, userAttributes["foo"])
assertEquals(2, userAttributes["foo"])

// test remove incremented attribute
removeUserAttribute("foo")
Expand Down Expand Up @@ -130,20 +130,20 @@ class MPUserTest : BaseCleanStartedEachTest() {
fun testIncrementLongAttribute() {
MParticle.getInstance()!!.Identity().currentUser!!.apply {
assertTrue { getUserAttributes().isEmpty() }
setUserAttribute("foo", 10L)
setUserAttribute("foo", 10)

android_test_hack()
assertEquals(1, userAttributes.size)
assertEquals(10L, userAttributes["foo"])
incrementUserAttribute("foo", 37L)
assertEquals(10, userAttributes["foo"])
incrementUserAttribute("foo", 37)

android_test_hack()
assertEquals(47L, userAttributes["foo"])
assertEquals(47, userAttributes["foo"])

// test negative increment
incrementUserAttribute("foo", -21L)
incrementUserAttribute("foo", -21)
android_test_hack()
assertEquals(26L, userAttributes["foo"])
assertEquals(26, userAttributes["foo"])

// test remove incremented attribute
removeUserAttribute("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@ class MParticleTest : BaseCleanStartedEachTest() {
}
}

@OrchestratorOnly
@Test
@Throws(JSONException::class, InterruptedException::class)
fun testResetSync() {
testReset { MParticle.reset(mContext) }
}

@OrchestratorOnly
@Test
@Throws(JSONException::class, InterruptedException::class)
Expand Down
11 changes: 7 additions & 4 deletions android-core/src/main/java/com/mparticle/internal/MPUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -833,17 +833,20 @@ public static Number addNumbers(Number number1, Number number2) {

public static Object toNumberOrString(String stringValue) {
if (stringValue == null) {
return stringValue;
return null;
}
for (Character c : stringValue.toCharArray()) {
if (!Character.isDigit(c) && c != '.' && c != '-') {
return stringValue;
}
}
try {
return NumberFormat.getInstance().parse(stringValue);
} catch (ParseException e) {
}
return Integer.parseInt(stringValue);
} catch (NumberFormatException ignored){}
try {
return Double.parseDouble(stringValue);
} catch (NumberFormatException ignored){}

return stringValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ class MPUtilityTest {

@Test
fun testNumberDetection() {
Assert.assertEquals(12L, MPUtility.toNumberOrString("12"))
Assert.assertEquals(12, MPUtility.toNumberOrString("12"))
Assert.assertEquals(1.5, MPUtility.toNumberOrString("1.5"))
Assert.assertEquals(-1.5, MPUtility.toNumberOrString("-1.5"))
Assert.assertEquals(0L, MPUtility.toNumberOrString("0"))
Assert.assertEquals(0, MPUtility.toNumberOrString("0"))
// too big for a Long, should return a String
Assert.assertEquals(
3.245987293478593E47,
Expand Down

0 comments on commit 4759c74

Please sign in to comment.