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.
pull/1487/head
Victor Julien 10 years ago
parent fb479902e4
commit bc2b53f10b

@ -3,7 +3,7 @@ identifier i;
position p1; 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@ @script:python@
p1 << banned.p1; p1 << banned.p1;

@ -205,8 +205,9 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
uint32_t abs_off = 0; uint32_t abs_off = 0;
int32_t rel_off = 0; int32_t rel_off = 0;
uint8_t flags = 0; uint8_t flags = 0;
char *saveptr = NULL;
tok = strtok(asn1str, ASN_DELIM); tok = strtok_r(asn1str, ASN_DELIM, &saveptr);
if (tok == NULL) { if (tok == NULL) {
SCLogError(SC_ERR_INVALID_VALUE, "Malformed asn1 argument: %s", SCLogError(SC_ERR_INVALID_VALUE, "Malformed asn1 argument: %s",
asn1str); asn1str);
@ -223,7 +224,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
} else if (strcasecmp("oversize_length", tok) == 0) { } else if (strcasecmp("oversize_length", tok) == 0) {
flags |= ASN1_OVERSIZE_LEN; flags |= ASN1_OVERSIZE_LEN;
/* get the param */ /* get the param */
tok = strtok(NULL, ASN_DELIM); tok = strtok_r(NULL, ASN_DELIM, &saveptr);
if ( tok == NULL || if ( tok == NULL ||
ByteExtractStringUint32(&ov_len, 10, 0, tok) <= 0) ByteExtractStringUint32(&ov_len, 10, 0, tok) <= 0)
{ {
@ -234,7 +235,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
} else if (strcasecmp("absolute_offset", tok) == 0) { } else if (strcasecmp("absolute_offset", tok) == 0) {
flags |= ASN1_ABSOLUTE_OFFSET; flags |= ASN1_ABSOLUTE_OFFSET;
/* get the param */ /* get the param */
tok = strtok(NULL, ASN_DELIM); tok = strtok_r(NULL, ASN_DELIM, &saveptr);
if (tok == NULL || if (tok == NULL ||
ByteExtractStringUint32(&abs_off, 10, 0, tok) <= 0) ByteExtractStringUint32(&abs_off, 10, 0, tok) <= 0)
{ {
@ -245,7 +246,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
} else if (strcasecmp("relative_offset",tok) == 0) { } else if (strcasecmp("relative_offset",tok) == 0) {
flags |= ASN1_RELATIVE_OFFSET; flags |= ASN1_RELATIVE_OFFSET;
/* get the param */ /* get the param */
tok = strtok(NULL, ASN_DELIM); tok = strtok_r(NULL, ASN_DELIM, &saveptr);
if (tok == NULL || if (tok == NULL ||
ByteExtractStringInt32(&rel_off, 10, 0, tok) <= 0) ByteExtractStringInt32(&rel_off, 10, 0, tok) <= 0)
{ {
@ -258,7 +259,7 @@ DetectAsn1Data *DetectAsn1Parse(char *asn1str)
asn1str); asn1str);
return NULL; return NULL;
} }
tok = strtok(NULL, ASN_DELIM); tok = strtok_r(NULL, ASN_DELIM, &saveptr);
} }
fd = SCMalloc(sizeof(DetectAsn1Data)); fd = SCMalloc(sizeof(DetectAsn1Data));

@ -121,14 +121,17 @@ DetectThresholdData *DetectDetectionFilterParse (char *rawstr)
int seconds_pos = 0, count_pos = 0; int seconds_pos = 0, count_pos = 0;
uint16_t pos = 0; uint16_t pos = 0;
int i = 0; int i = 0;
char *saveptr = NULL;
copy_str = SCStrdup(rawstr); copy_str = SCStrdup(rawstr);
if (unlikely(copy_str == NULL)) { if (unlikely(copy_str == NULL)) {
goto error; 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")) if(strstr(df_opt,"count"))
count_found++; count_found++;
if(strstr(df_opt,"second")) if(strstr(df_opt,"second"))

@ -142,8 +142,11 @@ static DetectThresholdData *DetectThresholdParse(char *rawstr)
goto error; 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")) if(strstr(threshold_opt,"count"))
count_found++; count_found++;
if(strstr(threshold_opt,"second")) if(strstr(threshold_opt,"second"))

@ -43,27 +43,17 @@ void SCProtoNameInit()
FILE *fp = fopen(PROTO_FILE,"r"); FILE *fp = fopen(PROTO_FILE,"r");
if (fp != NULL) { if (fp != NULL) {
char line[200]; char line[200];
#if !defined(__WIN32) && !defined(_WIN32)
char *ptr = NULL; char *ptr = NULL;
#endif /* __WIN32 */
while(fgets(line, sizeof(line), fp) != NULL) { while(fgets(line, sizeof(line), fp) != NULL) {
if (line[0] == '#') if (line[0] == '#')
continue; continue;
#if defined(__WIN32) || defined(_WIN32)
char *name = strtok(line," \t");
#else
char *name = strtok_r(line," \t", &ptr); char *name = strtok_r(line," \t", &ptr);
#endif /* __WIN32 */
if (name == NULL) if (name == NULL)
continue; continue;
#if defined(__WIN32) || defined(_WIN32)
char *proto_ch = strtok(NULL," \t");
#else
char *proto_ch = strtok_r(NULL," \t", &ptr); char *proto_ch = strtok_r(NULL," \t", &ptr);
#endif /* __WIN32 */
if (proto_ch == NULL) if (proto_ch == NULL)
continue; continue;
@ -71,11 +61,7 @@ void SCProtoNameInit()
if (proto >= 255) if (proto >= 255)
continue; continue;
#if defined(__WIN32) || defined(_WIN32)
char *cname = strtok(NULL, " \t");
#else
char *cname = strtok_r(NULL, " \t", &ptr); char *cname = strtok_r(NULL, " \t", &ptr);
#endif /* __WIN32 */
if (known_proto[proto] != NULL) { if (known_proto[proto] != NULL) {
SCFree(known_proto[proto]); SCFree(known_proto[proto]);

Loading…
Cancel
Save