]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-master: Make sure we can't accidentally add duplicate getopt args.
authorTimo Sirainen <tss@iki.fi>
Fri, 7 Aug 2015 12:32:14 +0000 (15:32 +0300)
committerTimo Sirainen <tss@iki.fi>
Fri, 7 Aug 2015 12:32:14 +0000 (15:32 +0300)
src/lib-master/master-service.c
src/lib-master/master-service.h

index 93b1527b9a7b4684b5563592d12f47cf3b46a336..fa2d74ced95a13437c3de58f23f5079f7ec37e19 100644 (file)
@@ -251,6 +251,8 @@ int master_getopt(struct master_service *service)
 {
        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 @@ int master_getopt(struct master_service *service)
        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)
 {
index 45470b0c8d7cbf7f13bfb81939612bfc5573db84..27aad5365d48d61d3bb69e937460a9a64602151c 100644 (file)
@@ -63,6 +63,9 @@ master_service_init(const char *name, enum master_service_flags flags,
 /* 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);