From 7bf79a1a47143d0b2d1eb87fe1b938f870edc5b2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 25 Aug 2005 17:54:19 +0000 Subject: [PATCH] * fix behaviour of empty TXT lists git-svn-id: file:///home/lennart/svn/public/avahi/trunk@445 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/strlst-test.c | 6 ++++ avahi-common/strlst.c | 62 ++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c index a4c758f..b725fa7 100644 --- a/avahi-common/strlst-test.c +++ b/avahi-common/strlst-test.c @@ -111,6 +111,12 @@ int main(int argc, char *argv[]) { avahi_string_list_free(a); avahi_string_list_free(b); + + size = avahi_string_list_serialize(NULL, data, sizeof(data)); + assert(size == 1); + + a = avahi_string_list_parse(data, size); + assert(!a); return 0; } diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c index d1e17be..db5ac59 100644 --- a/avahi-common/strlst.c +++ b/avahi-common/strlst.c @@ -80,7 +80,9 @@ AvahiStringList *avahi_string_list_parse(const void* data, size_t size) { break; k = *(c++); - r = avahi_string_list_add_arbitrary(r, c, k); + + if (k > 0) /* Ignore empty strings */ + r = avahi_string_list_add_arbitrary(r, c, k); c += k; size -= 1 + k; @@ -153,34 +155,48 @@ size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) size_t used = 0; if (data) { - uint8_t *c; - AvahiStringList *n; - assert(data); + if (l) { + uint8_t *c; + AvahiStringList *n; - l = avahi_string_list_reverse(l); - c = data; - - for (n = l; n; n = n->next) { - size_t k; - if (size < 1) - break; - - k = n->size; - if (k > 255) - k = 255; + l = avahi_string_list_reverse(l); + c = data; - if (k > size-1) - k = size-1; + for (n = l; n; n = n->next) { + size_t k; + if (size < 1) + break; + + k = n->size; + if (k > 255) + k = 255; + + if (k > size-1) + k = size-1; + + *(c++) = k; + memcpy(c, n->text, k); + c += k; + + used += 1+ k; + } + + l = avahi_string_list_reverse(l); - *(c++) = k; - memcpy(c, n->text, k); - c += k; + } else { + + /* Empty lists are treated specially. To comply with + * section 6.1 of the DNS-SD spec, we return a single + * empty string (i.e. a NUL byte)*/ + + if (size > 0) { + *(uint8_t*) data = 0; + used = 1; + } - used += 1+ k; } - - l = avahi_string_list_reverse(l); + } else { AvahiStringList *n; -- 2.39.5