]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
createuser: fix parsing of --connection-limit argument
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 10 Feb 2020 15:14:58 +0000 (12:14 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Mon, 10 Feb 2020 15:14:58 +0000 (12:14 -0300)
The original coding failed to quote the argument properly.

Reported-by: Daniel Gustafsson
Discussion: 1B8AE66C-85AB-4728-9BB4-612E8E61C219@yesql.se

src/bin/scripts/createuser.c

index edadd136a6d86a47df29d3d7cd8eda940f6ce6bb..0fac9c2d50a662e5f57772a0b553f6eccf3b79d4 100644 (file)
@@ -62,7 +62,7 @@ main(int argc, char *argv[])
        enum trivalue prompt_password = TRI_DEFAULT;
        bool            echo = false;
        bool            interactive = false;
-       char       *conn_limit = NULL;
+       int                     conn_limit = -2;        /* less than minimum valid value */
        bool            pwprompt = false;
        char       *newpassword = NULL;
        char            newuser_buf[128];
@@ -89,6 +89,8 @@ main(int argc, char *argv[])
        while ((c = getopt_long(argc, argv, "h:p:U:g:wWedDsSaArRiIlLc:PE",
                                                        long_options, &optindex)) != -1)
        {
+               char   *endptr;
+
                switch (c)
                {
                        case 'h':
@@ -145,7 +147,14 @@ main(int argc, char *argv[])
                                login = TRI_NO;
                                break;
                        case 'c':
-                               conn_limit = pg_strdup(optarg);
+                               conn_limit = strtol(optarg, &endptr, 10);
+                               if (*endptr != '\0' || conn_limit < -1) /* minimum valid value */
+                               {
+                                       fprintf(stderr,
+                                                       _("%s: invalid value for --connection-limit: %s\n"),
+                                                       progname, optarg);
+                                       exit(1);
+                               }
                                break;
                        case 'P':
                                pwprompt = true;
@@ -300,8 +309,8 @@ main(int argc, char *argv[])
                appendPQExpBufferStr(&sql, " REPLICATION");
        if (replication == TRI_NO)
                appendPQExpBufferStr(&sql, " NOREPLICATION");
-       if (conn_limit != NULL)
-               appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit);
+       if (conn_limit >= -1)
+               appendPQExpBuffer(&sql, " CONNECTION LIMIT %d", conn_limit);
        if (roles.head != NULL)
        {
                SimpleStringListCell *cell;