Skip to content

Commit

Permalink
Solves #119
Browse files Browse the repository at this point in the history
Use compiled patterns in the org.freedesktop.dbus.connections.AbstractConnection
  • Loading branch information
Oleg Kleshchev committed Oct 25, 2020
1 parent 6fbd45a commit da1a416
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public abstract class AbstractConnection implements Closeable {
/** Lame method to setup endianness used on DBus messages */
private static byte endianness = getSystemEndianness();

public static final boolean FLOAT_SUPPORT = (null != System.getenv("DBUS_JAVA_FLOATS"));
public static final String BUSNAME_REGEX = "^[-_a-zA-Z][-_a-zA-Z0-9]*(\\.[-_a-zA-Z][-_a-zA-Z0-9]*)*$";
public static final String CONNID_REGEX = "^:[0-9]*\\.[0-9]*$";
public static final String OBJECT_REGEX = "^/([-_a-zA-Z0-9]+(/[-_a-zA-Z0-9]+)*)?$";
public static final Pattern DOLLAR_PATTERN = Pattern.compile("[$]");
public static final boolean FLOAT_SUPPORT = (null != System.getenv("DBUS_JAVA_FLOATS"));
public static final Pattern BUSNAME_REGEX = Pattern.compile("^[-_a-zA-Z][-_a-zA-Z0-9]*(\\.[-_a-zA-Z][-_a-zA-Z0-9]*)*$");
public static final Pattern CONNID_REGEX = Pattern.compile("^:[0-9]*\\.[0-9]*$");
public static final Pattern OBJECT_REGEX_PATTERN = Pattern.compile("^/([-_a-zA-Z0-9]+(/[-_a-zA-Z0-9]+)*)?$");
public static final Pattern DOLLAR_PATTERN = Pattern.compile("[$]");

public static final int MAX_ARRAY_LENGTH = 67108864;
public static final int MAX_NAME_LENGTH = 255;
Expand Down Expand Up @@ -303,7 +303,7 @@ public void exportObject(String objectpath, DBusInterface object) throws DBusExc
if (null == objectpath || "".equals(objectpath)) {
throw new DBusException("Must Specify an Object Path");
}
if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !(OBJECT_REGEX_PATTERN.matcher(objectpath).matches())) {
throw new DBusException("Invalid object path: " + objectpath);
}
synchronized (getExportedObjects()) {
Expand Down Expand Up @@ -334,7 +334,7 @@ public void addFallback(String _objectPrefix, DBusInterface _object) throws DBus
if (null == _objectPrefix || "".equals(_objectPrefix)) {
throw new DBusException("Must Specify an Object Path");
}
if (!_objectPrefix.matches(OBJECT_REGEX) || _objectPrefix.length() > MAX_NAME_LENGTH) {
if (_objectPrefix.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(_objectPrefix).matches()) {
throw new DBusException("Invalid object path: " + _objectPrefix);
}
ExportedObject eo = new ExportedObject(_object, weakreferences);
Expand Down Expand Up @@ -423,7 +423,7 @@ public <T extends DBusSignal> void removeSigHandler(Class<T> type, DBusInterface
throw new ClassCastException("Not A DBus Signal");
}
String objectpath = getImportedObjects().get(object).getObjectPath();
if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}
removeSigHandler(new DBusMatchRule(type, null, objectpath), handler);
Expand Down Expand Up @@ -478,7 +478,7 @@ public <T extends DBusSignal> void addSigHandler(Class<T> type, DBusInterface ob
throw new DBusException("Not an object exported or imported by this connection");
}
String objectpath = rObj.getObjectPath();
if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}
addSigHandler(new DBusMatchRule(type, null, objectpath), handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public DBusInterface getExportedObject(String _source, String _path) throws DBus
* If the busname is incorrectly formatted.
*/
public void releaseBusName(String _busname) throws DBusException {
if (!_busname.matches(BUSNAME_REGEX) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || !BUSNAME_REGEX.matcher(_busname).matches()) {
throw new DBusException("Invalid bus name");
}
try {
Expand All @@ -468,7 +468,7 @@ public void releaseBusName(String _busname) throws DBusException {
* formatted.
*/
public void requestBusName(String _busname) throws DBusException {
if (!_busname.matches(BUSNAME_REGEX) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || !BUSNAME_REGEX.matcher(_busname).matches()) {
throw new DBusException("Invalid bus name");
}

Expand Down Expand Up @@ -549,7 +549,7 @@ public DBusInterface getPeerRemoteObject(String _busname, String _objectpath) th
throw new DBusException("Invalid bus name: null");
}

if ((!_busname.matches(BUSNAME_REGEX) && !_busname.matches(CONNID_REGEX)) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || (!BUSNAME_REGEX.matcher(_busname).matches() && !CONNID_REGEX.matcher(_busname).matches())) {
throw new DBusException("Invalid bus name: " + _busname);
}

Expand Down Expand Up @@ -589,11 +589,11 @@ public DBusInterface getRemoteObject(String _busname, String _objectpath) throws
throw new DBusException("Invalid object path: null");
}

if ((!_busname.matches(BUSNAME_REGEX) && !_busname.matches(CONNID_REGEX)) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || (!BUSNAME_REGEX.matcher(_busname).matches() && !CONNID_REGEX.matcher(_busname).matches())) {
throw new DBusException("Invalid bus name: " + _busname);
}

if (!_objectpath.matches(OBJECT_REGEX) || _objectpath.length() > MAX_NAME_LENGTH) {
if (_objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(_objectpath).matches()) {
throw new DBusException("Invalid object path: " + _objectpath);
}

Expand Down Expand Up @@ -631,7 +631,7 @@ public <I extends DBusInterface> I getPeerRemoteObject(String _busname, String _
throw new DBusException("Invalid bus name: null");
}

if ((!_busname.matches(BUSNAME_REGEX) && !_busname.matches(CONNID_REGEX)) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || (!BUSNAME_REGEX.matcher(_busname).matches() && !CONNID_REGEX.matcher(_busname).matches())) {
throw new DBusException("Invalid bus name: " + _busname);
}

Expand Down Expand Up @@ -706,11 +706,11 @@ public <I extends DBusInterface> I getRemoteObject(String _busname, String _obje
throw new ClassCastException("Not A DBus Interface");
}

if ((!_busname.matches(BUSNAME_REGEX) && !_busname.matches(CONNID_REGEX)) || _busname.length() > MAX_NAME_LENGTH) {
if (_busname.length() > MAX_NAME_LENGTH || (!BUSNAME_REGEX.matcher(_busname).matches() && !CONNID_REGEX.matcher(_busname).matches())) {
throw new DBusException("Invalid bus name: " + _busname);
}

if (!_objectpath.matches(OBJECT_REGEX) || _objectpath.length() > MAX_NAME_LENGTH) {
if (!OBJECT_REGEX_PATTERN.matcher(_objectpath).matches() || _objectpath.length() > MAX_NAME_LENGTH) {
throw new DBusException("Invalid object path: " + _objectpath);
}

Expand Down Expand Up @@ -753,11 +753,11 @@ public <T extends DBusSignal> void removeSigHandler(Class<T> _type, String _sour
if (!DBusSignal.class.isAssignableFrom(_type)) {
throw new ClassCastException("Not A DBus Signal");
}
if (_source.matches(BUSNAME_REGEX)) {
if (BUSNAME_REGEX.matcher(_source).matches()) {
throw new DBusException(
"Cannot watch for signals based on well known bus name as source, only unique names.");
}
if (!_source.matches(CONNID_REGEX) || _source.length() > MAX_NAME_LENGTH) {
if (_source.length() > MAX_NAME_LENGTH || !CONNID_REGEX.matcher(_source).matches()) {
throw new DBusException("Invalid bus name: " + _source);
}
removeSigHandler(new DBusMatchRule(_type, _source, null), _handler);
Expand Down Expand Up @@ -786,15 +786,15 @@ public <T extends DBusSignal> void removeSigHandler(Class<T> _type, String _sour
if (!DBusSignal.class.isAssignableFrom(_type)) {
throw new ClassCastException("Not A DBus Signal");
}
if (_source.matches(BUSNAME_REGEX)) {
if (BUSNAME_REGEX.matcher(_source).matches()) {
throw new DBusException(
"Cannot watch for signals based on well known bus name as source, only unique names.");
}
if (!_source.matches(CONNID_REGEX) || _source.length() > MAX_NAME_LENGTH) {
if (_source.length() > MAX_NAME_LENGTH || !CONNID_REGEX.matcher(_source).matches()) {
throw new DBusException("Invalid bus name: " + _source);
}
String objectpath = getImportedObjects().get(_object).getObjectPath();
if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}
removeSigHandler(new DBusMatchRule(_type, _source, objectpath), _handler);
Expand Down Expand Up @@ -849,11 +849,11 @@ public <T extends DBusSignal> void addSigHandler(Class<T> _type, String _source,
if (!DBusSignal.class.isAssignableFrom(_type)) {
throw new ClassCastException("Not A DBus Signal");
}
if (_source.matches(BUSNAME_REGEX)) {
if (BUSNAME_REGEX.matcher(_source).matches()) {
throw new DBusException(
"Cannot watch for signals based on well known bus name as source, only unique names.");
}
if (!_source.matches(CONNID_REGEX) || _source.length() > MAX_NAME_LENGTH) {
if (_source.length() > MAX_NAME_LENGTH || !CONNID_REGEX.matcher(_source).matches()) {
throw new DBusException("Invalid bus name: " + _source);
}
addSigHandler(new DBusMatchRule(_type, _source, null), (DBusSigHandler<? extends DBusSignal>) _handler);
Expand Down Expand Up @@ -884,15 +884,15 @@ public <T extends DBusSignal> void addSigHandler(Class<T> _type, String _source,
if (!DBusSignal.class.isAssignableFrom(_type)) {
throw new ClassCastException("Not A DBus Signal");
}
if (_source.matches(BUSNAME_REGEX)) {
if (BUSNAME_REGEX.matcher(_source).matches()) {
throw new DBusException(
"Cannot watch for signals based on well known bus name as source, only unique names.");
}
if (!_source.matches(CONNID_REGEX) || _source.length() > MAX_NAME_LENGTH) {
if (_source.length() > MAX_NAME_LENGTH || !CONNID_REGEX.matcher(_source).matches()) {
throw new DBusException("Invalid bus name: " + _source);
}
String objectpath = getImportedObjects().get(_object).getObjectPath();
if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}
addSigHandler(new DBusMatchRule(_type, _source, objectpath), (DBusSigHandler<? extends DBusSignal>) _handler);
Expand Down Expand Up @@ -985,7 +985,7 @@ public void disconnect() {
// concurrent modification exception later (calling releaseBusName() will modify the busnames List)
synchronized (busnames) {
List<String> lBusNames = busnames.stream()
.filter(busName -> busName != null && !(!busName.matches(BUSNAME_REGEX) || busName.length() > MAX_NAME_LENGTH))
.filter(busName -> busName != null && !(busName.length() > MAX_NAME_LENGTH || !BUSNAME_REGEX.matcher(busName).matches()))
.collect(Collectors.toList());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public DBusInterface getRemoteObject(String objectpath) throws DBusException {
throw new DBusException("Invalid object path: null");
}

if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public <T extends DBusInterface> T getRemoteObject(String objectpath, Class<T> t
throw new ClassCastException("Not A DBus Interface");
}

if (!objectpath.matches(OBJECT_REGEX) || objectpath.length() > MAX_NAME_LENGTH) {
if (objectpath.length() > MAX_NAME_LENGTH || !OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.freedesktop.dbus.DBusMatchRule;
Expand All @@ -37,6 +38,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.freedesktop.dbus.connections.AbstractConnection.OBJECT_REGEX_PATTERN;

public class DBusSignal extends Message {
private static final Map<String, Class<? extends DBusSignal>> CLASS_CACHE =
new ConcurrentHashMap<>();
Expand Down Expand Up @@ -268,7 +271,7 @@ private void cacheConstructors(Class<? extends DBusSignal> _clazz) {
protected DBusSignal(String objectpath, Object... args) throws DBusException {
super(DBusConnection.getEndianness(), Message.MessageType.SIGNAL, (byte) 0);

if (!objectpath.matches(AbstractConnection.OBJECT_REGEX)) {
if (!OBJECT_REGEX_PATTERN.matcher(objectpath).matches()) {
throw new DBusException("Invalid object path: " + objectpath);
}

Expand Down

0 comments on commit da1a416

Please sign in to comment.