From: Victor Julien Date: Wed, 25 Sep 2019 20:23:51 +0000 (+0200) Subject: posix: remove deprecated index/rindex calls X-Git-Tag: suricata-5.0.0~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2da90a1cd82eb73f492034b1d67156d27f83bf21;p=thirdparty%2Fsuricata.git posix: remove deprecated index/rindex calls Replace index by strchr and rindex by strrchr. index(3) states "POSIX.1-2008 removes the specifications of index() and rindex(), recommending strchr(3) and strrchr(3) instead." Add index/rindex to banned function check so they don't get reintroduced. Bug #1443. --- diff --git a/configure.ac b/configure.ac index 48e026817a..b219eaefaa 100644 --- a/configure.ac +++ b/configure.ac @@ -221,7 +221,7 @@ # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC - AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strndup strerror strncasecmp strtol strtoul memchr memrchr clock_gettime]) + AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strrchr strdup strndup strerror strncasecmp strtol strtoul memchr memrchr clock_gettime]) AC_CHECK_FUNCS([strptime]) AC_CHECK_DECL([getrandom], diff --git a/qa/coccinelle/banned-functions.cocci b/qa/coccinelle/banned-functions.cocci index e011cb3bf2..d64aa728a4 100644 --- a/qa/coccinelle/banned-functions.cocci +++ b/qa/coccinelle/banned-functions.cocci @@ -3,7 +3,7 @@ identifier i; position p1; @@ -\(strtok@i\|sprintf@i\|strcat@i\|strcpy@i\|strncpy@i\|strncat@i\|strndup@i\|strchrnul@i\|rand@i\|rand_r@i\|memmem@i\)(...)@p1 +\(strtok@i\|sprintf@i\|strcat@i\|strcpy@i\|strncpy@i\|strncat@i\|strndup@i\|strchrnul@i\|rand@i\|rand_r@i\|memmem@i\|index@i\|rindex@i\)(...)@p1 @script:python@ p1 << banned.p1; diff --git a/src/decode.c b/src/decode.c index 4f32c41067..a0ad4a6ce9 100644 --- a/src/decode.c +++ b/src/decode.c @@ -547,7 +547,7 @@ void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv) } char name[256]; - char *dot = index(DEvents[i].event_name, '.'); + char *dot = strchr(DEvents[i].event_name, '.'); BUG_ON(!dot); snprintf(name, sizeof(name), "%s.%s", stats_decoder_events_prefix, dot+1); diff --git a/src/detect-dce-opnum.c b/src/detect-dce-opnum.c index 8d841d6e36..93c83e98db 100644 --- a/src/detect-dce-opnum.c +++ b/src/detect-dce-opnum.c @@ -157,7 +157,7 @@ static DetectDceOpnumData *DetectDceOpnumArgParse(const char *arg) * once we are done using it */ dup_str_head = dup_str; dup_str_temp = dup_str; - while ( (comma_token = index(dup_str, ',')) != NULL) { + while ( (comma_token = strchr(dup_str, ',')) != NULL) { comma_token[0] = '\0'; dup_str = comma_token + 1; @@ -172,7 +172,7 @@ static DetectDceOpnumData *DetectDceOpnumArgParse(const char *arg) prev_dor = dor; } - if ((hyphen_token = index(dup_str_temp, '-')) != NULL) { + if ((hyphen_token = strchr(dup_str_temp, '-')) != NULL) { hyphen_token[0] = '\0'; hyphen_token++; dor->range1 = atoi(dup_str_temp); @@ -200,7 +200,7 @@ static DetectDceOpnumData *DetectDceOpnumArgParse(const char *arg) prev_dor->next = dor; } - if ( (hyphen_token = index(dup_str, '-')) != NULL) { + if ( (hyphen_token = strchr(dup_str, '-')) != NULL) { hyphen_token[0] = '\0'; hyphen_token++; dor->range1 = atoi(dup_str); diff --git a/src/output-json-stats.c b/src/output-json-stats.c index c6e7e3844e..b22b8df7cf 100644 --- a/src/output-json-stats.c +++ b/src/output-json-stats.c @@ -171,12 +171,12 @@ static json_t *OutputStats2Json(json_t *js, const char *key) { void *iter; - const char *dot = index(key, '.'); + const char *dot = strchr(key, '.'); if (dot == NULL) return NULL; if (strlen(dot) > 2) { if (*(dot + 1) == '.' && *(dot + 2) != '\0') - dot = index(dot + 2, '.'); + dot = strchr(dot + 2, '.'); } size_t predot_len = (dot - key) + 1; @@ -184,7 +184,7 @@ static json_t *OutputStats2Json(json_t *js, const char *key) strlcpy(s, key, predot_len); iter = json_object_iter_at(js, s); - const char *s2 = index(dot+1, '.'); + const char *s2 = strchr(dot+1, '.'); json_t *value = json_object_iter_value(iter); if (value == NULL) { @@ -232,8 +232,8 @@ json_t *StatsToJSON(const StatsTable *st, uint8_t flags) continue; const char *name = st->stats[u].name; const char *shortname = name; - if (rindex(name, '.') != NULL) { - shortname = &name[rindex(name, '.') - name + 1]; + if (strrchr(name, '.') != NULL) { + shortname = &name[strrchr(name, '.') - name + 1]; } json_t *js_type = OutputStats2Json(js_stats, name); if (js_type != NULL) { @@ -270,7 +270,7 @@ json_t *StatsToJSON(const StatsTable *st, uint8_t flags) char str[256]; snprintf(str, sizeof(str), "%s.%s", st->tstats[u].tm_name, st->tstats[u].name); - char *shortname = &str[rindex(str, '.') - str + 1]; + char *shortname = &str[strrchr(str, '.') - str + 1]; json_t *js_type = OutputStats2Json(threads, str); if (js_type != NULL) { diff --git a/src/util-affinity.c b/src/util-affinity.c index d78c509fbb..c0216c5c80 100644 --- a/src/util-affinity.c +++ b/src/util-affinity.c @@ -109,8 +109,8 @@ void BuildCpusetWithCallback(const char *name, ConfNode *node, a = 0; b = max; stop = 1; - } else if (index(lnode->val, '-') != NULL) { - char *sep = index(lnode->val, '-'); + } else if (strchr(lnode->val, '-') != NULL) { + char *sep = strchr(lnode->val, '-'); char *end; a = strtoul(lnode->val, &end, 10); if (end != sep) { diff --git a/src/util-debug.c b/src/util-debug.c index 678b0e0050..608580f505 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -324,7 +324,7 @@ static SCError SCLogMessageGetBuffer( char *temp_fmt = local_format; char *substr = temp_fmt; - while ( (temp_fmt = index(temp_fmt, SC_LOG_FMT_PREFIX)) ) { + while ( (temp_fmt = strchr(temp_fmt, SC_LOG_FMT_PREFIX)) ) { if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { return SC_OK; } diff --git a/src/util-host-os-info.c b/src/util-host-os-info.c index e4ab989cd6..d22443abb5 100644 --- a/src/util-host-os-info.c +++ b/src/util-host-os-info.c @@ -161,19 +161,19 @@ int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int } /* check if we have more addresses in the host_os_ip_range */ - if ((ip_str_rem = index(ip_str, ',')) != NULL) { + if ((ip_str_rem = strchr(ip_str, ',')) != NULL) { ip_str_rem[0] = '\0'; ip_str_rem++; recursive = TRUE; } /* check if we have received a netblock */ - if ( (netmask_str = index(ip_str, '/')) != NULL) { + if ( (netmask_str = strchr(ip_str, '/')) != NULL) { netmask_str[0] = '\0'; netmask_str++; } - if (index(ip_str, ':') == NULL) { + if (strchr(ip_str, ':') == NULL) { /* if we are here, we have an IPV4 address */ if ( (ipv4_addr = ValidateIPV4Address(ip_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address"); @@ -252,10 +252,10 @@ int SCHInfoGetHostOSFlavour(const char *ip_addr_str) struct in6_addr *ipv6_addr = NULL; void *user_data = NULL; - if (ip_addr_str == NULL || index(ip_addr_str, '/') != NULL) + if (ip_addr_str == NULL || strchr(ip_addr_str, '/') != NULL) return -1; - if (index(ip_addr_str, ':') != NULL) { + if (strchr(ip_addr_str, ':') != NULL) { if ( (ipv6_addr = ValidateIPV6Address(ip_addr_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address"); return -1; @@ -344,7 +344,7 @@ void SCHInfoLoadFromConfig(void) ConfNode *host; TAILQ_FOREACH(host, &policy->head, next) { int is_ipv4 = 1; - if (host->val != NULL && index(host->val, ':') != NULL) + if (host->val != NULL && strchr(host->val, ':') != NULL) is_ipv4 = 0; if (SCHInfoAddHostOSInfo(policy->name, host->val, is_ipv4) == -1) { SCLogError(SC_ERR_INVALID_ARGUMENT, diff --git a/src/win32-misc.h b/src/win32-misc.h index 24bb42e31d..5f2e262aa3 100644 --- a/src/win32-misc.h +++ b/src/win32-misc.h @@ -26,9 +26,6 @@ #include -#define index strchr -#define rindex strrchr - #define bzero(s, n) memset(s, 0, n) #ifndef O_NOFOLLOW