From 6e0aed337477150c40da4b439d7436894bc4d12b Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Sep 2021 09:39:55 +0200
Subject: [PATCH] FIX: Fix issue #96

The lektord daemon crashed when passing one argument to the `addid`
command. This was due to the way the loop was written, it was possible
to call strtol with a NULL char pointer, thus the SEGV.
---
 src/base/commands.c  | 8 ++++++--
 src/database/queue.c | 9 ++++++++-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/base/commands.c b/src/base/commands.c
index edd5f42b..20926b1f 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -368,8 +368,12 @@ command_addid(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX])
     RETURN_UNLESS(args, "Invalid argument", false);
     errno = 0;
     int i, id;
-    for (i = 0, id = strtol(args[i], NULL, 0); id != 0 && !errno; ++i, id = strtol(args[i], NULL, 0))
-        errno |= database_queue_add_id(srv->db, id, 1);
+    for (i = 0; !errno && args[i]; ++i) {
+        id = strtol(args[i], NULL, 0);
+        if (id == 0)
+            break;
+        errno |= !database_queue_add_id(srv->db, id, 1);
+    }
     srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
     return !errno;
 }
diff --git a/src/database/queue.c b/src/database/queue.c
index 9e20c08c..10427be6 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -294,11 +294,18 @@ database_queue_add_id(lkt_db *db, int id, int priority)
     reorder(db, priority, error_no_stmt);
 
     SQLITE_EXEC(db, "COMMIT;", error);
-    LOG_INFO("DB-DEBUG", "Added kara with id %d and priority %d", id, priority);
     return true;
 error:
+    LOG_ERROR("DB",
+              "Error while adding the kara id %d into the queue with the "
+              "priority %d",
+              id, priority);
     sqlite3_finalize(stmt);
 error_no_stmt:
+    LOG_ERROR("DB",
+              "Need to rollback the queue because of failed insertion of id "
+              "%d with priority %d in the queue",
+              id, priority);
     SQLITE_DO_ROLLBACK(db);
     __queue_resequence(db);
     return false;
-- 
GitLab