commit 58ed875b41c6e351484942ce4958c26f4c73baa0
parent c6e18247d29bfe3e12b43820cedbb95e6c78469d
Author: nibo <nibo@relim.de>
Date: Mon, 28 Jul 2025 08:48:03 +0200
Make it safe to pass NULL pointer to *_free() function
I had this rule eversince but didn't follow it closely.
Good to see that I'm human who makes mistakes like
expected. Let's not focus on not making mistakes but
to expect and properly handle a mistake.
Diffstat:
2 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/src/chord_diagram.c b/src/chord_diagram.c
@@ -336,6 +336,9 @@ string_diagram_string_count(struct StringDiagram *d)
static void
string_diagram_free(struct StringDiagram *d)
{
+ if (!d) {
+ return;
+ }
free(d->name);
free(d);
}
@@ -539,6 +542,9 @@ keyboard_diagram_copy_all_but_name(struct KeyboardDiagram *diagram)
static void
keyboard_diagram_free(struct KeyboardDiagram *d)
{
+ if (!d) {
+ return;
+ }
free(d->name);
free(d);
}
@@ -555,6 +561,9 @@ chord_diagram_new()
void
chord_diagram_free(struct ChordDiagram *d)
{
+ if (!d) {
+ return;
+ }
free(d->color);
switch (d->type) {
case CHORD_DIAGRAM_CONTENT_STRING:
diff --git a/src/config.c b/src/config.c
@@ -236,6 +236,9 @@ config_note_new(void)
static void
config_note_free(struct Note *note)
{
+ if (!note) {
+ return;
+ }
free(note->note);
free(note->sharp);
free(note->flat);
@@ -245,6 +248,9 @@ config_note_free(struct Note *note)
static void
config_notes_free(struct Note **notes)
{
+ if (!notes) {
+ return;
+ }
int i;
for (i = 0; i<7; i++) {
config_note_free(notes[i]);
@@ -1141,6 +1147,9 @@ config_load_from_data(const char *data)
void
config_free(struct Config *config)
{
+ if (!config) {
+ return;
+ }
int i;
free(config->metadata_separator);