Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
briar
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Julian Dehm
briar
Commits
eb3983f6
Verified
Commit
eb3983f6
authored
Oct 29, 2018
by
akwizgran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use safe ASCII decoding in ModemImpl.
parent
e2ce49c3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
4 deletions
+22
-4
ModemImpl.java
...java/org/briarproject/bramble/plugin/modem/ModemImpl.java
+22
-4
No files found.
bramble-java/src/main/java/org/briarproject/bramble/plugin/modem/ModemImpl.java
View file @
eb3983f6
...
...
@@ -10,6 +10,10 @@ import org.briarproject.bramble.api.system.Clock;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.nio.ByteBuffer
;
import
java.nio.charset.CharacterCodingException
;
import
java.nio.charset.Charset
;
import
java.nio.charset.CharsetDecoder
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.Semaphore
;
import
java.util.concurrent.locks.Condition
;
...
...
@@ -22,6 +26,7 @@ import javax.annotation.Nullable;
import
jssc.SerialPortEvent
;
import
jssc.SerialPortEventListener
;
import
static
java
.
nio
.
charset
.
CodingErrorAction
.
IGNORE
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
MILLISECONDS
;
import
static
java
.
util
.
logging
.
Level
.
INFO
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
...
...
@@ -35,6 +40,8 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
ModemImpl
.
class
.
getName
());
private
static
final
Charset
US_ASCII
=
Charset
.
forName
(
"US-ASCII"
);
private
static
final
int
MAX_LINE_LENGTH
=
256
;
private
static
final
int
[]
BAUD_RATES
=
{
256000
,
128000
,
115200
,
57600
,
38400
,
19200
,
14400
,
9600
,
4800
,
1200
...
...
@@ -106,7 +113,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
throw
e
;
}
// Wait for the event thread to receive "OK"
boolean
success
=
false
;
boolean
success
;
try
{
lock
.
lock
();
try
{
...
...
@@ -353,8 +360,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
for
(
int
i
=
0
;
i
<
b
.
length
;
i
++)
{
line
[
lineLen
]
=
b
[
i
];
if
(
b
[
i
]
==
'\n'
)
{
// FIXME: Use CharsetDecoder to catch invalid ASCII
String
s
=
new
String
(
line
,
0
,
lineLen
,
"US-ASCII"
).
trim
();
String
s
=
toAscii
(
line
,
lineLen
).
trim
();
lineLen
=
0
;
if
(
LOG
.
isLoggable
(
INFO
))
LOG
.
info
(
"Modem status: "
+
s
);
if
(
s
.
startsWith
(
"CONNECT"
))
{
...
...
@@ -436,7 +442,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
throw
e
;
}
// Wait for the event thread to receive "CONNECT"
boolean
success
=
false
;
boolean
success
;
try
{
lock
.
lock
();
try
{
...
...
@@ -461,4 +467,16 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
stateChange
.
release
();
}
}
private
String
toAscii
(
byte
[]
bytes
,
int
len
)
{
CharsetDecoder
decoder
=
US_ASCII
.
newDecoder
();
decoder
.
onMalformedInput
(
IGNORE
);
decoder
.
onUnmappableCharacter
(
IGNORE
);
ByteBuffer
buffer
=
ByteBuffer
.
wrap
(
bytes
,
0
,
len
);
try
{
return
decoder
.
decode
(
buffer
).
toString
();
}
catch
(
CharacterCodingException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment