Use strtok_r instead of strtok
authorChristian Biesinger <cbiesinger@google.com>
Sat, 2 Nov 2019 17:09:31 +0000 (12:09 -0500)
committerChristian Biesinger <cbiesinger@google.com>
Wed, 6 Nov 2019 20:03:11 +0000 (14:03 -0600)
Improves threadsafety. This will be important when the patch series at
https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/176
lands.

gdb/ChangeLog:

2019-11-06  Christian Biesinger  <cbiesinger@google.com>

* linux-tdep.c (linux_info_proc): Use strtok_r instead of strtok.
* mi/mi-main.c (output_cores): Likewise.
* nat/linux-osdata.c (linux_xfer_osdata_cpus): Likewise.
(linux_xfer_osdata_modules): Likewise.
* remote.c (register_remote_support_xml): Likewise.
* sparc64-tdep.c (adi_is_addr_mapped): Likewise.
* xml-syscall.c (syscall_create_syscall_desc): Likewise.

gdb/gdbserver/ChangeLog:

2019-11-06  Christian Biesinger  <cbiesinger@google.com>

* linux-x86-low.c (x86_linux_process_qsupported): Use strtok_r
instead of strtok.
* server.c (handle_query): Likewise.
(captured_main): Likewise.

Change-Id: Ief6138965a24398e5fc064598cd8f2abd3b5047c

gdb/ChangeLog
gdb/gdbserver/ChangeLog
gdb/gdbserver/linux-x86-low.c
gdb/gdbserver/server.c
gdb/linux-tdep.c
gdb/mi/mi-main.c
gdb/nat/linux-osdata.c
gdb/remote.c
gdb/sparc64-tdep.c
gdb/xml-syscall.c

index 048c2dd484369a037d7233a83e2f28104bf8383b..f43d3a5aed8afd891620a681aba8f41dc6891dd9 100644 (file)
@@ -1,3 +1,13 @@
+2019-11-06  Christian Biesinger  <cbiesinger@google.com>
+
+       * linux-tdep.c (linux_info_proc): Use strtok_r instead of strtok.
+       * mi/mi-main.c (output_cores): Likewise.
+       * nat/linux-osdata.c (linux_xfer_osdata_cpus): Likewise.
+       (linux_xfer_osdata_modules): Likewise.
+       * remote.c (register_remote_support_xml): Likewise.
+       * sparc64-tdep.c (adi_is_addr_mapped): Likewise.
+       * xml-syscall.c (syscall_create_syscall_desc): Likewise.
+
 2019-11-06  Tom Tromey  <tom@tromey.com>
 
        * tui/tui-interp.c: Don't include readline.h.
index 35684dbe82ede259222971cf55c86afee8abe0a1..9d0afaaf28e61eb5ae46a7e559bb24ae905f22de 100644 (file)
@@ -1,3 +1,13 @@
+2019-11-06  Christian Biesinger  <cbiesinger@google.com>
+
+       * linux-tdep.c (linux_info_proc): Use strtok_r instead of strtok.
+       * mi/mi-main.c (output_cores): Likewise.
+       * nat/linux-osdata.c (linux_xfer_osdata_cpus): Likewise.
+       (linux_xfer_osdata_modules): Likewise.
+       * remote.c (register_remote_support_xml): Likewise.
+       * sparc64-tdep.c (adi_is_addr_mapped): Likewise.
+       * xml-syscall.c (syscall_create_syscall_desc): Likewise.
+
 2019-11-01  Christian Biesinger  <cbiesinger@google.com>
 
        * configure: Regenerate.
