windy1 / zeroconf-rs

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

Closed samkearney closed 3 years ago

samkearney commented 3 years ago

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:

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().

windy1 commented 3 years ago

Thanks again!