pulse-server: module-zeroconf-publish: do not allocate service name dynamically

The service name cannot exceed AVAHI_LABEL_MAX-1 characters in length,
so store it in the service struct instead of dynamically allocating it.
This commit is contained in:
Barnabás Pőcze 2021-11-06 18:15:10 +01:00
parent d240d7a5ef
commit fd111da507

View file

@ -40,6 +40,7 @@
#include <avahi-common/alternative.h> #include <avahi-common/alternative.h>
#include <avahi-common/error.h> #include <avahi-common/error.h>
#include <avahi-common/domain.h> #include <avahi-common/domain.h>
#include <avahi-common/malloc.h>
#define NAME "zeroconf-publish" #define NAME "zeroconf-publish"
@ -67,7 +68,6 @@ enum service_subtype {
struct service { struct service {
struct module_zeroconf_publish_data *userdata; struct module_zeroconf_publish_data *userdata;
AvahiEntryGroup *entry_group; AvahiEntryGroup *entry_group;
char *service_name;
const char *service_type; const char *service_type;
enum service_subtype subtype; enum service_subtype subtype;
@ -77,6 +77,8 @@ struct service {
struct sample_spec ss; struct sample_spec ss;
struct channel_map cm; struct channel_map cm;
struct pw_properties *props; struct pw_properties *props;
char service_name[AVAHI_LABEL_MAX];
}; };
struct module_zeroconf_publish_data { struct module_zeroconf_publish_data {
@ -112,19 +114,15 @@ static const struct pw_core_events core_events = {
.error = on_core_error, .error = on_core_error,
}; };
static char *get_service_name(struct pw_manager_object *o) static void get_service_name(struct pw_manager_object *o, char *buf, size_t length)
{ {
const char *hn, *un, *n; const char *hn, *un, *n;
char *service_name;
hn = pw_get_host_name(); hn = pw_get_host_name();
un = pw_get_user_name(); un = pw_get_user_name();
n = pw_properties_get(o->props, PW_KEY_NODE_DESCRIPTION); n = pw_properties_get(o->props, PW_KEY_NODE_DESCRIPTION);
service_name = calloc(1, AVAHI_LABEL_MAX - 1); snprintf(buf, length, "%s@%s: %s", un, hn, n);
snprintf(service_name, AVAHI_LABEL_MAX - 1, "%s@%s: %s", un, hn, n);
return service_name;
} }
static int service_free(void *d, struct pw_manager_object *o) static int service_free(void *d, struct pw_manager_object *o)
@ -145,11 +143,6 @@ static int service_free(void *d, struct pw_manager_object *o)
avahi_entry_group_free(s->entry_group); avahi_entry_group_free(s->entry_group);
} }
if (s->service_name) {
pw_log_debug("Removing service: %s", s->service_name);
free(s->service_name);
}
if (s->name) { if (s->name) {
pw_log_debug("Removing service for node: %s", s->name); pw_log_debug("Removing service for node: %s", s->name);
free(s->name); free(s->name);
@ -294,7 +287,7 @@ static struct service *create_service(struct module_zeroconf_publish_data *d, st
s->userdata = d; s->userdata = d;
s->entry_group = NULL; s->entry_group = NULL;
s->service_name = get_service_name(o); get_service_name(o, s->service_name, sizeof(s->service_name));
fill_service_data(d, s, o); fill_service_data(d, s, o);
@ -346,8 +339,8 @@ static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupStat
t = avahi_alternative_service_name(s->service_name); t = avahi_alternative_service_name(s->service_name);
pw_log_info("Name collision, renaming %s to %s.", s->service_name, t); pw_log_info("Name collision, renaming %s to %s.", s->service_name, t);
free(s->service_name); snprintf(s->service_name, sizeof(s->service_name), "%s", t);
s->service_name = t; avahi_free(t);
publish_service(s); publish_service(s);
break; break;