Skip to content

Commit 65e9ea7

Browse files
committed
ANDROID: Added range checking for the web services port
1 parent 69a2d7f commit 65e9ea7

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2021-04-02 (12.21)
2+
ANDROID: Added range checking for the web services port
3+
14
2021-03-28 (12.21)
25
COMMON: Fix CIRCLE command to ensure radius uses the WINDOW coordinate system
36
COMMON: Fix to ensure the default VIEW is maintained during resizing

src/platform/android/app/src/main/assets/main.bas

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,12 @@ sub do_setup()
160160
cls
161161
print boldOn + "Setup web service port number."
162162
print boldOff
163-
print "Enter a port number to allow web browser or desktop IDE access. ";
164-
print "Enter -1 to disable this feature, or press <enter> to leave ";
165-
print "this screen without making any changes."
163+
print "Enter a port number to allow web browser or desktop IDE access. "
164+
print
165+
print "Values outside the range [1024-65535] will disable this feature."
166+
print
167+
print "Press <enter> to leave this screen without making any changes."
168+
print
166169
print "The current setting is: " + env("serverSocket")
167170
print
168171
color colText, colBkGnd
@@ -211,7 +214,7 @@ sub server_info()
211214
local serverSocket = env("serverSocket")
212215
local ipAddr = env("IP_ADDR")
213216

214-
if (len(serverSocket) > 0 && int(serverSocket) > 0 && len(ipAddr)) then
217+
if (len(serverSocket) > 0 && int(serverSocket) > 1023 && int(serverSocket) < 65536 && len(ipAddr)) then
215218
serverSocket = ipAddr + ":" + serverSocket
216219
print boldOff + "Web Service: " + boldOn + serverSocket
217220
print boldOff + "Access token: " + boldOn + env("serverToken")

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void browseFile(final byte[] pathBytes) {
198198
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
199199
startActivity(browserIntent);
200200
} catch (Exception e) {
201-
Log.i(TAG, "browseFile failed: " + e.toString());
201+
Log.i(TAG, "browseFile failed: ", e);
202202
}
203203
}
204204

@@ -443,7 +443,7 @@ public void run() {
443443
_mediaPlayer.start();
444444
}
445445
catch (IOException e) {
446-
Log.i(TAG, "Failed: ", e);
446+
Log.i(TAG, "playAudio failed: ", e);
447447
}
448448
}
449449
}).start();
@@ -920,7 +920,7 @@ private void processSettings() {
920920
p.load(getApplication().openFileInput("settings.txt"));
921921
int socket = Integer.parseInt(p.getProperty("serverSocket", "-1"));
922922
String token = p.getProperty("serverToken", new Date().toString());
923-
if (socket != -1) {
923+
if (socket > 1023 && socket < 65536) {
924924
startServer(socket, token);
925925
} else {
926926
Log.i(TAG, "Web service disabled");
@@ -941,9 +941,9 @@ private String readBuffer(File inputFile) {
941941
}
942942
input.close();
943943
} catch (FileNotFoundException e) {
944-
Log.i(TAG, "Failed: " + e.toString());
944+
Log.i(TAG, "readBuffer failed: ", e);
945945
} catch (IOException e) {
946-
Log.i(TAG, "Failed: " + e.toString());
946+
Log.i(TAG, "readBuffer failed: ", e);
947947
}
948948
return result.toString();
949949
}
@@ -960,10 +960,17 @@ private String readLine(DataInputStream inputReader) throws IOException {
960960
}
961961

962962
private void runServer(final int socketNum, final String token) throws IOException {
963-
ServerSocket serverSocket = new ServerSocket(socketNum);
964963
Log.i(TAG, "Listening :" + socketNum);
965964
Log.i(TAG, "Token :" + token);
966-
while (true) {
965+
ServerSocket serverSocket;
966+
try {
967+
serverSocket = new ServerSocket(socketNum);
968+
}
969+
catch (IllegalArgumentException e) {
970+
Log.i(TAG, "Failed to start server: ", e);
971+
serverSocket = null;
972+
}
973+
while (serverSocket != null) {
967974
Socket socket = null;
968975
DataInputStream inputStream = null;
969976
try {
@@ -1002,7 +1009,8 @@ private void runServer(final int socketNum, final String token) throws IOExcepti
10021009
}
10031010
}
10041011
catch (IOException e) {
1005-
Log.i(TAG, "Failed: " + e.toString());
1012+
Log.i(TAG, "Server failed: ", e);
1013+
break;
10061014
}
10071015
finally {
10081016
Log.i(TAG, "socket cleanup");
@@ -1014,6 +1022,7 @@ private void runServer(final int socketNum, final String token) throws IOExcepti
10141022
}
10151023
}
10161024
}
1025+
Log.i(TAG, "server stopped");
10171026
}
10181027

10191028
private void sendResponse(Socket socket, String content) throws IOException {
@@ -1053,7 +1062,7 @@ public void run() {
10531062
runServer(socketNum, token);
10541063
}
10551064
catch (IOException e) {
1056-
Log.i(TAG, "Failed: " + e.toString());
1065+
Log.i(TAG, "startServer failed: ", e);
10571066
}
10581067
}
10591068
});

0 commit comments

Comments
 (0)