zeroconf is a cross-platform library that wraps underlying ZeroConf/mDNS implementations such as Bonjour or Avahi, providing an easy and idiomatic way to both register and browse services.
MIT License
77
stars
25
forks
source link
Fix size constant passed to avahi_address_snprint() #11
Problem: avahi_address_snprint() takes the size in bytes of the buffer being printed into as its second argument. This library was using std::mem::size_of_val() on a CString type to pass this argument; however, that will return the size of the CString type, which is not necessarily the size of the underlying array that the CString was generated from.
On 64-bit platforms the size of CString happens to be 16, which fits the string representation of most IPv4 addresses, but is lower than AVAHI_ADDRESS_STR_MAX which is defined to 40. This also means:
The avahi_address_to_string() function will probably not work with most IPv6 addresses at all
It won't even work with IPv4 addresses on 32-bit ARM platforms where the size of CString is smaller
I believe the correct solution is to pass the constant that was used to allocate the string buffer instead. I also added 2 tests, the IPv6 one fails using the previous code on 64-bit Ubuntu 20.04, but passes with the modified implementation of avahi_address_to_string().
Problem:
avahi_address_snprint()
takes the size in bytes of the buffer being printed into as its second argument. This library was usingstd::mem::size_of_val()
on aCString
type to pass this argument; however, that will return the size of theCString
type, which is not necessarily the size of the underlying array that theCString
was generated from.On 64-bit platforms the size of
CString
happens to be 16, which fits the string representation of most IPv4 addresses, but is lower than AVAHI_ADDRESS_STR_MAX which is defined to 40. This also means:avahi_address_to_string()
function will probably not work with most IPv6 addresses at allCString
is smallerI believe the correct solution is to pass the constant that was used to allocate the string buffer instead. I also added 2 tests, the IPv6 one fails using the previous code on 64-bit Ubuntu 20.04, but passes with the modified implementation of
avahi_address_to_string()
.