Fixed usages of strtok to use strtok_r instead
authorErich Keane <erich.keane@intel.com>
Thu, 11 Dec 2014 19:37:33 +0000 (11:37 -0800)
committerErich Keane <erich.keane@intel.com>
Thu, 11 Dec 2014 19:37:33 +0000 (11:37 -0800)
strtok is not thread safe and has a global state variable that
have serious side effects.  If any consumer of ours was to use
threads and strtok they could very easily corrupt the state on us
(or us on them!).  This fix replaces it with the non-side effect
POSIX version, which is strtok_r.  Note that in occollection.c
we are already using strtok_r.

Change-Id: Ie39a2a2ab35a0b1f13a82542ef6963662ac5c53e
Signed-off-by: Erich Keane <erich.keane@intel.com>
resource/csdk/occoap/src/occoap.c
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocstack.c

index cc9c95a..5c2e7ed 100644 (file)
@@ -22,6 +22,8 @@
 //=============================================================================
 // Includes
 //=============================================================================
+#define _POSIX_C_SOURCE 200112L
+#include <string.h>
 #include "occoap.h"
 #include "ocstackconfig.h"
 #include "occlientcb.h"
@@ -339,20 +341,22 @@ static void HandleCoAPResponses(struct coap_context_t *ctx,
 
     #ifdef WITH_PRESENCE
     if(!strcmp((char *)rcvdUri, (char *)OC_PRESENCE_URI)){
+        char* tokSavePtr;
+
         isPresenceNotification = 1;
         OC_LOG(INFO, TAG, PCF("Received a presence notification"));
-        tok = strtok((char *)bufRes, "[:]}");
+        tok = strtok_r((char *)bufRes, "[:]}", &tokSavePtr);
         bufRes[strlen((char *)bufRes)] = ':';
-        tok = strtok(NULL, "[:]}");
+        tok = strtok_r(NULL, "[:]}", &tokSavePtr);
         bufRes[strlen((char *)bufRes)] = ':';
         VERIFY_NON_NULL(tok);
         sequenceNumber = (uint32_t )atol(tok);
         OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", sequenceNumber);
-        tok = strtok(NULL, "[:]}");
+        tok = strtok_r(NULL, "[:]}", &tokSavePtr);
         VERIFY_NON_NULL(tok);
         maxAge = (uint32_t )atol(tok);
         OC_LOG_V(DEBUG, TAG, "The received TTL is %u", maxAge);
-        tok = strtok(NULL, "[:]}");
+        tok = strtok_r(NULL, "[:]}", &tokSavePtr);
         if(tok) {
             bufRes[strlen((char *)bufRes)] = ':';
             resourceTypeName = (char *)OCMalloc(strlen(tok));
index 01abbc1..a2a1c4e 100644 (file)
@@ -18,6 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+#define _POSIX_C_SOURCE 200112L
 #include <string.h>
 #include "ocstack.h"
 #include "ocstackconfig.h"
@@ -73,8 +74,9 @@ static OCStackResult ValidateUrlQuery (unsigned char *url, unsigned char *query,
     if (strcmp ((char *)url, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0) {
         *filterOn = STACK_RES_DISCOVERY_NOFILTER;
         if (query && *query) {
-            filterParam = strtok ((char *)query, "=");
-            *filterValue = strtok (NULL, " ");
+            char* strTokPtr;
+            filterParam = strtok_r((char *)query, "=", &strTokPtr);
+            *filterValue = strtok_r(NULL, " ", &strTokPtr);
             if (!(*filterValue)) {
                 return OC_STACK_INVALID_QUERY;
             } else if (strcmp (filterParam, OC_RSRVD_INTERFACE) == 0) {
index eb0aef8..d0c5ee9 100644 (file)
@@ -22,6 +22,9 @@
 //-----------------------------------------------------------------------------
 // Includes
 //-----------------------------------------------------------------------------
+#define _POSIX_C_SOURCE 200112L
+#include <string.h>
+
 #include "ocstack.h"
 #include "ocstackinternal.h"
 #include "ocresourcehandler.h"
@@ -2353,7 +2356,8 @@ OCStackResult getResourceType(const char * uri, unsigned char** resourceType, ch
         goto exit;
     }
     strcpy(tempURI, uri);
-    leftToken = strtok((char *)tempURI, "?");
+    char* strTokPtr;
+    leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
 
     while(leftToken != NULL)
     {
@@ -2367,7 +2371,7 @@ OCStackResult getResourceType(const char * uri, unsigned char** resourceType, ch
             strcpy((char *)*resourceType, ((const char *)&leftToken[3]));
             break;
         }
-        leftToken = strtok(NULL, "?");
+        leftToken = strtok_r(NULL, "?", &strTokPtr);
     }
 
     *newURI = tempURI;