printf("\n");
- b = avahi_string_list_parse(data, size);
+ assert(avahi_string_list_parse(data, size, &b) == 0);
assert(avahi_string_list_equal(a, b));
assert(size == 1);
assert(size == n);
- a = avahi_string_list_parse(data, size);
+ assert(avahi_string_list_parse(data, size, &a) == 0);
assert(!a);
return 0;
return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
}
-AvahiStringList *avahi_string_list_parse(const void* data, size_t size) {
- AvahiStringList *r = NULL;
+int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
const uint8_t *c;
+ AvahiStringList *r;
assert(data);
+ assert(ret);
+
+ r = NULL;
c = data;
- for (;;) {
+ while (size > 0) {
size_t k;
- if (size < 1)
- break;
-
k = *(c++);
+ size--;
- if (k > 0) /* Ignore empty strings */
- r = avahi_string_list_add_arbitrary(r, c, k);
- c += k;
+ if (k > size)
+ goto fail; /* Overflow */
+
+ if (k > 0) { /* Ignore empty strings */
+ AvahiStringList *n;
- size -= 1 + k;
+ if (!(n = avahi_string_list_add_arbitrary(r, c, k)))
+ goto fail; /* OOM */
+
+ r = n;
+ }
+
+ c += k;
+ size -= k;
}
- return r;
+ *ret = r;
+
+ return 0;
+
+fail:
+ avahi_string_list_free(*ret);
+ return -1;
}
void avahi_string_list_free(AvahiStringList *l) {
size_t avahi_string_list_serialize(AvahiStringList *l, void * data, size_t size);
/** Inverse of avahi_string_list_serialize() */
-AvahiStringList *avahi_string_list_parse(const void *data, size_t size);
+int avahi_string_list_parse(const void *data, size_t size, AvahiStringList **ret);
/** Compare to string lists */
int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b);
oid_data *data;
sw_result result = SW_E_UNKNOWN;
service_data *sdata;
+ AvahiStringList *txt = NULL;
assert(self);
assert(name);
AVAHI_WARN_LINKAGE;
- if ((*oid = oid_alloc(self, OID_ENTRY_GROUP)) == (sw_discovery_oid) -1)
+ if (text_record && text_record_len > 0)
+ if (avahi_string_list_parse(text_record, text_record_len, &txt) < 0)
+ return SW_E_UNKNOWN;
+
+ if ((*oid = oid_alloc(self, OID_ENTRY_GROUP)) == (sw_discovery_oid) -1) {
+ avahi_string_list_free(txt);
return SW_E_UNKNOWN;
+ }
if (!(sdata = service_data_new(self))) {
+ avahi_string_list_free(txt);
oid_release(self, *oid);
return SW_E_MEM;
}
sdata->domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
sdata->host = host ? avahi_normalize_name_strdup(host) : NULL;
sdata->port = port;
- sdata->txt = text_record && text_record_len > 0 ? avahi_string_list_parse(text_record, text_record_len) : NULL;
+ sdata->txt = txt;
/* Some OOM checking would be cool here */
sw_octets text_record,
sw_uint32 text_record_len) {
+ AvahiStringList *txt;
assert(self);
AVAHI_WARN_LINKAGE;
return SW_E_UNKNOWN;
}
- (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len));
+ if (avahi_string_list_parse(text_record, text_record_len, &txt) < 0) {
+ avahi_free(*self);
+ *self = NULL;
+ return SW_E_UNKNOWN;
+ }
+
+ (*self)->index = (*self)->strlst = avahi_string_list_reverse(txt);
return SW_OKAY;
}
DNSServiceErrorType ret = kDNSServiceErr_Unknown;
int error;
DNSServiceRef sdref = NULL;
+ AvahiStringList *txt = NULL;
AVAHI_WARN_LINKAGE;
return kDNSServiceErr_Unsupported;
}
- if (!(sdref = sdref_new()))
+ if (txtRecord && txtLen > 0)
+ if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0)
+ return kDNSServiceErr_Invalid;
+
+ if (!(sdref = sdref_new())) {
+ avahi_string_list_free(txt);
return kDNSServiceErr_Unknown;
+ }
sdref->context = context;
sdref->service_register_callback = callback;
sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
sdref->service_port = ntohs(port);
- sdref->service_txt = txtRecord && txtLen > 0 ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
+ sdref->service_txt = txt;
/* Some OOM checking would be cool here */
case AVAHI_DNS_TYPE_TXT:
if (rdlength > 0) {
- r->data.txt.string_list = avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength);
+ if (avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength, &r->data.txt.string_list) < 0)
+ return -1;
if (avahi_dns_packet_skip(p, rdlength) < 0)
return -1;