]> granicus.if.org Git - sudo/commitdiff
Use strtok_r() instead of strtok()
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 19 Jun 2015 18:35:51 +0000 (12:35 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 19 Jun 2015 18:35:51 +0000 (12:35 -0600)
14 files changed:
lib/util/getgrouplist.c
lib/util/regress/glob/globtest.c
lib/util/sudo_debug.c
plugins/group_file/getgrent.c
plugins/group_file/plugin_test.c
plugins/sample/sample_plugin.c
plugins/sudoers/group_plugin.c
plugins/sudoers/interfaces.c
plugins/sudoers/ldap.c
plugins/sudoers/logging.c
plugins/sudoers/regress/logging/check_wrap.c
plugins/sudoers/sudo_nss.c
plugins/sudoers/sudoreplay.c
plugins/sudoers/tsgetgrpw.c

index 0bcd7bef1ce008212844fdbd54b2a2df7534acd5..d05135919e754be253030eaa0a5d1b9f2c722d03 100644 (file)
@@ -72,8 +72,10 @@ sudo_getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroupsp)
     aix_setauthdb((char *) name);
 #endif
     if ((grset = getgrset(name)) != NULL) {
+       char *last;
        const char *errstr;
-       for (cp = strtok(grset, ","); cp != NULL; cp = strtok(NULL, ",")) {
+
+       for (cp = strtok_r(grset, ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
            gid = sudo_strtoid(cp, NULL, NULL, &errstr);
            if (errstr == NULL && gid != basegid) {
                if (ngroups == grpsize)
index d8930916d86817134798192f661a649c8e3a7e6c..27fe00fe35c64cc10bebbef6a955f83e8de70229 100644 (file)
@@ -118,7 +118,7 @@ main(int argc, char **argv)
                        }
                        *ep = '\0';
                        entry.flags = 0;
-                       for ((cp = strtok(cp, "|")); cp != NULL; (cp = strtok(NULL, "|"))) {
+                       for ((cp = strtok_r(cp, "|", &ep)); cp != NULL; (cp = strtok_r(NULL, "|", &ep))) {
                                if (strcmp(cp, "GLOB_APPEND") == 0)
                                        entry.flags |= GLOB_APPEND;
                                else if (strcmp(cp, "GLOB_DOOFFS") == 0)
index e9f1d51aff9f47af015909b96918cdcd82c149c2..fa9fd7b98cf0e2ddd4945209057a4cc90b59dbb8 100644 (file)
@@ -148,7 +148,7 @@ static struct sudo_debug_output *
 sudo_debug_new_output(struct sudo_debug_instance *instance,
     struct sudo_debug_file *debug_file)
 {
-    char *buf, *cp, *subsys, *pri;
+    char *buf, *cp, *last, *subsys, *pri;
     struct sudo_debug_output *output;
     unsigned int i, j;
 
@@ -205,7 +205,7 @@ sudo_debug_new_output(struct sudo_debug_instance *instance,
     buf = strdup(debug_file->debug_flags);
     if (buf == NULL)
        goto bad;
-    for ((cp = strtok(buf, ",")); cp != NULL; (cp = strtok(NULL, ","))) {
+    for ((cp = strtok_r(buf, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
        /* Should be in the form subsys@pri. */
        subsys = cp;
        if ((pri = strchr(cp, '@')) == NULL)
index e8af8177068310cda160224a492df29b3f984614..d159d6563cb3cd9166d67e3694a370f04cdb12f5 100644 (file)
@@ -124,11 +124,13 @@ next_entry:
     if (len > 0 && colon[len - 1] == '\n')
        colon[len - 1] = '\0';
     if (*colon != '\0') {
+       char *last;
+
        gr.gr_mem = gr_mem;
-       cp = strtok(colon, ",");
+       cp = strtok_r(colon, ",", &last);
        for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
            gr.gr_mem[n] = cp;
-           cp = strtok(NULL, ",");
+           cp = strtok_r(NULL, ",", &last);
        }
        gr.gr_mem[n++] = NULL;
     } else
index cc8a0865fb0ac7c1b72bdb3d24f6565a845e8e3e..1be808ab5f35d96474c270cbfbca8773efee4e06 100644 (file)
@@ -125,13 +125,15 @@ group_plugin_load(char *plugin_info)
             }
         }
        if (ac != 0)    {
+           char *last;
+
            argv = malloc(ac * sizeof(char *));
            if (argv == NULL) {
                perror(NULL);
                return -1;
            }
            ac = 0;
-           for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
+           for ((cp = strtok_r(args, " \t", &last)); cp != NULL; (cp = strtok_r(NULL, " \t", &last)))
                argv[ac++] = cp;
        }
     }
index 75c795b43de7c9919b24b21f6f19a25ee084c13f..5f7ca5f23d7320de9b4f3599819186b02d2ec7fa 100644 (file)
@@ -243,7 +243,7 @@ build_command_info(const char *command)
 static char *
 find_editor(int nfiles, char * const files[], char **argv_out[])
 {
-    char *cp, **ep, **nargv, *editor, *editor_path;
+    char *cp, *last, **ep, **nargv, *editor, *editor_path;
     int ac, i, nargc, wasblank;
 
     /* Lookup EDITOR in user's environment. */
@@ -275,7 +275,7 @@ find_editor(int nfiles, char * const files[], char **argv_out[])
        }
     }
     /* If we can't find the editor in the user's PATH, give up. */
-    cp = strtok(editor, " \t");
+    cp = strtok_r(editor, " \t", &last);
     if (cp == NULL ||
        (editor_path = find_in_path(editor, plugin_state.envp)) == NULL) {
        free(editor);
@@ -291,7 +291,7 @@ find_editor(int nfiles, char * const files[], char **argv_out[])
     }
     for (ac = 0; cp != NULL && ac < nargc; ac++) {
        nargv[ac] = cp;
-       cp = strtok(NULL, " \t");
+       cp = strtok_r(NULL, " \t", &last);
     }
     nargv[ac++] = "--";
     for (i = 0; i < nfiles; )
index 20e094d2f8bf269091283cb662ac03ee6795771d..7afc357d1281fb60fb5896d6aefce0023fae0d3c 100644 (file)
@@ -124,7 +124,7 @@ group_plugin_load(char *plugin_info)
     if (args != NULL) {
        int ac = 0;
        bool wasblank = true;
-       char *cp;
+       char *cp, *last;
 
         for (cp = args; *cp != '\0'; cp++) {
             if (isblank((unsigned char)*cp)) {
@@ -134,14 +134,14 @@ group_plugin_load(char *plugin_info)
                 ac++;
             }
         }
-       if (ac != 0)    {
+       if (ac != 0) {
            argv = reallocarray(NULL, ac, sizeof(char *));
            if (argv == NULL) {
                sudo_warnx(U_("unable to allocate memory"));
                goto done;
            }
            ac = 0;
-           for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
+           for ((cp = strtok_r(args, " \t", &last)); cp != NULL; (cp = strtok_r(NULL, " \t", &last)))
                argv[ac++] = cp;
        }
     }
index d205b00fb7fb4c3c67f2401c206db2d813f849da..f077bea2680131c9d8e30d1d5094596ba31456dc 100644 (file)
@@ -57,14 +57,14 @@ static struct interface_list interfaces;
 bool
 set_interfaces(const char *ai)
 {
-    char *addrinfo, *addr, *mask;
+    char *addrinfo, *addr, *mask, *last;
     struct interface *ifp;
     bool rval = false;
     debug_decl(set_interfaces, SUDOERS_DEBUG_NETIF)
 
     if ((addrinfo = strdup(ai)) == NULL)
        debug_return_bool(false);
-    for (addr = strtok(addrinfo, " \t"); addr != NULL; addr = strtok(NULL, " \t")) {
+    for (addr = strtok_r(addrinfo, " \t", &last); addr != NULL; addr = strtok_r(NULL, " \t", &last)) {
        /* Separate addr and mask. */
        if ((mask = strchr(addr, '/')) == NULL)
            continue;
index ff84c6d7cc0c9f7b43edf9558946f6fd9d333685..15f9efdb25fe37613d2768905594e315379bb564 100644 (file)
@@ -425,7 +425,7 @@ struct sudo_nss sudo_nss_ldap = {
 static bool
 sudo_ldap_conf_add_ports(void)
 {
-    char *host, *port, defport[13];
+    char *host, *last, *port, defport[13];
     char hostbuf[LINE_MAX * 2];
     int len;
     debug_decl(sudo_ldap_conf_add_ports, SUDOERS_DEBUG_LDAP)
@@ -437,7 +437,7 @@ sudo_ldap_conf_add_ports(void)
        debug_return_bool(false);
     }
 
-    for ((host = strtok(ldap_conf.host, " \t")); host; (host = strtok(NULL, " \t"))) {
+    for ((host = strtok_r(ldap_conf.host, " \t", &last)); host; (host = strtok_r(NULL, " \t", &last))) {
        if (hostbuf[0] != '\0')
            CHECK_STRLCAT(hostbuf, " ", sizeof(hostbuf));
        CHECK_STRLCAT(hostbuf, host, sizeof(hostbuf));
@@ -471,20 +471,21 @@ static int
 sudo_ldap_parse_uri(const struct ldap_config_str_list *uri_list)
 {
     const struct ldap_config_str *entry;
-    char *buf, *uri, *host, *cp, *port;
-    char hostbuf[LINE_MAX];
+    char *buf, hostbuf[LINE_MAX];
     int nldap = 0, nldaps = 0;
     int rc = -1;
     debug_decl(sudo_ldap_parse_uri, SUDOERS_DEBUG_LDAP)
 
     hostbuf[0] = '\0';
     STAILQ_FOREACH(entry, uri_list, entries) {
+       char *cp, *host, *last, *port, *uri;
+
        buf = strdup(entry->val);
        if (buf == NULL) {
            sudo_warnx(U_("unable to allocate memory"));
            goto done;
        }
-       for ((uri = strtok(buf, " \t")); uri != NULL; (uri = strtok(NULL, " \t"))) {
+       for ((uri = strtok_r(buf, " \t", &last)); uri != NULL; (uri = strtok_r(NULL, " \t", &last))) {
            if (strncasecmp(uri, "ldap://", 7) == 0) {
                nldap++;
                host = uri + 7;
index 57bc5b9a659b2ad406625ab4955d8ace1d5e751c..7fd0db99b971d497258c47279e48a9d356939fe4 100644 (file)
@@ -646,7 +646,7 @@ send_mail(const char *fmt, ...)
            break;
        case 0:
            {
-               char *argv[MAX_MAILFLAGS + 1];
+               char *last, *argv[MAX_MAILFLAGS + 1];
                char *mflags, *mpath = def_mailerpath;
                int i;
 
@@ -673,10 +673,10 @@ send_mail(const char *fmt, ...)
                    argv[0] = mpath;
 
                i = 1;
-               if ((p = strtok(mflags, " \t"))) {
+               if ((p = strtok_r(mflags, " \t", &last))) {
                    do {
                        argv[i] = p;
-                   } while (++i < MAX_MAILFLAGS && (p = strtok(NULL, " \t")));
+                   } while (++i < MAX_MAILFLAGS && (p = strtok_r(NULL, " \t", &last)));
                }
                argv[i] = NULL;
 
index 3cab4354ae91e1bfeeaf8911d828c3295a290108..90b14e6fae019f7da0c552bdc63908b39d43033b 100644 (file)
@@ -57,7 +57,7 @@ main(int argc, char *argv[])
 {
     size_t len;
     FILE *fp;
-    char *cp, *dash, *line, lines[2][2048];
+    char *line, lines[2][2048];
     int lineno = 0;
     int which = 0;
 
@@ -78,14 +78,18 @@ main(int argc, char *argv[])
      * 60-80,40
      */
     while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {
+       char *cp, *last;
+
        len = strcspn(line, "\n");
        line[len] = '\0';
 
        /* If we read the 2nd line, parse list of line lengths and check. */
        if (which) {
            lineno++;
-           for (cp = strtok(lines[1], ","); cp != NULL; cp = strtok(NULL, ",")) {
+           for (cp = strtok_r(lines[1], ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
+               char *dash;
                size_t maxlen;
+
                /* May be either a number or a range. */
                dash = strchr(cp, '-');
                if (dash != NULL) {
index 1b709df8861f99aabfe8e8e5844c5b2787329bef..2065d7baa04e970da7ac50b61ba666337937427a 100644 (file)
@@ -69,7 +69,7 @@ struct sudo_nss_list *
 sudo_read_nss(void)
 {
     FILE *fp;
-    char *cp, *line = NULL;
+    char *line = NULL;
     size_t linesize = 0;
 #ifdef HAVE_SSSD
     bool saw_sss = false;
@@ -86,6 +86,8 @@ sudo_read_nss(void)
        goto nomatch;
 
     while (sudo_parseln(&line, &linesize, NULL, fp) != -1) {
+       char *cp, *last;
+
        /* Skip blank or comment lines */
        if (*line == '\0')
            continue;
@@ -95,7 +97,7 @@ sudo_read_nss(void)
            continue;
 
        /* Parse line */
-       for ((cp = strtok(line + 8, " \t")); cp != NULL; (cp = strtok(NULL, " \t"))) {
+       for ((cp = strtok_r(line + 8, " \t", &last)); cp != NULL; (cp = strtok_r(NULL, " \t", &last))) {
            if (strcasecmp(cp, "files") == 0 && !saw_files) {
                SUDO_NSS_CHECK_UNUSED(sudo_nss_file, "files");
                TAILQ_INSERT_TAIL(&snl, &sudo_nss_file, entries);
@@ -149,7 +151,7 @@ struct sudo_nss_list *
 sudo_read_nss(void)
 {
     FILE *fp;
-    char *cp, *ep, *line = NULL;
+    char *cp, *ep, *last, *line = NULL;
     size_t linesize = 0;
 #ifdef HAVE_SSSD
     bool saw_sss = false;
@@ -178,7 +180,7 @@ sudo_read_nss(void)
            continue;
 
        /* Parse line */
-       for ((cp = strtok(cp, ",")); cp != NULL; (cp = strtok(NULL, ","))) {
+       for ((cp = strtok_r(cp, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
            /* Trim leading whitespace. */
            while (isspace((unsigned char)*cp))
                cp++;
index 054f3cd508ebd190d238b11f573b126853a1c906..2b9d31e3995feb1816a064d1311d7ca0c237ae89 100644 (file)
@@ -247,7 +247,7 @@ main(int argc, char *argv[])
        case 'f':
            /* Set the replay filter. */
            def_filter = false;
-           for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) {
+           for (cp = strtok_r(optarg, ",", &ep); cp; cp = strtok_r(NULL, ",", &ep)) {
                if (strcmp(cp, "stdout") == 0)
                    io_log_files[IOFD_STDOUT].enabled = true;
                else if (strcmp(cp, "stderr") == 0)
index e341a16eb80e76b1751a513f6c86cc42a5411150..f752bfcd4ad67732cdda0f0581a5d58173e551b1 100644 (file)
@@ -274,11 +274,13 @@ next_entry:
     if (len > 0 && colon[len - 1] == '\n')
        colon[len - 1] = '\0';
     if (*colon != '\0') {
+       char *last;
+
        gr.gr_mem = gr_mem;
-       cp = strtok(colon, ",");
+       cp = strtok_r(colon, ",", &last);
        for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
            gr.gr_mem[n] = cp;
-           cp = strtok(NULL, ",");
+           cp = strtok_r(NULL, ",", &last);
        }
        gr.gr_mem[n++] = NULL;
     } else