From 4a2a9ec685290fa0cfc98845ee60f01dd3680340 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Mon, 20 Mar 2023 16:04:17 +0100 Subject: [PATCH] Replace uses of die() with err() + return --- src/emu/cpu.c | 6 ++++-- src/emu/model_pvt.c | 12 ++++++++++-- src/emu/ovniemu.c | 12 ++++++++---- src/emu/pv/cfg.c | 2 +- src/emu/pv/pcf.c | 36 ++++++++++++++++++++++++------------ src/emu/pv/prv.c | 3 +-- src/emu/stream.c | 2 +- src/emu/task.c | 5 ++++- src/emu/thread.c | 28 ++++++++++++++++++---------- 9 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/emu/cpu.c b/src/emu/cpu.c index 8c5b3f4..9ba2d40 100644 --- a/src/emu/cpu.c +++ b/src/emu/cpu.c @@ -107,8 +107,10 @@ cpu_init_end(struct cpu *cpu) } for (int i = 0; i < CPU_CHAN_MAX; i++) { - if (chan_name[i] == NULL) - die("chan_name is null"); + if (chan_name[i] == NULL) { + err("chan_name is null"); + return -1; + } chan_init(&cpu->chan[i], CHAN_SINGLE, chan_fmt, cpu->gindex, chan_name[i]); diff --git a/src/emu/model_pvt.c b/src/emu/model_pvt.c index b270317..b082416 100644 --- a/src/emu/model_pvt.c +++ b/src/emu/model_pvt.c @@ -18,8 +18,12 @@ create_values(const struct model_pvt_spec *pvt, if (q == NULL) return 0; - for (const struct pcf_value_label *p = q; p->label != NULL; p++) - pcf_add_value(t, p->value, p->label); + for (const struct pcf_value_label *p = q; p->label != NULL; p++) { + if (pcf_add_value(t, p->value, p->label) == NULL) { + err("pcf_add_value failed"); + return -1; + } + } return 0; } @@ -47,6 +51,10 @@ create_type(const struct model_pvt_spec *pvt, } struct pcf_type *pcftype = pcf_add_type(pcf, type, label); + if (pcftype == NULL) { + err("pcf_add_type failed"); + return -1; + } if (create_values(pvt, pcftype, i) != 0) { err("create_values failed"); diff --git a/src/emu/ovniemu.c b/src/emu/ovniemu.c index 37199ce..17e3e77 100644 --- a/src/emu/ovniemu.c +++ b/src/emu/ovniemu.c @@ -28,11 +28,15 @@ main(int argc, char *argv[]) return 1; } - if (emu_init(emu, argc, argv) != 0) - die("emu_init failed\n"); + if (emu_init(emu, argc, argv) != 0) { + err("emu_init failed\n"); + return 1; + } - if (emu_connect(emu) != 0) - die("emu_connect failed\n"); + if (emu_connect(emu) != 0) { + err("emu_connect failed\n"); + return 1; + } signal(SIGINT, stop_emulation); diff --git a/src/emu/pv/cfg.c b/src/emu/pv/cfg.c index 62d075c..3384fb7 100644 --- a/src/emu/pv/cfg.c +++ b/src/emu/pv/cfg.c @@ -69,7 +69,7 @@ copy_recursive(const char *src, const char *dst) char *newdst = calloc(1, PATH_MAX); if (newdst == NULL) { - die("calloc failed:"); + err("calloc failed:"); return -1; } diff --git a/src/emu/pv/pcf.c b/src/emu/pv/pcf.c index 8a6feb9..bf86178 100644 --- a/src/emu/pv/pcf.c +++ b/src/emu/pv/pcf.c @@ -153,20 +153,26 @@ pcf_add_type(struct pcf *pcf, int type_id, const char *label) { struct pcf_type *pcftype = pcf_find_type(pcf, type_id); - if (pcftype != NULL) - die("PCF type %d already defined\n", type_id); + if (pcftype != NULL) { + err("PCF type %d already defined", type_id); + return NULL; + } pcftype = calloc(1, sizeof(struct pcf_type)); - if (pcftype == NULL) - die("calloc failed: %s\n", strerror(errno)); + if (pcftype == NULL) { + err("calloc failed:"); + return NULL; + } pcftype->id = type_id; pcftype->values = NULL; pcftype->nvalues = 0; int len = snprintf(pcftype->label, MAX_PCF_LABEL, "%s", label); - if (len >= MAX_PCF_LABEL) - die("PCF type label too long\n"); + if (len >= MAX_PCF_LABEL) { + err("PCF type label too long"); + return NULL; + } HASH_ADD_INT(pcf->types, id, pcftype); @@ -193,18 +199,24 @@ pcf_add_value(struct pcf_type *type, int value, const char *label) { struct pcf_value *pcfvalue = pcf_find_value(type, value); - if (pcfvalue != NULL) - die("PCF value %d already in type %d\n", value, type->id); + if (pcfvalue != NULL) { + err("PCF value %d already in type %d", value, type->id); + return NULL; + } pcfvalue = calloc(1, sizeof(struct pcf_value)); - if (pcfvalue == NULL) - die("calloc failed: %s\n", strerror(errno)); + if (pcfvalue == NULL) { + err("calloc failed:"); + return NULL; + } pcfvalue->value = value; int len = snprintf(pcfvalue->label, MAX_PCF_LABEL, "%s", label); - if (len >= MAX_PCF_LABEL) - die("PCF value label too long\n"); + if (len >= MAX_PCF_LABEL) { + err("PCF value label too long"); + return NULL; + } HASH_ADD_INT(type->values, value, pcfvalue); diff --git a/src/emu/pv/prv.c b/src/emu/pv/prv.c index c7fa205..e4bd3dd 100644 --- a/src/emu/pv/prv.c +++ b/src/emu/pv/prv.c @@ -34,8 +34,7 @@ prv_open(struct prv *prv, long nrows, const char *path) FILE *f = fopen(path, "w"); if (f == NULL) { - die("cannot open file '%s' for writting: %s\n", - path, strerror(errno)); + err("cannot open file '%s' for writting:", path); return -1; } diff --git a/src/emu/stream.c b/src/emu/stream.c index a21d704..e53aabb 100644 --- a/src/emu/stream.c +++ b/src/emu/stream.c @@ -147,7 +147,7 @@ int stream_clkoff_set(struct stream *stream, int64_t clkoff) { if (stream->cur_ev) { - die("cannot set clokoff in started stream '%s'", + err("cannot set clokoff in started stream '%s'", stream->relpath); return -1; } diff --git a/src/emu/task.c b/src/emu/task.c index 12b674e..92faef6 100644 --- a/src/emu/task.c +++ b/src/emu/task.c @@ -300,7 +300,10 @@ task_create_pcf_types(struct pcf_type *pcftype, struct task_type *types) } } - pcf_add_value(pcftype, tt->gid, tt->label); + if (pcf_add_value(pcftype, tt->gid, tt->label) == NULL) { + err("pcf_add_value failed"); + return -1; + } } return ret; diff --git a/src/emu/thread.c b/src/emu/thread.c index 0969f48..f74a341 100644 --- a/src/emu/thread.c +++ b/src/emu/thread.c @@ -163,8 +163,12 @@ create_values(struct pcf_type *t, int i) if (q == NULL) return 0; - for (const struct pcf_value_label *p = *q; p->label != NULL; p++) - pcf_add_value(t, p->value, p->label); + for (const struct pcf_value_label *p = *q; p->label != NULL; p++) { + if (pcf_add_value(t, p->value, p->label) == NULL) { + err("pcf_add_value failed"); + return -1; + } + } return 0; } @@ -179,6 +183,10 @@ create_type(struct pcf *pcf, int i) const char *label = pvt_name[i]; struct pcf_type *pcftype = pcf_add_type(pcf, type, label); + if (pcftype == NULL) { + err("pcf_add_type failed"); + return -1; + } if (create_values(pcftype, i) != 0) { err("create_values failed"); @@ -255,7 +263,7 @@ thread_set_state(struct thread *th, enum thread_state state) { /* The state must be updated when a cpu is set */ if (th->cpu == NULL) { - die("thread %d doesn't have a CPU", th->tid); + err("thread %d doesn't have a CPU", th->tid); return -1; } @@ -385,12 +393,12 @@ int thread_set_cpu(struct thread *th, struct cpu *cpu) { if (cpu == NULL) { - err("thread_set_cpu: CPU is NULL\n"); + err("CPU is NULL"); return -1; } if (th->cpu != NULL) { - err("thread_set_cpu: thread %d already has a CPU\n", th->tid); + err("thread %d already has a CPU", th->tid); return -1; } @@ -399,7 +407,7 @@ thread_set_cpu(struct thread *th, struct cpu *cpu) /* Update cpu channel */ struct chan *c = &th->chan[TH_CHAN_CPU]; if (chan_set(c, value_int64(cpu->gindex)) != 0) { - err("thread_set_cpu: chan_set failed\n"); + err("chan_set failed"); return -1; } @@ -410,7 +418,7 @@ int thread_unset_cpu(struct thread *th) { if (th->cpu == NULL) { - err("thread_unset_cpu: thread %d doesn't have a CPU\n", th->tid); + err("thread %d doesn't have a CPU", th->tid); return -1; } @@ -418,7 +426,7 @@ thread_unset_cpu(struct thread *th) struct chan *c = &th->chan[TH_CHAN_CPU]; if (chan_set(c, value_null()) != 0) { - err("thread_set_cpu: chan_set failed\n"); + err("chan_set failed"); return -1; } @@ -429,7 +437,7 @@ int thread_migrate_cpu(struct thread *th, struct cpu *cpu) { if (th->cpu == NULL) { - die("thread_migrate_cpu: thread %d doesn't have a CPU\n", th->tid); + err("thread %d doesn't have a CPU", th->tid); return -1; } @@ -437,7 +445,7 @@ thread_migrate_cpu(struct thread *th, struct cpu *cpu) struct chan *c = &th->chan[TH_CHAN_CPU]; if (chan_set(c, value_int64(cpu->gindex)) != 0) { - err("thread_set_cpu: chan_set failed\n"); + err("chan_set failed"); return -1; }