Browse files

Merge pull request #2063 from mstepanov/timob-7143

[TIMOB-7143] Android: Connect with invalid port value causes crash
  • Loading branch information...
2 parents 160966b + 8ccf1d7 commit e5d9b55c59e30d70b20b7d4f1e86d974f9a23e94 ayeung committed Apr 24, 2012
View
6 android/modules/network/src/java/ti/modules/titanium/network/socket/TCPProxy.java
@@ -60,7 +60,7 @@ public void connect() throws Exception
if ((state != SocketModule.LISTENING) && (state != SocketModule.CONNECTED)) {
Object host = getProperty("host");
Object port = getProperty("port");
- if((host != null) && (port != null)) {
+ if((host != null) && (port != null) && (TiConvert.toInt(port) > 0)) {
new ConnectedSocketThread().start();
} else {
@@ -206,7 +206,7 @@ public void run()
try {
if (timeoutProperty != null) {
- int timeout = TiConvert.toInt(timeoutProperty);
+ int timeout = TiConvert.toInt(timeoutProperty, 0);
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(host, TiConvert.toInt(getProperty("port"))), timeout);
@@ -248,7 +248,7 @@ public void run()
Object optionValue;
if((optionValue = acceptOptions.get("timeout")) != null) {
- acceptedTcpProxy.setProperty("timeout", TiConvert.toInt(optionValue));
+ acceptedTcpProxy.setProperty("timeout", TiConvert.toInt(optionValue, 0));
}
if((optionValue = acceptOptions.get("error")) != null) {
if(optionValue instanceof KrollFunction) {
View
17 android/titanium/src/java/org/appcelerator/titanium/util/TiConvert.java
@@ -382,6 +382,23 @@ public static int toInt(Object value)
}
/**
+ * If value is a Double, Integer, Long or String, converts it to Integer. Otherwise
+ * returns default value.
+ * @param value the value to convert.
+ * @param def the default value to return
+ * @return an int value.
+ * @module.api
+ */
+ public static int toInt(Object value, int def)
+ {
+ try {
+ return toInt(value);
+ } catch (NumberFormatException e) {
+ return def;
+ }
+ }
+
+ /**
* Takes a value out of a hash table then attempts to convert it using {@link #toInt(Object)}.
* @param hashMap the hash map to search.
* @param key the lookup key.

0 comments on commit e5d9b55

Please sign in to comment.