From a36a676d4313225bbcec44120eb38ce871106d0a Mon Sep 17 00:00:00 2001
From: Bohdan Artiukhov <69891500+bohdanprog@users.noreply.github.com>
Date: Thu, 20 Jun 2024 14:16:39 +0200
Subject: [PATCH] fix: android PathParser crash app if pass some wrong d prop
(#2308)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes #2086
# Summary
The application crashes if an error is thrown when something goes wrong
during path parse.
## Test Plan
You can easily check in that component `Test2086` how it works after the
fix.
## Compatibility
| OS | Implemented |
| ------- | :---------: |
| iOS | ✅ |
| Android | ✅ |
---
TestsExample/App.js | 1 +
TestsExample/src/Test2086.tsx | 45 +++++++++++++++++++
.../main/java/com/horcrux/svg/PathParser.java | 18 ++++----
3 files changed, 54 insertions(+), 10 deletions(-)
create mode 100644 TestsExample/src/Test2086.tsx
diff --git a/TestsExample/App.js b/TestsExample/App.js
index 39724f5f..96451eb3 100644
--- a/TestsExample/App.js
+++ b/TestsExample/App.js
@@ -10,6 +10,7 @@ import Test1845 from './src/Test1845';
import Test1986 from './src/Test1986';
import Test2071 from './src/Test2071';
import Test2080 from './src/Test2080';
+import Test2086 from './src/Test2086';
import Test2089 from './src/Test2089';
import Test2148 from './src/Test2148';
import Test2196 from './src/Test2196';
diff --git a/TestsExample/src/Test2086.tsx b/TestsExample/src/Test2086.tsx
new file mode 100644
index 00000000..ef478d64
--- /dev/null
+++ b/TestsExample/src/Test2086.tsx
@@ -0,0 +1,45 @@
+import React, {useState} from 'react';
+import Svg, {Path} from 'react-native-svg';
+import {StyleSheet, Text, View, Button} from 'react-native';
+
+export default function Test2086() {
+ const [display, setDisplay] = useState(false);
+ return (
+
+
+ Click on the button to crash the app
+
+
+ );
+}
+
+const MySVG = ({style, toggle}) => {
+ return (
+
+ );
+};
+
+const styles = StyleSheet.create({
+ container: {
+ paddingVertical: 200,
+ flex: 1,
+ backgroundColor: '#fff',
+ alignItems: 'center',
+ justifyContent: 'center',
+ },
+});
diff --git a/android/src/main/java/com/horcrux/svg/PathParser.java b/android/src/main/java/com/horcrux/svg/PathParser.java
index f618030e..18d3d28a 100644
--- a/android/src/main/java/com/horcrux/svg/PathParser.java
+++ b/android/src/main/java/com/horcrux/svg/PathParser.java
@@ -62,7 +62,7 @@ class PathParser {
if (!has_prev_cmd && first_char != 'M' && first_char != 'm') {
// The first segment must be a MoveTo.
- throw new Error(String.format("Unexpected character '%c' (i=%d, s=%s)", first_char, i, s));
+ throw new IllegalArgumentException(String.format("Unexpected character '%c' (i=%d, s=%s)", first_char, i, s));
}
// TODO: simplify
@@ -75,7 +75,7 @@ class PathParser {
} else if (is_number_start(first_char) && has_prev_cmd) {
if (prev_cmd == 'Z' || prev_cmd == 'z') {
// ClosePath cannot be followed by a number.
- throw new Error(String.format("Unexpected number after 'z' (s=%s)", s));
+ throw new IllegalArgumentException(String.format("Unexpected number after 'z' (s=%s)", s));
}
if (prev_cmd == 'M' || prev_cmd == 'm') {
@@ -93,7 +93,7 @@ class PathParser {
cmd = prev_cmd;
}
} else {
- throw new Error(String.format("Unexpected character '%c' (i=%d, s=%s)", first_char, i, s));
+ throw new IllegalArgumentException(String.format("Unexpected character '%c' (i=%d, s=%s)", first_char, i, s));
}
boolean absolute = is_absolute(cmd);
@@ -226,7 +226,8 @@ class PathParser {
}
default:
{
- throw new Error(String.format("Unexpected comand '%c' (s=%s)", cmd, s));
+ throw new IllegalArgumentException(String.format("Unexpected comand '%c' (s=%s)", cmd, s));
+
}
}
@@ -623,8 +624,7 @@ class PathParser {
c = s.charAt(i);
}
} else if (c != '.') {
- throw new Error(
- String.format("Invalid number formating character '%c' (i=%d, s=%s)", c, i, s));
+ throw new IllegalArgumentException(String.format("Invalid number formating character '%c' (i=%d, s=%s)", c, i, s));
}
// Consume fraction.
@@ -649,8 +649,7 @@ class PathParser {
} else if (c >= '0' && c <= '9') {
skip_digits();
} else {
- throw new Error(
- String.format("Invalid number formating character '%c' (i=%d, s=%s)", c, i, s));
+ throw new IllegalArgumentException(String.format("Invalid number formating character '%c' (i=%d, s=%s)", c, i, s));
}
}
}
@@ -660,8 +659,7 @@ class PathParser {
// inf, nan, etc. are an error.
if (Float.isInfinite(n) || Float.isNaN(n)) {
- throw new Error(
- String.format("Invalid number '%s' (start=%d, i=%d, s=%s)", num, start, i, s));
+ throw new IllegalArgumentException(String.format("Invalid number '%s' (start=%d, i=%d, s=%s)", num, start, i, s));
}
return n;