]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
parsing: s/strtok/strtok_r/g
authorVictor Julien <victor@inliniac.net>
Wed, 13 May 2015 12:25:49 +0000 (14:25 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 14 May 2015 12:27:02 +0000 (14:27 +0200)
Remove all strtok uses and replace them by strtok_r.

Do the same for Windows builds. Cygwin builds fine with strtok_r.

Add strtok to banned function list.

qa/coccinelle/banned-functions.cocci
src/detect-asn1.c
src/detect-detection-filter.c
src/detect-threshold.c
src/util-proto-name.c

index bc2c60e6cbae496236d65062d00dbdf9040e8726..5913521cb38f1615a25a85d0b6c5d31df59d1bc3 100644 (file)
@@ -3,7 +3,7 @@ identifier i;
 position p1;
 @@
 
-\(sprintf@i\|strcat@i\|strcpy@i\|strncpy@i\|strncat@i\|strndup@i\|strchrdup@i\)(...)@p1
+\(strtok@i\|sprintf@i\|strcat@i\|strcpy@i\|strncpy@i\|strncat@i\|strndup@i\|strchrdup@i\)(...)@p1
 
 @script:python@
 p1 << banned.p1;
index 8b7ff06f393b55fd1a1a8c592c863bb840680178..69d90829e27cbb3231574d339ee95e6f41f872e1 100644 (file)
@@ -205,8 +205,9 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
     uint32_t abs_off = 0;
     int32_t rel_off = 0;
     uint8_t flags = 0;
+    char *saveptr = NULL;
 
-    tok = strtok(asn1str, ASN_DELIM);
+    tok = strtok_r(asn1str, ASN_DELIM, &saveptr);
     if (tok == NULL) {
         SCLogError(SC_ERR_INVALID_VALUE, "Malformed asn1 argument: %s",
                    asn1str);
@@ -223,7 +224,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
         } else if (strcasecmp("oversize_length", tok) == 0) {
             flags |= ASN1_OVERSIZE_LEN;
             /* get the param */
-            tok = strtok(NULL, ASN_DELIM);
+            tok = strtok_r(NULL, ASN_DELIM, &saveptr);
             if ( tok == NULL ||
                 ByteExtractStringUint32(&ov_len, 10, 0, tok) <= 0)
             {
@@ -234,7 +235,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
         } else if (strcasecmp("absolute_offset", tok) == 0) {
             flags |= ASN1_ABSOLUTE_OFFSET;
             /* get the param */
-            tok = strtok(NULL, ASN_DELIM);
+            tok = strtok_r(NULL, ASN_DELIM, &saveptr);
             if (tok == NULL ||
                 ByteExtractStringUint32(&abs_off, 10, 0, tok) <= 0)
             {
@@ -245,7 +246,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
         } else if (strcasecmp("relative_offset",tok) == 0) {
             flags |= ASN1_RELATIVE_OFFSET;
             /* get the param */
-            tok = strtok(NULL, ASN_DELIM);
+            tok = strtok_r(NULL, ASN_DELIM, &saveptr);
             if (tok == NULL ||
                 ByteExtractStringInt32(&rel_off, 10, 0, tok) <= 0)
             {
@@ -258,7 +259,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
                        asn1str);
             return NULL;
         }
-        tok = strtok(NULL, ASN_DELIM);
+        tok = strtok_r(NULL, ASN_DELIM, &saveptr);
     }
 
     fd = SCMalloc(sizeof(DetectAsn1Data));
index b16178cb6498888aa79f3d2c279f2c371575f2f9..7e1ee8bc9b8e4b9607a18c7eaf0b3a3604dba1de 100644 (file)
@@ -121,14 +121,17 @@ DetectThresholdData *DetectDetectionFilterParse (char *rawstr)
     int seconds_pos = 0, count_pos = 0;
     uint16_t pos = 0;
     int i = 0;
+    char *saveptr = NULL;
 
     copy_str = SCStrdup(rawstr);
     if (unlikely(copy_str == NULL)) {
         goto error;
     }
 
-    for(pos = 0, df_opt = strtok(copy_str,",");  pos < strlen(copy_str) &&  df_opt != NULL;  pos++, df_opt = strtok(NULL,",")) {
-
+    for (pos = 0, df_opt = strtok_r(copy_str,",", &saveptr);
+         pos < strlen(copy_str) && df_opt != NULL;
+         pos++, df_opt = strtok_r(NULL,",", &saveptr))
+    {
         if(strstr(df_opt,"count"))
             count_found++;
         if(strstr(df_opt,"second"))
index 4f47756718ed6cfa24dffdfa4fec28a9c9c63604..5c83ee4d473020ff21e5d7e188ae8d5e8549c94d 100644 (file)
@@ -142,8 +142,11 @@ static DetectThresholdData *DetectThresholdParse(char *rawstr)
         goto error;
     }
 
-    for(pos = 0, threshold_opt = strtok(copy_str,",");  pos < strlen(copy_str) &&  threshold_opt != NULL;  pos++, threshold_opt = strtok(NULL,",")) {
-
+    char *saveptr = NULL;
+    for (pos = 0, threshold_opt = strtok_r(copy_str,",", &saveptr);
+         pos < strlen(copy_str) && threshold_opt != NULL;
+         pos++, threshold_opt = strtok_r(NULL,"," , &saveptr))
+    {
         if(strstr(threshold_opt,"count"))
             count_found++;
         if(strstr(threshold_opt,"second"))
index 82ec8e64ba6f91c9ddf666f6f09c790b3fc59861..0b958884ca4ef639c9226e25679b3486ac46f56c 100644 (file)
@@ -43,27 +43,17 @@ void SCProtoNameInit()
     FILE *fp = fopen(PROTO_FILE,"r");
     if (fp != NULL) {
         char line[200];
-#if !defined(__WIN32) && !defined(_WIN32)
         char *ptr = NULL;
-#endif /* __WIN32 */
 
         while(fgets(line, sizeof(line), fp) != NULL) {
             if (line[0] == '#')
                 continue;
 
-#if defined(__WIN32) || defined(_WIN32)
-            char *name = strtok(line," \t");
-#else
             char *name = strtok_r(line," \t", &ptr);
-#endif /* __WIN32 */
             if (name == NULL)
                 continue;
 
-#if defined(__WIN32) || defined(_WIN32)
-            char *proto_ch = strtok(NULL," \t");
-#else
             char *proto_ch = strtok_r(NULL," \t", &ptr);
-#endif /* __WIN32 */
             if (proto_ch == NULL)
                 continue;
 
@@ -71,11 +61,7 @@ void SCProtoNameInit()
             if (proto >= 255)
                 continue;
 
-#if defined(__WIN32) || defined(_WIN32)
-            char *cname = strtok(NULL, " \t");
-#else
             char *cname = strtok_r(NULL, " \t", &ptr);
-#endif /* __WIN32 */
 
             if (known_proto[proto] != NULL) {
                 SCFree(known_proto[proto]);