Skip to content

Commit

Permalink
Android: Fix missing or corrupt service name
Browse files Browse the repository at this point in the history
The publish service name and type are passed to startServicePublish() as
char pointers.  startServicePublish() calls runOnAndroidThread which asks
the java code to run registerService().  If name and type are objects on
the stack, they could get freed  / deleted before the registerService() is
run in the java thread which would cause registerService() to use deleted
objects.  Fix --> make permanent objects for name and type.
  • Loading branch information
jbagg committed May 22, 2023
1 parent 61f5676 commit f89c736
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions androidnsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ QZeroConfPrivate::~QZeroConfPrivate()
void QZeroConfPrivate::startServicePublish(const char *name, const char *type, quint16 port)
{
QAndroidJniObject ref(nsdManager);
publishName = name;
publishType = type;
QtAndroid::runOnAndroidThread([=](){
QAndroidJniObject txtMap("java/util/HashMap");
foreach (const QByteArray &key, txtRecords.keys()) {
Expand All @@ -86,8 +88,8 @@ void QZeroConfPrivate::startServicePublish(const char *name, const char *type, q
}

ref.callMethod<void>("registerService", "(Ljava/lang/String;Ljava/lang/String;ILjava/util/Map;)V",
QAndroidJniObject::fromString(QString(name)).object<jstring>(),
QAndroidJniObject::fromString(QString(type)).object<jstring>(),
QAndroidJniObject::fromString(publishName).object<jstring>(),
QAndroidJniObject::fromString(publishType).object<jstring>(),
port,
txtMap.object());
});
Expand Down
2 changes: 2 additions & 0 deletions androidnsd_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class QZeroConfPrivate: QObject
bool browserExists = false;
bool publisherExists = false;
QMap<QByteArray, QByteArray> txtRecords;
QString publishName;
QString publishType;


private slots:
Expand Down

0 comments on commit f89c736

Please sign in to comment.