Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use strtok_r() instead of non reentrant strtok().
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 15 Sep 2017 13:06:02 +0000 (15:06 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 15 Sep 2017 13:12:07 +0000 (15:12 +0200)
contrib/benchmarking_code_block/inject.h
src/xbt/automaton/automatonparse_promela.c
src/xbt/memory_map.cpp

index a79ac28..8d7907d 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2014. The SimGrid Team.
+/* Copyright (c) 2013-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -66,7 +66,8 @@ static inline void xbt_inject_init(char *inputfile)
   if (fgets(line, 200, fpInput) == NULL)
     printf("Error input file is empty!"); // Skipping first row
   while (fgets(line, 200, fpInput) != NULL) {
-    key = strtok(line, "\t");
+    char *saveptr; /* for strtok_r() */
+    key = strtok_r(line, "\t", &saveptr);
 
     xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
     if (data)
@@ -75,16 +76,16 @@ static inline void xbt_inject_init(char *inputfile)
     data = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
 
     data->block_id = key;
-    data->counts   = atoi(strtok(NULL, "\t"));
-    data->mean     = atof(strtok(NULL, "\t"));
-    data->n        = atoi(strtok(NULL, "\t"));
+    data->counts   = atoi(strtok_r(NULL, "\t", &saveptr));
+    data->mean     = atof(strtok_r(NULL, "\t", &saveptr));
+    data->n        = atoi(strtok_r(NULL, "\t", &saveptr));
 
     data->breaks     = (double*)malloc(sizeof(double) * data->n);
     data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
     for (int i        = 0; i < data->n; i++)
-      data->breaks[i] = atof(strtok(NULL, "\t"));
+      data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
     for (int i            = 0; i < (data->n - 1); i++)
-      data->percentage[i] = atof(strtok(NULL, "\t"));
+      data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));
 
     xbt_dict_set(mydict, key, data, NULL);
   }
@@ -118,7 +119,8 @@ static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStre
   }
 
   while (fgets(line, MAX_LINE_INJ, fpInput) != NULL) {
-    key = strtok(line, "\t");
+    char *saveptr; /* for strtok_r() */
+    key = strtok_r(line, "\t", &saveptr);
 
     xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
     if (data)
@@ -126,16 +128,16 @@ static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStre
 
     data             = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
     data->block_id   = key;
-    data->counts     = atoi(strtok(NULL, "\t"));
-    data->mean       = atof(strtok(NULL, "\t"));
-    data->n          = atoi(strtok(NULL, "\t"));
+    data->counts     = atoi(strtok_r(NULL, "\t", &saveptr));
+    data->mean       = atof(strtok_r(NULL, "\t", &saveptr));
+    data->n          = atoi(strtok_r(NULL, "\t", &saveptr));
     data->breaks     = (double*)malloc(sizeof(double) * data->n);
     data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
 
     for (int i        = 0; i < data->n; i++)
-      data->breaks[i] = atof(strtok(NULL, "\t"));
+      data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
     for (int i = 0; i < (data->n - 1); i++) {
-      data->percentage[i] = atof(strtok(NULL, "\t"));
+      data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));
     }
 
     xbt_dict_set(mydict, key, data, NULL);
index dffb34e..1c4b4da 100644 (file)
@@ -17,15 +17,15 @@ static xbt_automaton_t parsed_automaton;
 char* state_id_src;
 
 static void new_state(char* id, int src){
-
+  char* saveptr; // for strtok_r()
   char* id_copy = xbt_strdup(id);
-  char* first_part = strtok(id_copy,"_");
+  char* first_part = strtok_r(id_copy, "_", &saveptr);
   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
 
   if(strcmp(first_part,"accept")==0){
     type = 1;
   }else{
-    char* second_part = strtok(NULL,"_");
+    char* second_part = strtok_r(NULL, "_", &saveptr);
     if(strcmp(second_part,"init")==0){
       type = -1;
     }
index b2185d7..0f77a8d 100644 (file)
@@ -193,12 +193,13 @@ XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
     line[read - 1] = '\0';
 
     /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
+    char* saveptr; // for strtok_r()
     char* lfields[6];
-    lfields[0] = strtok(line, " ");
+    lfields[0] = strtok_r(line, " ", &saveptr);
 
     int i;
     for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
-      lfields[i] = std::strtok(nullptr, " ");
+      lfields[i] = strtok_r(nullptr, " ", &saveptr);
     }
 
     /* Check to see if we got the expected amount of columns */
@@ -207,7 +208,7 @@ XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
 
     /* Ok we are good enough to try to get the info we need */
     /* First get the start and the end address of the map   */
-    char *tok = std::strtok(lfields[0], "-");
+    char* tok = strtok_r(lfields[0], "-", &saveptr);
     if (tok == nullptr)
       xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
 
@@ -218,7 +219,7 @@ XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
     if (*endptr != '\0')
       xbt_abort();
 
-    tok = std::strtok(nullptr, "-");
+    tok = strtok_r(nullptr, "-", &saveptr);
     if (tok == nullptr)
       xbt_abort();
 
@@ -268,7 +269,7 @@ XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
       xbt_abort();
 
     /* Get the device major:minor bytes */
-    tok = std::strtok(lfields[3], ":");
+    tok = strtok_r(lfields[3], ":", &saveptr);
     if (tok == nullptr)
       xbt_abort();
 
@@ -277,7 +278,7 @@ XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
     if (*endptr != '\0')
       xbt_abort();
 
-    tok = std::strtok(nullptr, ":");
+    tok = strtok_r(nullptr, ":", &saveptr);
     if (tok == nullptr)
       xbt_abort();