diff --git a/spa/plugins/alsa/alsa-udev.c b/spa/plugins/alsa/alsa-udev.c index 896e713b3..636387e4e 100644 --- a/spa/plugins/alsa/alsa-udev.c +++ b/spa/plugins/alsa/alsa-udev.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -228,27 +229,24 @@ static void unescape(const char *src, char *dst) static int check_device_pcm_class(const char *devname) { - FILE *f; char path[PATH_MAX]; char buf[16]; size_t sz; /* Check device class */ - spa_scnprintf(path, sizeof(path), "/sys/class/sound/%s/pcm_class", - devname); - f = fopen(path, "re"); + spa_scnprintf(path, sizeof(path), "/sys/class/sound/%s/pcm_class", devname); + + spa_autoptr(FILE) f = fopen(path, "re"); if (f == NULL) return -errno; sz = fread(buf, 1, sizeof(buf) - 1, f); buf[sz] = '\0'; - fclose(f); return spa_strstartswith(buf, "modem") ? -ENXIO : 0; } static int get_num_pcm_devices(unsigned int card_id) { char prefix[32]; - DIR *snd = NULL; struct dirent *entry; int num_dev = 0; int res; @@ -257,7 +255,8 @@ static int get_num_pcm_devices(unsigned int card_id) spa_scnprintf(prefix, sizeof(prefix), "pcmC%uD", card_id); - if ((snd = opendir("/dev/snd")) == NULL) + spa_autoptr(DIR) snd = opendir("/dev/snd"); + if (snd == NULL) return -errno; while ((errno = 0, entry = readdir(snd)) != NULL) { @@ -271,20 +270,13 @@ static int get_num_pcm_devices(unsigned int card_id) ++num_dev; } } - if (errno != 0) - res = -errno; - else - res = num_dev; - closedir(snd); - return res; + return errno != 0 ? -errno : num_dev; } static int check_device_available(struct impl *this, struct device *device, int *num_pcm) { char path[PATH_MAX]; - DIR *card = NULL, *pcm = NULL; - FILE *f; char buf[16]; size_t sz; struct dirent *entry, *entry_pcm; @@ -314,7 +306,8 @@ static int check_device_available(struct impl *this, struct device *device, int spa_scnprintf(path, sizeof(path), "/proc/asound/card%u", (unsigned int)device->id); - if ((card = opendir(path)) == NULL) + spa_autoptr(DIR) card = opendir(path); + if (card == NULL) goto done; while ((errno = 0, entry = readdir(card)) != NULL) { @@ -330,7 +323,9 @@ static int check_device_available(struct impl *this, struct device *device, int /* Check busy status */ spa_scnprintf(path, sizeof(path), "/proc/asound/card%u/%s", (unsigned int)device->id, entry->d_name); - if ((pcm = opendir(path)) == NULL) + + spa_autoptr(DIR) pcm = opendir(path); + if (pcm == NULL) goto done; while ((errno = 0, entry_pcm = readdir(pcm)) != NULL) { @@ -341,12 +336,11 @@ static int check_device_available(struct impl *this, struct device *device, int spa_scnprintf(path, sizeof(path), "/proc/asound/card%u/%s/%s/status", (unsigned int)device->id, entry->d_name, entry_pcm->d_name); - f = fopen(path, "re"); + spa_autoptr(FILE) f = fopen(path, "re"); if (f == NULL) goto done; sz = fread(buf, 1, 6, f); buf[sz] = '\0'; - fclose(f); if (!spa_strstartswith(buf, "closed")) { spa_log_debug(this->log, "card %u pcm device %s busy", @@ -359,9 +353,6 @@ static int check_device_available(struct impl *this, struct device *device, int } if (errno != 0) goto done; - - closedir(pcm); - pcm = NULL; } if (errno != 0) goto done; @@ -371,10 +362,7 @@ done: spa_log_info(this->log, "card %u: failed to find busy status (%s)", (unsigned int)device->id, spa_strerror(-errno)); } - if (card) - closedir(card); - if (pcm) - closedir(pcm); + return res; } @@ -519,7 +507,7 @@ static int emit_object_info(struct impl *this, struct device *device) static bool check_access(struct impl *this, struct device *device) { char path[128], prefix[32]; - DIR *snd = NULL; + spa_autoptr(DIR) snd = NULL; struct dirent *entry; bool accessible = false; @@ -544,7 +532,6 @@ static bool check_access(struct impl *this, struct device *device) break; } } - closedir(snd); } if (accessible != device->accessible) diff --git a/src/modules/flatpak-utils.h b/src/modules/flatpak-utils.h index 36ae0eb3e..8a05b3772 100644 --- a/src/modules/flatpak-utils.h +++ b/src/modules/flatpak-utils.h @@ -22,6 +22,7 @@ #include #endif +#include #include #include @@ -62,8 +63,8 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices) { #if defined(__linux__) char root_path[2048]; - int root_fd, info_fd, res; struct stat stat_buf; + int res; if (app_id) *app_id = NULL; @@ -71,8 +72,9 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices) *devices = NULL; snprintf(root_path, sizeof(root_path), "/proc/%d/root", (int)pid); - root_fd = openat (AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY); - if (root_fd == -1) { + + spa_autoclose int root_fd = openat(AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY); + if (root_fd < 0) { res = -errno; if (res == -EACCES) { struct statfs buf; @@ -90,9 +92,9 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices) pw_log_info("failed to open \"%s\": %s", root_path, spa_strerror(res)); return res; } - info_fd = openat (root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY); - close (root_fd); - if (info_fd == -1) { + + spa_autoclose int info_fd = openat(root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY); + if (info_fd < 0) { if (errno == ENOENT) { pw_log_debug("no .flatpak-info, client on the host"); /* No file => on the host */ @@ -128,7 +130,7 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices) pw_log_error("PID %d .flatpak-info parsing failed: %s", (int)pid, spa_strerror(res)); } - close(info_fd); + return 1; #else return 0; diff --git a/src/modules/module-protocol-pulse/cmd.c b/src/modules/module-protocol-pulse/cmd.c index 3be178f64..5bdf0d613 100644 --- a/src/modules/module-protocol-pulse/cmd.c +++ b/src/modules/module-protocol-pulse/cmd.c @@ -4,6 +4,7 @@ #include +#include #include #include "module.h" @@ -68,15 +69,14 @@ static int parse_cmd(void *user_data, const char *location, { struct impl *impl = user_data; struct spa_json it[3]; - char key[512], *s; + char key[512]; int res = 0; - s = strndup(str, len); + spa_autofree char *s = strndup(str, len); spa_json_init(&it[0], s, len); if (spa_json_enter_array(&it[0], &it[1]) < 0) { pw_log_error("config file error: pulse.cmd is not an array"); - res = -EINVAL; - goto exit; + return -EINVAL; } while (spa_json_enter_object(&it[1], &it[2]) > 0) { @@ -107,8 +107,7 @@ static int parse_cmd(void *user_data, const char *location, if (res < 0) break; } -exit: - free(s); + return res; } diff --git a/src/modules/module-protocol-pulse/module.c b/src/modules/module-protocol-pulse/module.c index 8e5f6b044..f49262200 100644 --- a/src/modules/module-protocol-pulse/module.c +++ b/src/modules/module-protocol-pulse/module.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -117,7 +118,8 @@ int module_unload(struct module *module) /** utils */ void module_args_add_props(struct pw_properties *props, const char *str) { - char *s = strdup(str), *p = s, *e, f; + spa_autofree char *s = strdup(str); + char *p = s, *e, f; const char *k, *v; const struct str_map *map; @@ -160,7 +162,6 @@ void module_args_add_props(struct pw_properties *props, const char *str) } pw_properties_set(props, k, v); } - free(s); } int module_args_to_audioinfo_keys(struct impl *impl, struct pw_properties *props, diff --git a/src/modules/module-protocol-pulse/pulse-server.c b/src/modules/module-protocol-pulse/pulse-server.c index c1521996a..eaa3450ae 100644 --- a/src/modules/module-protocol-pulse/pulse-server.c +++ b/src/modules/module-protocol-pulse/pulse-server.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -2516,12 +2517,12 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag, uint32_t sink_index, volume; struct sample *sample; const char *sink_name, *name; - struct pw_properties *props = NULL; + spa_autoptr(pw_properties) props = NULL; struct pw_manager_object *o; int res; if ((props = pw_properties_new(NULL, NULL)) == NULL) - goto error_errno; + return -errno; if ((res = message_get(m, TAG_U32, &sink_index, @@ -2529,13 +2530,13 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag, TAG_U32, &volume, TAG_STRING, &name, TAG_INVALID)) < 0) - goto error_proto; + return -EPROTO; if (client->version >= 13) { if ((res = message_get(m, TAG_PROPLIST, props, TAG_INVALID)) < 0) - goto error_proto; + return -EPROTO; } pw_log_info("[%s] %s tag:%u sink_index:%u sink_name:%s name:%s", @@ -2545,35 +2546,19 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag, pw_properties_update(props, &client->props->dict); if (sink_index != SPA_ID_INVALID && sink_name != NULL) - goto error_inval; + return -EINVAL; o = find_device(client, sink_index, sink_name, PW_DIRECTION_OUTPUT, NULL); if (o == NULL) - goto error_noent; + return -ENOENT; sample = find_sample(impl, SPA_ID_INVALID, name); if (sample == NULL) - goto error_noent; + return -ENOENT; pw_properties_setf(props, PW_KEY_TARGET_OBJECT, "%"PRIu64, o->serial); - return pending_sample_new(client, sample, props, tag); - -error_errno: - res = -errno; - goto error; -error_proto: - res = -EPROTO; - goto error; -error_inval: - res = -EINVAL; - goto error; -error_noent: - res = -ENOENT; - goto error; -error: - pw_properties_free(props); - return res; + return pending_sample_new(client, sample, spa_steal_ptr(props), tag); } static int do_remove_sample(struct client *client, uint32_t command, uint32_t tag, struct message *m) @@ -3184,11 +3169,8 @@ static int do_set_stream_name(struct client *client, uint32_t command, uint32_t static int do_update_proplist(struct client *client, uint32_t command, uint32_t tag, struct message *m) { uint32_t channel, mode; - struct stream *stream; - struct pw_properties *props; - int res; - props = pw_properties_new(NULL, NULL); + spa_autoptr(pw_properties) props = pw_properties_new(NULL, NULL); if (props == NULL) return -errno; @@ -3196,7 +3178,7 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t if (message_get(m, TAG_U32, &channel, TAG_INVALID) < 0) - goto error_protocol; + return -EPROTO; } else { channel = SPA_ID_INVALID; } @@ -3208,12 +3190,12 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t TAG_U32, &mode, TAG_PROPLIST, props, TAG_INVALID) < 0) - goto error_protocol; + return -EPROTO; if (command != COMMAND_UPDATE_CLIENT_PROPLIST) { - stream = pw_map_lookup(&client->streams, channel); + struct stream *stream = pw_map_lookup(&client->streams, channel); if (stream == NULL || stream->type == STREAM_TYPE_UPLOAD) - goto error_noentity; + return -ENOENT; pw_stream_update_properties(stream->stream, &props->dict); } else { @@ -3223,29 +3205,17 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t pw_core_update_properties(client->core, &client->props->dict); } } - res = reply_simple_ack(client, tag); -exit: - pw_properties_free(props); - return res; -error_protocol: - res = -EPROTO; - goto exit; -error_noentity: - res = -ENOENT; - goto exit; + return reply_simple_ack(client, tag); } static int do_remove_proplist(struct client *client, uint32_t command, uint32_t tag, struct message *m) { uint32_t i, channel; - struct stream *stream; - struct pw_properties *props; struct spa_dict dict; struct spa_dict_item *items; - int res; - props = pw_properties_new(NULL, NULL); + spa_autoptr(pw_properties) props = pw_properties_new(NULL, NULL); if (props == NULL) return -errno; @@ -3253,7 +3223,7 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t if (message_get(m, TAG_U32, &channel, TAG_INVALID) < 0) - goto error_protocol; + return -EPROTO; } else { channel = SPA_ID_INVALID; } @@ -3267,7 +3237,7 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t if (message_get(m, TAG_STRING, &key, TAG_INVALID) < 0) - goto error_protocol; + return -EPROTO; if (key == NULL) break; pw_properties_set(props, key, key); @@ -3281,25 +3251,16 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t } if (command != COMMAND_UPDATE_CLIENT_PROPLIST) { - stream = pw_map_lookup(&client->streams, channel); + struct stream *stream = pw_map_lookup(&client->streams, channel); if (stream == NULL || stream->type == STREAM_TYPE_UPLOAD) - goto error_noentity; + return -ENOENT; pw_stream_update_properties(stream->stream, &dict); } else { pw_core_update_properties(client->core, &dict); } - res = reply_simple_ack(client, tag); -exit: - pw_properties_free(props); - return res; -error_protocol: - res = -EPROTO; - goto exit; -error_noentity: - res = -ENOENT; - goto exit; + return reply_simple_ack(client, tag); } @@ -3672,7 +3633,7 @@ static int fill_sink_info_proplist(struct message *m, const struct spa_dict *sin const struct pw_manager_object *card) { struct pw_device_info *card_info = card ? card->info : NULL; - struct pw_properties *props = NULL; + spa_autoptr(pw_properties) props = NULL; if (card_info && card_info->props) { props = pw_properties_new_dict(sink_props); @@ -3682,9 +3643,8 @@ static int fill_sink_info_proplist(struct message *m, const struct spa_dict *sin pw_properties_add(props, card_info->props); sink_props = &props->dict; } - message_put(m, TAG_PROPLIST, sink_props, TAG_INVALID); - pw_properties_free(props); + message_put(m, TAG_PROPLIST, sink_props, TAG_INVALID); return 0; } @@ -3882,7 +3842,7 @@ static int fill_source_info_proplist(struct message *m, const struct spa_dict *s const struct pw_manager_object *card, const bool is_monitor) { struct pw_device_info *card_info = card ? card->info : NULL; - struct pw_properties *props = NULL; + spa_autoptr(pw_properties) props = NULL; if ((card_info && card_info->props) || is_monitor) { props = pw_properties_new_dict(source_props); @@ -3897,9 +3857,8 @@ static int fill_source_info_proplist(struct message *m, const struct spa_dict *s source_props = &props->dict; } - message_put(m, TAG_PROPLIST, source_props, TAG_INVALID); - pw_properties_free(props); + message_put(m, TAG_PROPLIST, source_props, TAG_INVALID); return 0; } @@ -5140,12 +5099,9 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3 const char *object_path = NULL; const char *message = NULL; const char *params = NULL; - char *response = NULL; - char *path = NULL; struct message *reply; struct pw_manager_object *o; int len = 0; - int res; if (message_get(m, TAG_STRING, &object_path, @@ -5164,11 +5120,13 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3 len = strlen(object_path); if (len > 0 && object_path[len - 1] == '/') --len; - path = strndup(object_path, len); + + spa_autofree char *path = strndup(object_path, len); if (path == NULL) return -ENOMEM; - res = -ENOENT; + spa_autofree char *response = NULL; + int res = -ENOENT; spa_list_for_each(o, &manager->object_list, link) { if (o->message_object_path && spa_streq(o->message_object_path, path)) { @@ -5180,7 +5138,6 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3 } } - free(path); if (res < 0) return res; @@ -5188,7 +5145,7 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3 reply = reply_new(client, tag); message_put(reply, TAG_STRING, response, TAG_INVALID); - free(response); + return client_queue_message(client, reply); } diff --git a/src/modules/module-protocol-pulse/server.c b/src/modules/module-protocol-pulse/server.c index e01974eeb..53b1a7e69 100644 --- a/src/modules/module-protocol-pulse/server.c +++ b/src/modules/module-protocol-pulse/server.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "client.h" @@ -404,7 +405,7 @@ on_connect(void *data, int fd, uint32_t mask) client_access = server->client_access; if (server->addr.ss_family == AF_UNIX) { - char *app_id = NULL, *devices = NULL; + spa_autofree char *app_id = NULL, *devices = NULL; #ifdef SO_PRIORITY val = 6; @@ -443,8 +444,6 @@ on_connect(void *data, int fd, uint32_t mask) else pw_properties_set(client->props, PW_KEY_MEDIA_CATEGORY, NULL); } - free(devices); - free(app_id); } else if (server->addr.ss_family == AF_INET || server->addr.ss_family == AF_INET6) { diff --git a/src/modules/module-raop-sink.c b/src/modules/module-raop-sink.c index c70eb9cff..fc7072dea 100644 --- a/src/modules/module-raop-sink.c +++ b/src/modules/module-raop-sink.c @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -1211,7 +1212,7 @@ static int rtsp_do_announce(struct impl *impl) char key[512*2]; char iv[16*2]; int res, frames, rsa_len, ip_version; - char *sdp; + spa_autofree char *sdp = NULL; char local_ip[256]; host = pw_properties_get(impl->props, "raop.ip"); @@ -1289,10 +1290,8 @@ static int rtsp_do_announce(struct impl *impl) default: return -ENOTSUP; } - res = rtsp_send(impl, "ANNOUNCE", "application/sdp", sdp, rtsp_announce_reply); - free(sdp); - return res; + return rtsp_send(impl, "ANNOUNCE", "application/sdp", sdp, rtsp_announce_reply); } static int rtsp_auth_setup_reply(void *data, int status, const struct spa_dict *headers) @@ -1355,7 +1354,6 @@ static const char *find_attr(char **tokens, const char *key) static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers) { const char *str, *realm, *nonce; - char **tokens; int n_tokens; if ((str = spa_dict_lookup(headers, "WWW-Authenticate")) == NULL) @@ -1368,9 +1366,9 @@ static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers) pw_log_info("Auth: %s", str); - tokens = pw_split_strv(str, " ", INT_MAX, &n_tokens); + spa_auto(pw_strv) tokens = pw_split_strv(str, " ", INT_MAX, &n_tokens); if (tokens == NULL || tokens[0] == NULL) - goto error; + return -EINVAL; impl->auth_method = strdup(tokens[0]); @@ -1378,20 +1376,13 @@ static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers) realm = find_attr(tokens, "realm"); nonce = find_attr(tokens, "nonce"); if (realm == NULL || nonce == NULL) - goto error; + return -EINVAL; impl->realm = strdup(realm); impl->nonce = strdup(nonce); } - pw_free_strv(tokens); - - rtsp_send(impl, "OPTIONS", NULL, NULL, rtsp_auth_reply); - return 0; - -error: - pw_free_strv(tokens); - return -EINVAL; + return rtsp_send(impl, "OPTIONS", NULL, NULL, rtsp_auth_reply); } static int rtsp_options_reply(void *data, int status, const struct spa_dict *headers) diff --git a/src/modules/spa/module-device.c b/src/modules/spa/module-device.c index d125b483a..d23ace783 100644 --- a/src/modules/spa/module-device.c +++ b/src/modules/spa/module-device.c @@ -8,6 +8,7 @@ #include #include +#include #include #include "spa-device.h" @@ -51,12 +52,11 @@ SPA_EXPORT int pipewire__module_init(struct pw_impl_module *module, const char *args) { struct pw_properties *props = NULL; - char **argv = NULL; + spa_auto(pw_strv) argv = NULL; int n_tokens; struct pw_context *context = pw_impl_module_get_context(module); struct pw_impl_device *device; struct device_data *data; - int res; PW_LOG_TOPIC_INIT(mod_topic); @@ -69,10 +69,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) if (n_tokens == 2) { props = pw_properties_new_string(argv[1]); - if (props == NULL) { - res = -errno; - goto error_exit_cleanup; - } + if (props == NULL) + return -errno; } device = pw_spa_device_load(context, @@ -80,12 +78,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) 0, props, sizeof(struct device_data)); - if (device == NULL) { - res = -errno; - goto error_exit_cleanup; - } - - pw_free_strv(argv); + if (device == NULL) + return -errno; data = pw_spa_device_get_user_data(device); data->this = device; @@ -99,10 +93,6 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) return 0; error_arguments: - res = -EINVAL; pw_log_error("usage: module-spa-device " MODULE_USAGE); - goto error_exit_cleanup; -error_exit_cleanup: - pw_free_strv(argv); - return res; + return -EINVAL; } diff --git a/src/modules/spa/module-node.c b/src/modules/spa/module-node.c index 9dbc8f497..fac64dab1 100644 --- a/src/modules/spa/module-node.c +++ b/src/modules/spa/module-node.c @@ -10,6 +10,7 @@ #include #include +#include #include #include "spa-node.h" @@ -52,8 +53,8 @@ SPA_EXPORT int pipewire__module_init(struct pw_impl_module *module, const char *args) { struct pw_properties *props = NULL; - char **argv = NULL; - int n_tokens, res; + spa_auto(pw_strv) argv = NULL; + int n_tokens; struct pw_context *context = pw_impl_module_get_context(module); struct pw_impl_node *node; struct node_data *data; @@ -69,10 +70,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) if (n_tokens == 2) { props = pw_properties_new_string(argv[1]); - if (props == NULL) { - res = -errno; - goto error_exit_cleanup; - } + if (props == NULL) + return -errno; } node = pw_spa_node_load(context, @@ -81,12 +80,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) props, sizeof(struct node_data)); - if (node == NULL) { - res = -errno; - goto error_exit_cleanup; - } - - pw_free_strv(argv); + if (node == NULL) + return -errno; data = pw_spa_node_get_user_data(node); data->this = node; @@ -101,10 +96,6 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args) return 0; error_arguments: - res = -EINVAL; pw_log_error("usage: module-spa-node " MODULE_USAGE); - goto error_exit_cleanup; -error_exit_cleanup: - pw_free_strv(argv); - return res; + return -EINVAL; } diff --git a/src/pipewire/conf.c b/src/pipewire/conf.c index 987c3d7a6..b8dbc0ddc 100644 --- a/src/pipewire/conf.c +++ b/src/pipewire/conf.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -345,7 +346,8 @@ int pw_conf_save_state(const char *prefix, const char *name, const struct pw_pro { char path[PATH_MAX]; char *tmp_name; - int res, sfd, fd, count = 0; + spa_autoclose int sfd = -1; + int res, fd, count = 0; FILE *f; if ((sfd = open_write_dir(path, sizeof(path), prefix)) < 0) @@ -354,9 +356,9 @@ int pw_conf_save_state(const char *prefix, const char *name, const struct pw_pro tmp_name = alloca(strlen(name)+5); sprintf(tmp_name, "%s.tmp", name); if ((fd = openat(sfd, tmp_name, O_CLOEXEC | O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) { - pw_log_error("can't open file '%s': %m", tmp_name); res = -errno; - goto error; + pw_log_error("can't open file '%s': %m", tmp_name); + return res; } f = fdopen(fd, "w"); @@ -366,46 +368,43 @@ int pw_conf_save_state(const char *prefix, const char *name, const struct pw_pro fclose(f); if (renameat(sfd, tmp_name, sfd, name) < 0) { - pw_log_error("can't rename temp file '%s': %m", tmp_name); res = -errno; - goto error; + pw_log_error("can't rename temp file '%s': %m", tmp_name); + return res; } - res = 0; + pw_log_info("%p: saved state '%s%s'", conf, path, name); -error: - close(sfd); - return res; + + return 0; } static int conf_load(const char *path, struct pw_properties *conf) { char *data; struct stat sbuf; - int fd, count; + int count; - if ((fd = open(path, O_CLOEXEC | O_RDONLY)) < 0) + spa_autoclose int fd = open(path, O_CLOEXEC | O_RDONLY); + if (fd < 0) goto error; if (fstat(fd, &sbuf) < 0) - goto error_close; + goto error; if (sbuf.st_size > 0) { if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) - goto error_close; + goto error; count = pw_properties_update_string(conf, data, sbuf.st_size); munmap(data, sbuf.st_size); } else { count = 0; } - close(fd); pw_log_info("%p: loaded config '%s' with %d items", conf, path, count); return 0; -error_close: - close(fd); error: pw_log_warn("%p: error loading config '%s': %m", conf, path); return -errno; @@ -455,7 +454,7 @@ int pw_conf_load_conf(const char *prefix, const char *name, struct pw_properties char path[PATH_MAX]; char fname[PATH_MAX + 256]; int i, res, level = 0; - struct pw_properties *override = NULL; + spa_autoptr(pw_properties) override = NULL; const char *dname; if (name == NULL) { @@ -510,7 +509,7 @@ int pw_conf_load_conf(const char *prefix, const char *name, struct pw_properties } free(entries); } - pw_properties_free(override); + return 0; } @@ -660,15 +659,14 @@ static int parse_modules(void *user_data, const char *location, struct data *d = user_data; struct pw_context *context = d->context; struct spa_json it[4]; - char key[512], *s; + char key[512]; int res = 0; - s = strndup(str, len); + spa_autofree char *s = strndup(str, len); spa_json_init(&it[0], s, len); if (spa_json_enter_array(&it[0], &it[1]) < 0) { pw_log_error("config file error: context.modules is not an array"); - res = -EINVAL; - goto exit; + return -EINVAL; } while (spa_json_enter_object(&it[1], &it[2]) > 0) { @@ -714,8 +712,7 @@ static int parse_modules(void *user_data, const char *location, d->count++; } -exit: - free(s); + return res; } @@ -761,15 +758,14 @@ static int parse_objects(void *user_data, const char *location, struct data *d = user_data; struct pw_context *context = d->context; struct spa_json it[4]; - char key[512], *s; + char key[512]; int res = 0; - s = strndup(str, len); + spa_autofree char *s = strndup(str, len); spa_json_init(&it[0], s, len); if (spa_json_enter_array(&it[0], &it[1]) < 0) { pw_log_error("config file error: context.objects is not an array"); - res = -EINVAL; - goto exit; + return -EINVAL; } while (spa_json_enter_object(&it[1], &it[2]) > 0) { @@ -815,8 +811,7 @@ static int parse_objects(void *user_data, const char *location, break; d->count++; } -exit: - free(s); + return res; } @@ -880,15 +875,14 @@ static int parse_exec(void *user_data, const char *location, struct data *d = user_data; struct pw_context *context = d->context; struct spa_json it[4]; - char key[512], *s; + char key[512]; int res = 0; - s = strndup(str, len); + spa_autofree char *s = strndup(str, len); spa_json_init(&it[0], s, len); if (spa_json_enter_array(&it[0], &it[1]) < 0) { pw_log_error("config file error: context.exec is not an array"); - res = -EINVAL; - goto exit; + return -EINVAL; } while (spa_json_enter_object(&it[1], &it[2]) > 0) { @@ -926,8 +920,7 @@ static int parse_exec(void *user_data, const char *location, d->count++; } -exit: - free(s); + return res; } diff --git a/src/pipewire/filter.c b/src/pipewire/filter.c index 8efdbd0a0..271f2bafa 100644 --- a/src/pipewire/filter.c +++ b/src/pipewire/filter.c @@ -18,6 +18,7 @@ #include #include +#include #include "pipewire/pipewire.h" #include "pipewire/filter.h" #include "pipewire/private.h" @@ -1898,8 +1899,8 @@ int pw_filter_set_error(struct pw_filter *filter, ensure_loop(impl->main_loop, return -EIO); if (res < 0) { + spa_autofree char *value = NULL; va_list args; - char *value; int r; va_start(args, error); @@ -1911,8 +1912,6 @@ int pw_filter_set_error(struct pw_filter *filter, if (filter->proxy) pw_proxy_error(filter->proxy, res, value); filter_set_state(filter, PW_FILTER_STATE_ERROR, res, value); - - free(value); } return res; } diff --git a/src/pipewire/impl-metadata.c b/src/pipewire/impl-metadata.c index 53811e664..c14d49aa2 100644 --- a/src/pipewire/impl-metadata.c +++ b/src/pipewire/impl-metadata.c @@ -7,6 +7,7 @@ #include #include +#include #include "pipewire/impl.h" #include "pipewire/private.h" @@ -576,8 +577,8 @@ int pw_impl_metadata_set_propertyf(struct pw_impl_metadata *metadata, uint32_t subject, const char *key, const char *type, const char *fmt, ...) { + spa_autofree char *value = NULL; va_list args; - char *value; int res; va_start(args, fmt); @@ -586,8 +587,5 @@ int pw_impl_metadata_set_propertyf(struct pw_impl_metadata *metadata, if (res < 0) return -errno; - res = pw_impl_metadata_set_property(metadata, subject, key, type, value); - free(value); - - return res; + return pw_impl_metadata_set_property(metadata, subject, key, type, value); } diff --git a/src/pipewire/impl-module.c b/src/pipewire/impl-module.c index 15488132a..982cf520c 100644 --- a/src/pipewire/impl-module.c +++ b/src/pipewire/impl-module.c @@ -15,6 +15,7 @@ #include +#include #include "pipewire/impl.h" #include "pipewire/private.h" @@ -38,7 +39,6 @@ static char *find_module(const char *path, const char *name, int level) char *filename; struct dirent *entry; struct stat s; - DIR *dir; int res; filename = spa_aprintf("%s/%s.so", path, name); @@ -57,7 +57,7 @@ static char *find_module(const char *path, const char *name, int level) if (level <= 0) return NULL; - dir = opendir(path); + spa_autoptr(DIR) dir = opendir(path); if (dir == NULL) { res = -errno; pw_log_warn("could not open %s: %m", path); @@ -66,28 +66,22 @@ static char *find_module(const char *path, const char *name, int level) } while ((entry = readdir(dir))) { - char *newpath; - if (spa_streq(entry->d_name, ".") || spa_streq(entry->d_name, "..")) continue; - newpath = spa_aprintf("%s/%s", path, entry->d_name); + spa_autofree char *newpath = spa_aprintf("%s/%s", path, entry->d_name); if (newpath == NULL) break; if (entry->d_type == DT_DIR || - (entry->d_type == DT_UNKNOWN && stat(newpath, &s) == 0 && S_ISDIR(s.st_mode))) + (entry->d_type == DT_UNKNOWN && stat(newpath, &s) == 0 && S_ISDIR(s.st_mode))) { filename = find_module(newpath, name, level - 1); - - free(newpath); - - if (filename != NULL) - break; + if (filename) + return filename; + } } - closedir(dir); - - return filename; + return NULL; } static int diff --git a/src/pipewire/pipewire.c b/src/pipewire/pipewire.c index caed53687..73caa3b24 100644 --- a/src/pipewire/pipewire.c +++ b/src/pipewire/pipewire.c @@ -25,6 +25,7 @@ #include #include +#include #include "pipewire.h" #include "private.h" #include "i18n.h" @@ -498,7 +499,6 @@ static char * parse_pw_debug_env(void) { const char *str; - char **tokens; int n_tokens; char json[1024] = {0}; char *pos = json; @@ -516,7 +516,7 @@ parse_pw_debug_env(void) */ pos += spa_scnprintf(pos, end - pos, "[ { conn.* = %d },", SPA_LOG_LEVEL_NONE); - tokens = pw_split_strv(str, ",", INT_MAX, &n_tokens); + spa_auto(pw_strv) tokens = pw_split_strv(str, ",", INT_MAX, &n_tokens); if (n_tokens > 0) { int i; for (i = 0; i < n_tokens; i++) { @@ -536,7 +536,7 @@ parse_pw_debug_env(void) } } } - pw_free_strv(tokens); + pos += spa_scnprintf(pos, end - pos, "]"); return strdup(json); } diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index 2b2a45c04..668d1b15f 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -21,6 +21,7 @@ #define PW_ENABLE_DEPRECATED +#include #include "pipewire/pipewire.h" #include "pipewire/stream.h" #include "pipewire/private.h" @@ -2165,8 +2166,8 @@ int pw_stream_set_error(struct pw_stream *stream, ensure_loop(impl->main_loop, return -EIO); if (res < 0) { + spa_autofree char *value = NULL; va_list args; - char *value; int r; va_start(args, error); @@ -2178,8 +2179,6 @@ int pw_stream_set_error(struct pw_stream *stream, if (stream->proxy) pw_proxy_error(stream->proxy, res, value); stream_set_state(stream, PW_STREAM_STATE_ERROR, res, value); - - free(value); } return res; } diff --git a/src/tools/pw-cat.c b/src/tools/pw-cat.c index 04e57baf3..29fe4124d 100644 --- a/src/tools/pw-cat.c +++ b/src/tools/pw-cat.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -603,7 +604,6 @@ static unsigned int find_channel(const char *name) static int parse_channelmap(const char *channel_map, struct channelmap *map) { int i, nch; - char **ch; SPA_FOR_EACH_ELEMENT_VAR(maps, m) { if (spa_streq(m->name, channel_map)) { @@ -614,7 +614,7 @@ static int parse_channelmap(const char *channel_map, struct channelmap *map) } } - ch = pw_split_strv(channel_map, ",", SPA_AUDIO_MAX_CHANNELS, &nch); + spa_auto(pw_strv) ch = pw_split_strv(channel_map, ",", SPA_AUDIO_MAX_CHANNELS, &nch); if (ch == NULL) return -1; @@ -623,7 +623,7 @@ static int parse_channelmap(const char *channel_map, struct channelmap *map) int c = find_channel(ch[i]); map->channels[i] = c; } - pw_free_strv(ch); + return 0; }