index cafff6b109bea9fedeaab6fb59b65e4e21b451f0..54bd2a26de8eb59973fb575445abfe9f036e0954 100644 (file)
@@ -912,9 +912,11 @@ x86_linux_process_qsupported (char **features, int count)
       if (startswith (feature, "xmlRegisters="))
        {
          char *copy = xstrdup (feature + 13);
-         char *p;
 
-         for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ","))
+         char *saveptr;
+         for (char *p = strtok_r (copy, ",", &saveptr);
+              p != NULL;
+              p = strtok_r (NULL, ",", &saveptr))
            {
              if (strcmp (p, "i386") == 0)
                {
index 59e8a55313e501a2cb39f55e48e52fed9d0680b2..c5f7176cff83755ea98072ce2013695b43b64223 100644 (file)
@@ -2268,9 +2268,10 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
 
          /* Two passes, to avoid nested strtok calls in
             target_process_qsupported.  */
-         for (p = strtok (p + 1, ";");
+         char *saveptr;
+         for (p = strtok_r (p + 1, ";", &saveptr);
               p != NULL;
-              p = strtok (NULL, ";"))
+              p = strtok_r (NULL, ";", &saveptr))
            {
              count++;
              qsupported = XRESIZEVEC (char *, qsupported, count);
@@ -3633,12 +3634,11 @@ captured_main (int argc, char *argv[])
        }
       else if (startswith (*next_arg, "--disable-packet="))
        {
-         char *packets, *tok;
-
-         packets = *next_arg += sizeof ("--disable-packet=") - 1;
-         for (tok = strtok (packets, ",");
+         char *packets = *next_arg += sizeof ("--disable-packet=") - 1;
+         char *saveptr;
+         for (char *tok = strtok_r (packets, ",", &saveptr);
               tok != NULL;
-              tok = strtok (NULL, ","))
+              tok = strtok_r (NULL, ",", &saveptr))
            {
              if (strcmp ("vCont", tok) == 0)
                disable_packet_vCont = true;
index 567b01c5d15cf1b9576f370a6548f06824b8bc4a..18cee91dd3e88f2b67a19c5a82406d115adf164c 100644 (file)
@@ -839,9 +839,10 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
                           "      Size", "    Offset", "objfile");
            }
 
-         for (line = strtok (map.get (), "\n");
+         char *saveptr;
+         for (line = strtok_r (map.get (), "\n", &saveptr);
               line;
-              line = strtok (NULL, "\n"))
+              line = strtok_r (NULL, "\n", &saveptr))
            {
              ULONGEST addr, endaddr, offset, inode;
              const char *permissions, *device, *mapping_filename;
index 0e99fa39bdda43e0759e72050e646624e3530490..c14897a5f719caf48d080be6301ff818a7c3a784 100644 (file)
@@ -696,8 +696,9 @@ output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
   ui_out_emit_list list_emitter (uiout, field_name);
   auto cores = make_unique_xstrdup (xcores);
   char *p = cores.get ();
+  char *saveptr;
 
-  for (p = strtok (p, ","); p;  p = strtok (NULL, ","))
+  for (p = strtok_r (p, ",", &saveptr); p;  p = strtok_r (NULL, ",", &saveptr))
     uiout->field_string (NULL, p);
 }
 
index 67f9f3a42515497232913f420ebc88e3b7892611..84357e29559ee1577f8573c3cfabdade074f42fc 100644 (file)
@@ -566,11 +566,12 @@ linux_xfer_osdata_cpus (struct buffer *buffer)
              char *key, *value;
              int i = 0;
 
-             key = strtok (buf, ":");
+             char *saveptr;
+             key = strtok_r (buf, ":", &saveptr);
              if (key == NULL)
                continue;
 
-             value = strtok (NULL, ":");
+             value = strtok_r (NULL, ":", &saveptr);
              if (value == NULL)
                continue;
 
@@ -1216,36 +1217,36 @@ linux_xfer_osdata_modules (struct buffer *buffer)
        {
          if (fgets (buf, sizeof (buf), fp.get ()))
            {
-             char *name, *dependencies, *status, *tmp;
+             char *name, *dependencies, *status, *tmp, *saveptr;
              unsigned int size;
              unsigned long long address;
              int uses;
 
-             name = strtok (buf, " ");
+             name = strtok_r (buf, " ", &saveptr);
              if (name == NULL)
                continue;
 
-             tmp = strtok (NULL, " ");
+             tmp = strtok_r (NULL, " ", &saveptr);
              if (tmp == NULL)
                continue;
              if (sscanf (tmp, "%u", &size) != 1)
                continue;
 
-             tmp = strtok (NULL, " ");
+             tmp = strtok_r (NULL, " ", &saveptr);
              if (tmp == NULL)
                continue;
              if (sscanf (tmp, "%d", &uses) != 1)
                continue;
 
-             dependencies = strtok (NULL, " ");
+             dependencies = strtok_r (NULL, " ", &saveptr);
              if (dependencies == NULL)
                continue;
 
-             status = strtok (NULL, " ");
+             status = strtok_r (NULL, " ", &saveptr);
              if (status == NULL)
                continue;
 
-             tmp = strtok (NULL, "\n");
+             tmp = strtok_r (NULL, "\n", &saveptr);
              if (tmp == NULL)
                continue;
              if (sscanf (tmp, "%llx", &address) != 1)
index 8ea52d355ae924a5fa39a871bb7974db345c2dff..1ac9013408dbc57f6b61bf2aef7d1768905f5196 100644 (file)
@@ -5169,7 +5169,8 @@ register_remote_support_xml (const char *xml)
   else
     {
       char *copy = xstrdup (remote_support_xml + 13);
-      char *p = strtok (copy, ",");
+      char *saveptr;
+      char *p = strtok_r (copy, ",", &saveptr);
 
       do
        {
@@ -5180,7 +5181,7 @@ register_remote_support_xml (const char *xml)
              return;
            }
        }
-      while ((p = strtok (NULL, ",")) != NULL);
+      while ((p = strtok_r (NULL, ",", &saveptr)) != NULL);
       xfree (copy);
 
       remote_support_xml = reconcat (remote_support_xml,
index 873fbaa49b73601c06a3a7faf508f441cc2d2993..fc73b23145de5809a11e71f2c82a263606f67cd4 100644 (file)
@@ -316,8 +316,10 @@ adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt)
   if (data)
     {
       adi_stat_t adi_stat = get_adi_info (pid);
-      char *line;
-      for (line = strtok (data.get (), "\n"); line; line = strtok (NULL, "\n"))
+      char *saveptr;
+      for (char *line = strtok_r (data.get (), "\n", &saveptr);
+          line;
+          line = strtok_r (NULL, "\n", &saveptr))
         {
           ULONGEST addr, endaddr;
 
index dc988dfae8c131e2cf85c1d27c48ad4aa5bff388..3830faa43652fdce9e09efeaa2c0629ab7c8d46e 100644 (file)
@@ -221,9 +221,10 @@ syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
   /*  Add syscall to its groups.  */
   if (groups != NULL)
     {
-      for (char *group = strtok (groups, ",");
+      char *saveptr;
+      for (char *group = strtok_r (groups, ",", &saveptr);
           group != NULL;
-          group = strtok (NULL, ","))
+          group = strtok_r (NULL, ",", &saveptr))
        syscall_group_add_syscall (syscalls_info, sysdesc, group);
     }
 }