tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit 964d816726b8c765277b043894d534e9b1e0d60c
parent a9d37006eae7227d498c7c658360ee1e43b3e054
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 11 Jan 2023 23:44:04 +0100

Check aprintf allocation internally and add astrdup for less OOM_CHECK

Diffstat:
M.gitignore | 1+
Msrc/cmd.c | 11++---------
Msrc/data.c | 40+++++++++-------------------------------
Msrc/data.h | 1+
Msrc/history.c | 16++++++++--------
Msrc/log.c | 3++-
Msrc/mpris.c | 2+-
Msrc/player_mpd.c | 5++---
Msrc/ref.c | 4+++-
Msrc/strbuf.c | 8+++++---
Msrc/tui.c | 15++++-----------
Msrc/util.c | 22++++++++++++++++------
Msrc/util.h | 2+-
13 files changed, 55 insertions(+), 75 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -8,3 +8,4 @@ log* gmon.out* profile.txt compile_commands.json +perf* diff --git a/src/cmd.c b/src/cmd.c @@ -64,8 +64,6 @@ cmd_move(const char *name) CMD_ERROR("Same tag"); newpath = aprintf("%s/%s", tag->fpath, track->name); - OOM_CHECK(newpath); - if (path_exists(newpath)) { free(newpath); CMD_ERROR("File already exists"); @@ -112,8 +110,6 @@ cmd_copy(const char *name) CMD_ERROR("Same tag"); newpath = aprintf("%s/%s", tag->fpath, track->name); - OOM_CHECK(newpath); - if (path_exists(newpath)) { free(newpath); CMD_ERROR("File already exists"); @@ -180,8 +176,7 @@ cmd_reindex(const char *name) /* save old playing track */ if (player.track) { playing_tag = player.track->tag; - playing_name = strdup(player.track->name); - OOM_CHECK(playing_name); + playing_name = astrdup(player.track->name); } /* update each tag specified */ @@ -225,7 +220,6 @@ cmd_add_tag(const char *name) } fpath = aprintf("%s/%s", datadir, name); - OOM_CHECK(fpath); if (!make_dir(fpath)) { free(fpath); @@ -324,8 +318,7 @@ cmd_run(const char *query, bool *found) args = sep ? sep + 1 : ""; free(last_args); - last_args = strdup(args); - OOM_CHECK(last_args); + last_args = astrdup(args); *found = true; return commands[i].func(args); diff --git a/src/data.c b/src/data.c @@ -9,7 +9,7 @@ #include <dirent.h> #include <sys/stat.h> #include <unistd.h> - +#include <err.h> #include <stdbool.h> #include <string.h> @@ -44,13 +44,10 @@ tag_alloc(const char *path, const char *fname) struct tag *tag; tag = malloc(sizeof(struct tag)); - ASSERT(tag != NULL); + if (!tag) err(1, "malloc"); tag->fpath = aprintf("%s/%s", path, fname); - ASSERT(tag->fpath != NULL); - - tag->name = strdup(fname); - ASSERT(tag->name != NULL); + tag->name = astrdup(fname); tag->link = LINK_EMPTY; tag->link_sel = LINK_EMPTY; @@ -75,13 +72,10 @@ track_alloc(const char *dir, const char *fname) struct track *track; track = malloc(sizeof(struct track)); - ASSERT(track != NULL); + if (!track) err(1, "malloc"); track->fpath = aprintf("%s/%s", dir, fname); - ASSERT(track->fpath != NULL); - - track->name = strdup(fname); - ASSERT(track->name != NULL); + track->name = astrdup(fname); track->tag = NULL; @@ -121,13 +115,12 @@ tracks_load(struct tag *tag) FILE *file; index_path = aprintf("%s/index", tag->fpath); - OOM_CHECK(index_path); file = fopen(index_path, "r"); if (file == NULL) { index_update(tag); /* create index */ file = fopen(index_path, "r"); - ASSERT(file != NULL); + if (!file) err(1, "fopen %s", tag->name); } while (fgets(linebuf, sizeof(linebuf), file)) { @@ -152,7 +145,6 @@ tracks_save(struct tag *tag) /* write playlist back to index file */ index_path = aprintf("%s/index", tag->fpath); - OOM_CHECK(index_path); file = fopen(index_path, "w+"); if (!file) { @@ -295,7 +287,6 @@ index_update(struct tag *tag) if (!dir) ERROR("Failed to access dir: %s\n", tag->fpath); index_path = aprintf("%s/index", tag->fpath); - OOM_CHECK(index_path); file = fopen(index_path, "w+"); if (!file) ERROR("Failed to create index file in dir %s\n", tag->name); @@ -405,7 +396,6 @@ tag_add(const char *fname) /* add to tags list */ tag = tag_alloc(datadir, fname); - OOM_CHECK(tag); list_push_back(&tags, &tag->link); return tag; @@ -463,7 +453,6 @@ tag_rename(struct tag *tag, const char *name) char *newpath; newpath = aprintf("%s/%s", datadir, name); - OOM_CHECK(newpath); if (!move_dir(tag->fpath, newpath)) { free(newpath); @@ -474,14 +463,12 @@ tag_rename(struct tag *tag, const char *name) tag->fpath = newpath; free(tag->name); - tag->name = strdup(name); - OOM_CHECK(tag->name); + tag->name = astrdup(name); for (LIST_ITER(&tag->tracks, link)) { track = UPCAST(link, struct track, link_tt); free(track->fpath); track->fpath = aprintf("%s/%s", newpath, track->name); - OOM_CHECK(track->fpath); } return true; @@ -493,12 +480,10 @@ track_add(struct tag *tag, const char *fname) struct track *track; track = track_alloc(tag->fpath, fname); - OOM_CHECK(track); - track->tag = tag; /* insert track into sorted tracks list */ - list_insert_sorted(&tracks, &track->link, track_name_compare); + list_push_back(&tracks, &track->link); /* add to tag's tracks list */ list_push_back(&tag->tracks, &track->link_tt); @@ -546,7 +531,6 @@ track_rename(struct track *track, const char *name) char *newpath; newpath = aprintf("%s/%s", track->tag->fpath, name); - OOM_CHECK(newpath); if (path_exists(newpath)) { free(newpath); @@ -562,8 +546,7 @@ track_rename(struct track *track, const char *name) track->fpath = newpath; free(track->name); - track->name = strdup(name); - OOM_CHECK(track->name); + track->name = astrdup(name); return true; } @@ -577,8 +560,6 @@ acquire_lock(const char *datadir) int pid; lockpath = aprintf("%s/.lock", datadir); - OOM_CHECK(lockpath); - if (path_exists(lockpath)) { file = fopen(lockpath, "r"); if (file == NULL) { @@ -589,7 +570,6 @@ acquire_lock(const char *datadir) fgets(linebuf, sizeof(linebuf), file); pid = strtol(linebuf, NULL, 10); procpath = aprintf("/proc/%i", pid); - OOM_CHECK(procpath); if (path_exists(procpath)) { free(procpath); free(lockpath); @@ -618,7 +598,6 @@ release_lock(const char *datadir) bool status; lockpath = aprintf("%s/.lock", datadir); - OOM_CHECK(lockpath); status = rm_file(lockpath); @@ -656,7 +635,6 @@ data_load(void) continue; path = aprintf("%s/%s", datadir, ent->d_name); - OOM_CHECK(path); if (!stat(path, &st) && S_ISDIR(st.st_mode)) { tag = tag_add(ent->d_name); diff --git a/src/data.h b/src/data.h @@ -7,6 +7,7 @@ struct tag { char *name, *fpath; struct list tracks; + bool dirty; struct link link; /* tags list */ struct link link_sel; /* selected tags list */ diff --git a/src/history.c b/src/history.c @@ -3,13 +3,14 @@ #include "history.h" #include "util.h" +#include <err.h> #include <string.h> -static struct inputln * -history_list_prev(struct inputln *cur, const char *search); +static struct inputln *history_list_prev( + struct inputln *cur, const char *search); -static struct inputln * -history_list_next(struct inputln *cur, const char *search); +static struct inputln *history_list_next( + struct inputln *cur, const char *search); struct inputln * history_list_prev(struct inputln *cur, const char *search) @@ -139,7 +140,7 @@ inputln_alloc(void) struct inputln *ln; ln = malloc(sizeof(struct inputln)); - OOM_CHECK(ln); + if (!ln) err(1, "malloc"); inputln_init(ln); return ln; @@ -159,7 +160,7 @@ inputln_resize(struct inputln *ln, size_t size) ln->cap = size; ln->buf = realloc(ln->buf, ln->cap * sizeof(char)); - OOM_CHECK(ln->buf); + if (!ln->buf) err(1, "realloc"); ln->len = MIN(ln->len, ln->cap-1); ln->buf[ln->len] = '\0'; } @@ -222,8 +223,7 @@ inputln_copy(struct inputln *dst, struct inputln *src) dst->buf = NULL; } dst->len = src->len; - dst->buf = strdup(src->buf); - OOM_CHECK(dst->buf); + dst->buf = astrdup(src->buf); dst->cap = src->len + 1; dst->cur = dst->len; } diff --git a/src/log.c b/src/log.c @@ -1,6 +1,7 @@ #include "log.h" #include "util.h" +#include <err.h> #include <stdbool.h> #include <stdlib.h> @@ -18,7 +19,7 @@ log_init(void) if (!envstr) return; log_file = fopen(envstr, "w+"); - if (!log_file) PANIC("Failed to open log file\n"); + if (!log_file) err(1, "fopen %s", envstr); log_active = true; } diff --git a/src/mpris.c b/src/mpris.c @@ -97,7 +97,7 @@ dbus_init(void) ret = dbus_bus_request_name(dbus_conn, "org.mpris.MediaPlayer2.tmus", DBUS_NAME_FLAG_REPLACE_EXISTING, &err); if (dbus_error_is_set(&err)) - PANIC("Failed to register as MPRIS service\n"); + errx(1, "mpris register failed"); log_info("DBus active!\n"); diff --git a/src/player_mpd.c b/src/player_mpd.c @@ -74,9 +74,9 @@ mpd_loaded_track_name(struct mpd_song *song) path = mpd_song_get_uri(song); sep = strrchr(path, '/'); - if (!sep) return strdup(path); + if (!sep) return astrdup(path); - return strdup(sep + 1); + return astrdup(sep + 1); } void @@ -169,7 +169,6 @@ player_update(void) if (current_song) { free(player.track_name); player.track_name = mpd_loaded_track_name(current_song); - OOM_CHECK(player.track_name); player.loaded = true; player.time_pos = mpd_status_get_elapsed_time(status); player.time_end = mpd_song_get_duration(current_song); diff --git a/src/ref.c b/src/ref.c @@ -1,13 +1,15 @@ #include "ref.h" #include "util.h" +#include <err.h> + struct ref * ref_alloc(void *data) { struct ref *ref; ref = malloc(sizeof(struct ref)); - if (!ref) return NULL; + if (!ref) err(1, "malloc"); ref->link = LINK_EMPTY; ref->data = data; diff --git a/src/strbuf.c b/src/strbuf.c @@ -2,6 +2,7 @@ #include "util.h" +#include <err.h> #include <stdio.h> #include <stdarg.h> #include <string.h> @@ -32,7 +33,8 @@ void strbuf_append(struct strbuf *strbuf, const char *fmt, ...) { va_list ap, cpy; - int blen, slen; + ssize_t slen; + int blen; va_copy(cpy, ap); @@ -40,13 +42,13 @@ strbuf_append(struct strbuf *strbuf, const char *fmt, ...) va_start(cpy, fmt); slen = vsnprintf(NULL, 0, fmt, cpy); - ASSERT(slen >= 0); + if (slen < 0) err(1, "snprintf"); va_end(cpy); if (blen + slen + 1 > strbuf->cap) { strbuf->cap = blen + slen + 1; strbuf->buf = realloc(strbuf->buf, strbuf->cap); - OOM_CHECK(strbuf->buf); + if (strbuf->buf) err(1, "realloc"); } va_start(ap, fmt); diff --git a/src/tui.c b/src/tui.c @@ -167,8 +167,7 @@ command_name_gen(const char *text, int fwd, int reset) while (index >= 0 && index < command_count) { if (!strncmp(commands[index].name, text, len)) { - dup = strdup(commands[index].name); - OOM_CHECK(dup); + dup = astrdup(commands[index].name); return dup; } index += dir; @@ -205,8 +204,7 @@ track_vis_name_gen(const char *text, int fwd, int reset) if (strcasestr(track->name, text)) { cur = link; - dup = strdup(track->name); - OOM_CHECK(dup); + dup = astrdup(track->name); return dup; } @@ -248,8 +246,7 @@ track_name_gen(const char *text, int fwd, int reset) if (strcasestr(track->name, text)) { cur = link; - dup = strdup(track->name); - OOM_CHECK(dup); + dup = astrdup(track->name); return dup; } @@ -280,8 +277,7 @@ tag_name_gen(const char *text, int fwd, int reset) tag = UPCAST(link, struct tag, link); if (strcasestr(tag->name, text)) { cur = link; - dup = strdup(tag->name); - OOM_CHECK(dup); + dup = astrdup(tag->name); return dup; } link = fwd ? link->next : link->prev; @@ -302,7 +298,6 @@ rename_current_tag(void) tag = UPCAST(link, struct tag, link); cmd = aprintf("rename %s", tag->name); - OOM_CHECK(cmd); select_cmd_pane(IMODE_EXECUTE); inputln_replace(history->input, cmd); free(cmd); @@ -538,7 +533,6 @@ rename_current_track(void) track = tracks_vis_track(link); cmd = aprintf("rename %s", track->name); - OOM_CHECK(cmd); select_cmd_pane(IMODE_EXECUTE); inputln_replace(history->input, cmd); free(cmd); @@ -1304,4 +1298,3 @@ tui_restore(void) endwin(); } - diff --git a/src/util.c b/src/util.c @@ -5,7 +5,7 @@ #include "tui.h" #include <execinfo.h> - +#include <err.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> @@ -56,20 +56,32 @@ error(const char *fmtstr, ...) } char * +astrdup(const char *str) +{ + char *alloc; + + alloc = strdup(str); + if (!alloc) err(1, "strdup"); + + return alloc; +} + +char * aprintf(const char *fmtstr, ...) { va_list ap, cpy; - size_t size; + ssize_t size; char *str; va_copy(cpy, ap); va_start(ap, fmtstr); size = vsnprintf(NULL, 0, fmtstr, ap); + if (size < 0) err(1, "snprintf"); va_end(ap); str = malloc(size + 1); - if (!str) return NULL; + if (!str) err(1, "malloc"); va_start(cpy, fmtstr); vsnprintf(str, size + 1, fmtstr, cpy); @@ -108,9 +120,7 @@ sanitized(const char *instr) char *clean; int i; - clean = strdup(instr); - OOM_CHECK(clean); - + clean = astrdup(instr); for (i = 0, p = instr; *p; p++) { if (strchr(allowed, *p)) clean[i++] = *p; diff --git a/src/util.h b/src/util.h @@ -8,7 +8,6 @@ #define PANIC(...) panic(__FILE__, __LINE__, "" __VA_ARGS__) #define ASSERT(x) assert((x), __FILE__, __LINE__, #x) -#define OOM_CHECK(x) assert((x) != NULL, __FILE__, __LINE__, "Out of Memory!") #define ERROR(...) error("" __VA_ARGS__) #define UPCAST(iter, type, link) LINK_UPCAST(iter, type, link) @@ -17,6 +16,7 @@ void panic(const char *file, int line, const char *msg, ...); void assert(int cond, const char *file, int line, const char *condstr); void error(const char *fmtstr, ...); +char *astrdup(const char *str); char *aprintf(const char *fmtstr, ...); char *appendstrf(char *alloc, const char *fmtstr, ...);