From bc2b53f10bb681f5a7ed501ca316aeedb3c30527 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 13 May 2015 14:25:49 +0200 Subject: [PATCH] parsing: s/strtok/strtok_r/g 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 | 2 +- src/detect-asn1.c | 11 ++++++----- src/detect-detection-filter.c | 7 +++++-- src/detect-threshold.c | 7 +++++-- src/util-proto-name.c | 14 -------------- 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/qa/coccinelle/banned-functions.cocci b/qa/coccinelle/banned-functions.cocci index bc2c60e6cb..5913521cb3 100644 --- a/qa/coccinelle/banned-functions.cocci +++ b/qa/coccinelle/banned-functions.cocci @@ -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; diff --git a/src/detect-asn1.c b/src/detect-asn1.c index 8b7ff06f39..69d90829e2 100644 --- a/src/detect-asn1.c +++ b/src/detect-asn1.c @@ -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)); diff --git a/src/detect-detection-filter.c b/src/detect-detection-filter.c index b16178cb64..7e1ee8bc9b 100644 --- a/src/detect-detection-filter.c +++ b/src/detect-detection-filter.c @@ -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")) diff --git a/src/detect-threshold.c b/src/detect-threshold.c index 4f47756718..5c83ee4d47 100644 --- a/src/detect-threshold.c +++ b/src/detect-threshold.c @@ -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")) diff --git a/src/util-proto-name.c b/src/util-proto-name.c index 82ec8e64ba..0b958884ca 100644 --- a/src/util-proto-name.c +++ b/src/util-proto-name.c @@ -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]);