]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Properly crash if we are not handling a CLI argument
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 10:25:16 +0000 (10:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 10:25:16 +0000 (10:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/assert.c [new file with mode: 0644]
src/cli/lib/assert.h [new file with mode: 0644]
src/cli/pakfire-builder.c
src/cli/pakfire.c

index 24a9db311189d0be47d9b777da91c0b22a8dac67..5122c4ef784b586dafd249e7f892c21a4b6a5b8a 100644 (file)
@@ -426,6 +426,8 @@ noinst_LTLIBRARIES += \
        libcli.la
 
 libcli_la_SOURCES = \
+       src/cli/lib/assert.c \
+       src/cli/lib/assert.h \
        src/cli/lib/build.c \
        src/cli/lib/build.h \
        src/cli/lib/check.c \
diff --git a/src/cli/lib/assert.c b/src/cli/lib/assert.c
new file mode 100644 (file)
index 0000000..dfd67e4
--- /dev/null
@@ -0,0 +1,31 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "assert.h"
+
+void __assert_unreachable(const char* file, const char* function, unsigned int line) {
+       fprintf(stderr, "Code should not have been reached in %s at %s:%u. Aborting.\n",
+               file, function, line);
+
+       abort();
+}
diff --git a/src/cli/lib/assert.h b/src/cli/lib/assert.h
new file mode 100644 (file)
index 0000000..39d1e75
--- /dev/null
@@ -0,0 +1,29 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_ASSERT_H
+#define PAKFIRE_CLI_ASSERT_H
+
+void __assert_unreachable(const char* file, const char* function, unsigned int line);
+
+#define assert_unreachable() \
+       __assert_unreachable(__FILE__, __FUNCTION__, __LINE__)
+
+#endif /* PAKFIRE_CLI_ASSERT_H */
index e2c04fbf989f21de3707869b955c6026fb87872d..ea2603770b978f867f712f822f600b0d528c3cda 100644 (file)
@@ -28,6 +28,7 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/string.h>
 
+#include "lib/assert.h"
 #include "lib/build.h"
 #include "lib/clean.h"
 #include "lib/command.h"
@@ -166,7 +167,7 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
                                break;
 
                        default:
-                               break;
+                               assert_unreachable();
                }
        }
 
index d2565786764f7db341ef84fcd6159abd7b8a0039..1cdf9dc0dd6f7db1fc728e70be7e7c35a5052a08 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <pakfire/pakfire.h>
 
+#include "lib/assert.h"
 #include "lib/clean.h"
 #include "lib/command.h"
 #include "lib/info.h"
@@ -188,7 +189,7 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
                                break;
 
                        default:
-                               break;
+                               assert_unreachable();
                }
        }