From 5a2819bd9dc50d742577329f4cea8fe43279958d Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 10 Nov 2023 19:53:17 -0500 Subject: [PATCH] MT#55283 use g_auto for string buffers Change-Id: I135f75355a128feb8f3152c170ca4f9022d917a8 --- daemon/call.c | 2 +- daemon/codec.c | 4 ++-- daemon/janus.c | 6 +++--- daemon/main.c | 40 ++++++++++++++++++++-------------------- daemon/media_player.c | 2 +- daemon/recording.c | 2 +- daemon/statistics.c | 16 ++++++++-------- lib/auxlib.c | 6 +----- lib/auxlib.h | 3 +-- lib/loglib.c | 2 +- perf-tester/main.c | 12 ++++++------ recording-daemon/main.c | 12 ++++++------ 12 files changed, 51 insertions(+), 56 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index fc3233aa3..59c3fc9cb 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -1084,7 +1084,7 @@ int __init_stream(struct packet_stream *ps) { struct call_media *media = ps->media; struct call *call = ps->call; int dtls_active = -1; - AUTO_CLEANUP_GBUF(paramsbuf); + g_autoptr(char) paramsbuf = NULL; struct dtls_connection *dtls_conn = NULL; if (MEDIA_ISSET(media, DTLS)) { diff --git a/daemon/codec.c b/daemon/codec.c index 82499c431..15b2430b9 100644 --- a/daemon/codec.c +++ b/daemon/codec.c @@ -4675,8 +4675,8 @@ void codec_tracker_update(struct codec_store *cs) { if (!is_codec_touched_rate(sct, clockrate)) continue; - AUTO_CLEANUP_GBUF(pt_s); - pt_s = g_strdup_printf(STR_FORMAT "/%u", STR_FMT(supp_codec), clockrate); + g_autoptr(char) pt_s + = g_strdup_printf(STR_FORMAT "/%u", STR_FMT(supp_codec), clockrate); str pt_str = STR_INIT(pt_s); struct rtp_payload_type *pt = codec_add_payload_type(&pt_str, cs->media, NULL, NULL); diff --git a/daemon/janus.c b/daemon/janus.c index 59146c5ff..0986a6707 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -97,7 +97,7 @@ static uint64_t jr_str_int(JsonReader *r) { static struct call_monologue *janus_get_monologue(uint64_t handle_id, struct call *call, struct call_monologue *(*fn)(struct call *, const str *)) { - AUTO_CLEANUP_GBUF(handle_buf); + g_autoptr(char) handle_buf = NULL; handle_buf = g_strdup_printf("%" PRIu64, handle_id); str handle_str = STR_INIT(handle_buf); @@ -1592,7 +1592,7 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi // fetch call - AUTO_CLEANUP_GBUF(call_id); + g_autoptr(char) call_id = NULL; AUTO_CLEANUP_NULL(struct call *call, call_unlock_release); { LOCK(&janus_lock); @@ -1645,7 +1645,7 @@ static const char *janus_trickle(JsonReader *reader, struct janus_session *sessi // set required signalling flags flags.fragment = 1; - AUTO_CLEANUP_GBUF(handle_buf); + g_autoptr(char) handle_buf = NULL; handle_buf = g_strdup_printf("%" PRIu64, handle_id); bencode_strdup_str(&ngbuf->buffer, &flags.from_tag, handle_buf); bencode_strdup_str(&ngbuf->buffer, &flags.call_id, call_id); diff --git a/daemon/main.c b/daemon/main.c index 5bc9786e2..7cc78f9e3 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -458,41 +458,41 @@ static void options(int *argc, char ***argv) { AUTO_CLEANUP_GVBUF(listenngs); AUTO_CLEANUP_GVBUF(listenngtcps); AUTO_CLEANUP_GVBUF(listencli); - AUTO_CLEANUP_GBUF(graphitep); - AUTO_CLEANUP_GBUF(graphite_prefix_s); - AUTO_CLEANUP_GBUF(redisps); - AUTO_CLEANUP_GBUF(redisps_write); - AUTO_CLEANUP_GBUF(log_facility_cdr_s); - AUTO_CLEANUP_GBUF(log_facility_rtcp_s); - AUTO_CLEANUP_GBUF(log_facility_dtmf_s); - AUTO_CLEANUP_GBUF(log_format); + g_autoptr(char) graphitep = NULL; + g_autoptr(char) graphite_prefix_s = NULL; + g_autoptr(char) redisps = NULL; + g_autoptr(char) redisps_write = NULL; + g_autoptr(char) log_facility_cdr_s = NULL; + g_autoptr(char) log_facility_rtcp_s = NULL; + g_autoptr(char) log_facility_dtmf_s = NULL; + g_autoptr(char) log_format = NULL; bool sip_source = false; - AUTO_CLEANUP_GBUF(homerp); - AUTO_CLEANUP_GBUF(homerproto); + g_autoptr(char) homerp = NULL; + g_autoptr(char) homerproto = NULL; char *endptr; bool codecs = false; double max_load = 0; double max_cpu = 0; - AUTO_CLEANUP_GBUF(dtmf_udp_ep); - AUTO_CLEANUP_GBUF(endpoint_learning); - AUTO_CLEANUP_GBUF(dtls_sig); + g_autoptr(char) dtmf_udp_ep = NULL; + g_autoptr(char) endpoint_learning = NULL; + g_autoptr(char) dtls_sig = NULL; double silence_detect = 0; AUTO_CLEANUP_GVBUF(cn_payload); AUTO_CLEANUP_GVBUF(dtx_cn_params); bool debug_srtp = false; - AUTO_CLEANUP_GBUF(amr_dtx); + g_autoptr(char) amr_dtx = NULL; #ifdef HAVE_MQTT - AUTO_CLEANUP_GBUF(mqtt_publish_scope); + g_autoptr(char) mqtt_publish_scope = NULL; #endif - AUTO_CLEANUP_GBUF(mos); - AUTO_CLEANUP_GBUF(dcc); - AUTO_CLEANUP_GBUF(use_audio_player); - AUTO_CLEANUP_GBUF(control_pmtu); + g_autoptr(char) mos = NULL; + g_autoptr(char) dcc = NULL; + g_autoptr(char) use_audio_player = NULL; + g_autoptr(char) control_pmtu = NULL; #ifndef WITHOUT_NFTABLES bool nftables_start = false; bool nftables_stop = false; bool nftables_status = false; - AUTO_CLEANUP_GBUF(nftables_family); + g_autoptr(char) nftables_family = NULL; #endif rwlock_lock_w(&rtpe_config.config_lock); diff --git a/daemon/media_player.c b/daemon/media_player.c index a58056834..456af5c65 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1132,7 +1132,7 @@ err: // call->master_lock held in W int media_player_play_db(struct media_player *mp, long long id, long long repeat, long long start_pos) { const char *err; - AUTO_CLEANUP_GBUF(query); + g_autoptr(char) query = NULL; err = "missing configuration"; if (!rtpe_config.mysql_host || !rtpe_config.mysql_query) diff --git a/daemon/recording.c b/daemon/recording.c index 3db55e390..d8f254381 100644 --- a/daemon/recording.c +++ b/daemon/recording.c @@ -857,7 +857,7 @@ static void finish_proc(struct call *call, bool discard) { } const char *unlink_fn = recording->proc.meta_filepath; - AUTO_CLEANUP_GBUF(discard_fn); + g_autoptr(char) discard_fn = NULL; if (discard) { discard_fn = g_strdup_printf("%s.DISCARD", recording->proc.meta_filepath); int ret = rename(recording->proc.meta_filepath, discard_fn); diff --git a/daemon/statistics.c b/daemon/statistics.c index 1e8446927..6a94f0660 100644 --- a/daemon/statistics.c +++ b/daemon/statistics.c @@ -484,10 +484,10 @@ GQueue *statistics_gather_metrics(struct interface_sampled_rate_stats *interface double min = (double) atomic64_get(&rtpe_sampled_graphite_min_max_sampled.min.ng_command_times[i]) / 1000000.0; double max = (double) atomic64_get(&rtpe_sampled_graphite_min_max_sampled.max.ng_command_times[i]) / 1000000.0; double avg = (double) atomic64_get(&rtpe_sampled_graphite_avg.avg.ng_command_times[i]) / 1000000.0; - AUTO_CLEANUP(char *min_label, free_gbuf) = g_strdup_printf("min%sdelay", ng_command_strings[i]); - AUTO_CLEANUP(char *max_label, free_gbuf) = g_strdup_printf("max%sdelay", ng_command_strings[i]); - AUTO_CLEANUP(char *avg_label, free_gbuf) = g_strdup_printf("avg%sdelay", ng_command_strings[i]); - AUTO_CLEANUP(char *long_label, free_gbuf) = g_strdup_printf("Min/Max/Avg %s processing delay", ng_command_strings[i]); + g_autoptr(char) min_label = g_strdup_printf("min%sdelay", ng_command_strings[i]); + g_autoptr(char) max_label = g_strdup_printf("max%sdelay", ng_command_strings[i]); + g_autoptr(char) avg_label = g_strdup_printf("avg%sdelay", ng_command_strings[i]); + g_autoptr(char) long_label = g_strdup_printf("Min/Max/Avg %s processing delay", ng_command_strings[i]); METRICl(long_label, "%.6f/%.6f/%.6f sec", min, max, avg); METRICsva(min_label, "%.6f", min); METRICsva(max_label, "%.6f", max); @@ -498,10 +498,10 @@ GQueue *statistics_gather_metrics(struct interface_sampled_rate_stats *interface uint64_t min = atomic64_get(&rtpe_rate_graphite_min_max_avg_sampled.min.ng_commands[i]); uint64_t max = atomic64_get(&rtpe_rate_graphite_min_max_avg_sampled.max.ng_commands[i]); uint64_t avg = atomic64_get(&rtpe_rate_graphite_min_max_avg_sampled.avg.ng_commands[i]); - AUTO_CLEANUP(char *min_label, free_gbuf) = g_strdup_printf("min%srequestrate", ng_command_strings[i]); - AUTO_CLEANUP(char *max_label, free_gbuf) = g_strdup_printf("max%srequestrate", ng_command_strings[i]); - AUTO_CLEANUP(char *avg_label, free_gbuf) = g_strdup_printf("avg%srequestrate", ng_command_strings[i]); - AUTO_CLEANUP(char *long_label, free_gbuf) = g_strdup_printf("Min/Max/Avg %s requests per second", ng_command_strings[i]); + g_autoptr(char) min_label = g_strdup_printf("min%srequestrate", ng_command_strings[i]); + g_autoptr(char) max_label = g_strdup_printf("max%srequestrate", ng_command_strings[i]); + g_autoptr(char) avg_label = g_strdup_printf("avg%srequestrate", ng_command_strings[i]); + g_autoptr(char) long_label = g_strdup_printf("Min/Max/Avg %s requests per second", ng_command_strings[i]); METRICl(long_label, "%" PRIu64 "/%" PRIu64 "/%" PRIu64 " per sec", min, max, avg); METRICsva(min_label, "%" PRIu64 "", min); METRICsva(max_label, "%" PRIu64 "", max); diff --git a/lib/auxlib.c b/lib/auxlib.c index d30633ddc..05fc6eab0 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -176,7 +176,7 @@ void config_load(int *argc, char ***argv, GOptionEntry *app_entries, const char { AUTO_CLEANUP_NULL(GOptionContext *c, free_goptc); AUTO_CLEANUP_NULL(GError *er, free_gerror); - AUTO_CLEANUP_GBUF(use_section); + g_autoptr(char) use_section = NULL; const char *use_config; int fatal = 0; AUTO_CLEANUP(char **saved_argv, free_gvbuf) = g_strdupv(*argv); @@ -465,10 +465,6 @@ int timeval_cmp_ptr(const void *a, const void *b) { return 0; } -void free_gbuf(char **p) { - g_free(*p); -} - void free_gvbuf(char ***p) { g_strfreev(*p); } diff --git a/lib/auxlib.h b/lib/auxlib.h index ce8767812..b53c12312 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -78,13 +78,12 @@ int num_cpu_cores(int); #define UINT64F "%" G_GUINT64_FORMAT -void free_gbuf(char **); void free_gvbuf(char ***); #define AUTO_CLEANUP(decl, func) decl __attribute__ ((__cleanup__(func))) #define AUTO_CLEANUP_INIT(decl, func, val) AUTO_CLEANUP(decl, func) = val #define AUTO_CLEANUP_NULL(decl, func) AUTO_CLEANUP_INIT(decl, func, 0) -#define AUTO_CLEANUP_GBUF(var) AUTO_CLEANUP_NULL(char *var, free_gbuf) +G_DEFINE_AUTOPTR_CLEANUP_FUNC(char, g_free) #define AUTO_CLEANUP_GVBUF(var) AUTO_CLEANUP_NULL(char **var, free_gvbuf) diff --git a/lib/loglib.c b/lib/loglib.c index b1faa9fdb..2fa77ddf5 100644 --- a/lib/loglib.c +++ b/lib/loglib.c @@ -142,7 +142,7 @@ static void log_both(int facility_priority, const char *format, ...) { void __vpilog(int prio, const char *prefix, const char *fmt, va_list ap) { - AUTO_CLEANUP_GBUF(msg); + g_autoptr(char) msg = NULL; char *piece; const char *infix = ""; int len, xprio; diff --git a/perf-tester/main.c b/perf-tester/main.c index 9c04990e7..661155de9 100644 --- a/perf-tester/main.c +++ b/perf-tester/main.c @@ -655,7 +655,7 @@ static char *start_dump_stream(struct stream *s, const char *suffix) { s->fmtctx = avformat_alloc_context(); if (!s->fmtctx) goto out; - AUTO_CLEANUP(char *fn, free_gbuf) + g_autoptr(char) fn = g_strdup_printf("stream-dump-%llu%s.mkv", (long long unsigned) time(NULL), suffix ?: ""); @@ -1007,7 +1007,7 @@ static bool thread_collect(pid_t pid, struct stats *outp, struct stats_sample *s if (!pid) return false; - AUTO_CLEANUP(char *fn, free_gbuf) + g_autoptr(char) fn = g_strdup_printf("/proc/%i/task/%i/stat", (int) pid, (int) pid); AUTO_CLEANUP(FILE *fp, fclose_p) = fopen(fn, "r"); if (!fp) @@ -1212,7 +1212,7 @@ static int pid_compare(const void *a, const void *b) { static int other_threads_collect(const bool do_output, int starty, int maxy, int maxx, struct stats *totals) { - AUTO_CLEANUP(char *dn, free_gbuf) = g_strdup_printf("/proc/%u/task", getpid()); + g_autoptr(char) dn = g_strdup_printf("/proc/%u/task", getpid()); AUTO_CLEANUP(DIR *dp, closedir_p) = opendir(dn); if (!dp) return starty; @@ -1421,7 +1421,7 @@ static char *fixture_path_file(const char *base_fn) { static void fixture_read_avio(GPtrArray *fixture, struct testparams *prm) { AVFormatContext *fctx = NULL; - AUTO_CLEANUP(char *fn, free_gbuf) = fixture_path_file(prm->file); + g_autoptr(char) fn = fixture_path_file(prm->file); int ret = avformat_open_input(&fctx, fn, NULL, NULL); if (ret < 0) die("Failed to open input fixture"); @@ -1450,7 +1450,7 @@ static void fixture_read_avio(GPtrArray *fixture, struct testparams *prm) { static void fixture_read_raw(GPtrArray *fixture, struct testparams *prm) { - AUTO_CLEANUP(char *fn, free_gbuf) = fixture_path_file(prm->file); + g_autoptr(char) fn = fixture_path_file(prm->file); FILE *fp = fopen(fn, "r"); if (!fp) die("Failed to open input fixture"); @@ -1663,7 +1663,7 @@ static void *cpu_freq_monitor(void *p) { if (strncmp(ent->d_name, "policy", 6) != 0) continue; // skip - AUTO_CLEANUP(char *fn, free_gbuf) + g_autoptr(char) fn = g_strdup_printf("/sys/devices/system/cpu/cpufreq/%s/scaling_cur_freq", ent->d_name); AUTO_CLEANUP(FILE *fp, fclose_p) = fopen(fn, "r"); diff --git a/recording-daemon/main.c b/recording-daemon/main.c index ad393f6f6..c3220fe74 100644 --- a/recording-daemon/main.c +++ b/recording-daemon/main.c @@ -183,12 +183,12 @@ static mode_t chmod_parse(const char *s) { static void options(int *argc, char ***argv) { - AUTO_CLEANUP_GBUF(os_str); - AUTO_CLEANUP_GBUF(chmod_mode); - AUTO_CLEANUP_GBUF(chmod_dir_mode); - AUTO_CLEANUP_GBUF(user_uid); - AUTO_CLEANUP_GBUF(group_gid); - AUTO_CLEANUP_GBUF(mix_method_str); + g_autoptr(char) os_str = NULL; + g_autoptr(char) chmod_mode = NULL; + g_autoptr(char) chmod_dir_mode = NULL; + g_autoptr(char) user_uid = NULL; + g_autoptr(char) group_gid = NULL; + g_autoptr(char) mix_method_str = NULL; GOptionEntry e[] = { { "table", 't', 0, G_OPTION_ARG_INT, &ktable, "Kernel table rtpengine uses", "INT" },