dovecot-2.2: lib-master: Make sure we can't accidentally add dup...

dovecot at dovecot.org dovecot at dovecot.org
Fri Aug 7 12:33:15 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/ff17864ba6e0
changeset: 18925:ff17864ba6e0
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Aug 07 15:32:14 2015 +0300
description:
lib-master: Make sure we can't accidentally add duplicate getopt args.

diffstat:

 src/lib-master/master-service.c |  19 +++++++++++++++++++
 src/lib-master/master-service.h |   3 +++
 2 files changed, 22 insertions(+), 0 deletions(-)

diffs (49 lines):

diff -r f611d66e56f6 -r ff17864ba6e0 src/lib-master/master-service.c
--- a/src/lib-master/master-service.c	Fri Aug 07 15:30:26 2015 +0300
+++ b/src/lib-master/master-service.c	Fri Aug 07 15:32:14 2015 +0300
@@ -251,6 +251,8 @@
 {
 	int c;
 
+	i_assert(master_getopt_str_is_valid(service->getopt_str));
+
 	while ((c = getopt(service->argc, service->argv,
 			   service->getopt_str)) > 0) {
 		if (!master_service_parse_option(service, c, optarg))
@@ -259,6 +261,23 @@
 	return c;
 }
 
+bool master_getopt_str_is_valid(const char *str)
+{
+	unsigned int i, j;
+
+	/* make sure there are no duplicates. there are few enough characters
+	   that this should be fast enough. */
+	for (i = 0; str[i] != '\0'; i++) {
+		if (str[i] == ':' || str[i] == '+' || str[i] == '-')
+			continue;
+		for (j = i+1; str[j] != '\0'; j++) {
+			if (str[i] == str[j])
+				return FALSE;
+		}
+	}
+	return TRUE;
+}
+
 void master_service_init_log(struct master_service *service,
 			     const char *prefix)
 {
diff -r f611d66e56f6 -r ff17864ba6e0 src/lib-master/master-service.h
--- a/src/lib-master/master-service.h	Fri Aug 07 15:30:26 2015 +0300
+++ b/src/lib-master/master-service.h	Fri Aug 07 15:32:14 2015 +0300
@@ -63,6 +63,9 @@
 /* Call getopt() and handle internal parameters. Return values are the same as
    getopt()'s. */
 int master_getopt(struct master_service *service);
+/* Returns TRUE if str is a valid getopt_str. Currently this only checks for
+   duplicate args so they aren't accidentally added. */
+bool master_getopt_str_is_valid(const char *str);
 /* Parser command line option. Returns TRUE if processed. */
 bool master_service_parse_option(struct master_service *service,
 				 int opt, const char *arg);


More information about the dovecot-cvs mailing list