]> git.meshlink.io Git - catta/blobdiff - avahi-compat-libdns_sd/compat.c
* handle NULL sdrefs gracefully.
[catta] / avahi-compat-libdns_sd / compat.c
index 88a98a7903b757989fdf8cc5e2d31523baa50948..9d8ff1c00e281bffbf161698880380a82ee9f0d7 100644 (file)
@@ -469,25 +469,26 @@ static void sdref_unref(DNSServiceRef sdref) {
 }
 
 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
-    if (!sdref || sdref->n_ref <= 0)
-        return -1;
 
     AVAHI_WARN_LINKAGE;
     
+    if (!sdref || sdref->n_ref <= 0)
+        return -1;
+
     return sdref->main_fd;
 }
 
 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
     DNSServiceErrorType ret = kDNSServiceErr_Unknown;
 
-    assert(sdref);
-    assert(sdref->n_ref >= 1);
-    
     AVAHI_WARN_LINKAGE;
 
-    ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
-
+    if (!sdref || sdref->n_ref <= 0)
+        return kDNSServiceErr_BadParam;
+    
     sdref_ref(sdref);
+
+    ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
     
     /* Cleanup notification socket */
     if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
@@ -512,10 +513,10 @@ DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
     
 finish:
 
-    sdref_unref(sdref);
-
     ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
     
+    sdref_unref(sdref);
+
     return ret;
 }
 
@@ -610,10 +611,12 @@ DNSServiceErrorType DNSSD_API DNSServiceBrowse(
     struct type_info type_info;
     
     AVAHI_WARN_LINKAGE;
-    
-    assert(ret_sdref);
+
+    if (!ret_sdref)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+
     assert(regtype);
-    assert(callback);
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
         AVAHI_WARN_UNSUPPORTED;
@@ -707,7 +710,7 @@ static void service_resolver_callback(
 
             strcat(full_name, ".");
             
-            sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
+            sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, (unsigned char*) p, sdref->context);
 
             avahi_free(p);
             break;
@@ -736,7 +739,10 @@ DNSServiceErrorType DNSSD_API DNSServiceResolve(
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
+    if (!ret_sdref)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+
     assert(name);
     assert(regtype);
     assert(domain);
@@ -850,7 +856,10 @@ DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
+    if (!ret_sdref)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+
     assert(callback);
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly ||
@@ -1091,8 +1100,13 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister (
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
-    assert(regtype);
+    if (!ret_sdref)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+    
+    if (!regtype)
+           return kDNSServiceErr_BadParam;
+       
     assert(txtRecord || txtLen == 0);
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
@@ -1201,10 +1215,12 @@ DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord(
 
     int ret = kDNSServiceErr_Unknown;
     AvahiStringList *txt = NULL;
-    assert(sdref);
 
     AVAHI_WARN_LINKAGE;
 
+    if (!sdref || sdref->n_ref <= 0)
+        return kDNSServiceErr_BadParam;
+
     if (flags || rref) {
         AVAHI_WARN_UNSUPPORTED;
         return kDNSServiceErr_Unsupported;