diff mbox series

Do not register multiple handlers with same name

Message ID 20210429123011.592198-1-stefano.babic@babic.homelinux.org
State Accepted
Headers show
Series Do not register multiple handlers with same name | expand

Commit Message

Stefano Babic April 29, 2021, 12:30 p.m. UTC
From: Stefano Babic <sbabic@denx.de>

Each handler must have a unique name. Check and reject registgration of
new handlers if they were already registered under the same name.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 core/handler.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/core/handler.c b/core/handler.c
index fdf178b..0a713cf 100644
--- a/core/handler.c
+++ b/core/handler.c
@@ -25,10 +25,20 @@  static unsigned long handler_index = ULONG_MAX;
 int register_handler(const char *desc,
 		handler installer, HANDLER_MASK mask, void *data)
 {
+	int i;
 
-	if (nr_installers > MAX_INSTALLER_HANDLER - 1)
+	if ((nr_installers > MAX_INSTALLER_HANDLER - 1) || !desc)
 		return -1;
 
+	/*
+	 * Do not register the same handler twice
+	 */
+	for (i = 0; i < nr_installers; i++) {
+		if ((strlen(desc) == strlen(supported_types[i].desc)) &&
+			strcmp(desc, supported_types[i].desc) == 0)
+			return -1;
+	}
+
 	strlcpy(supported_types[nr_installers].desc, desc,
 		      sizeof(supported_types[nr_installers].desc));
 	supported_types[nr_installers].installer = installer;