Browse Source

* use strl*() instead snprintf() in filters

master
Alex 'AdUser' Z 8 years ago
parent
commit
a9c75e9cd1
  1. 4
      src/filters/CMakeLists.txt
  2. 2
      src/filters/filter.h
  3. 8
      src/filters/pcre.c
  4. 8
      src/filters/preg.c
  5. 4
      t/CMakeLists.txt

4
src/filters/CMakeLists.txt

@ -1,12 +1,12 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(FILTERS "") set(FILTERS "")
add_library("f2b_filter_preg" MODULE "preg.c") add_library("f2b_filter_preg" MODULE "preg.c" "../strlcpy.c")
list(APPEND FILTERS "f2b_filter_preg") list(APPEND FILTERS "f2b_filter_preg")
find_library(PCRE_FOUND "pcre") find_library(PCRE_FOUND "pcre")
if (WITH_PCRE AND PCRE_FOUND) if (WITH_PCRE AND PCRE_FOUND)
add_library("f2b_filter_pcre" MODULE "pcre.c") add_library("f2b_filter_pcre" MODULE "pcre.c" "../strlcpy.c")
target_link_libraries("f2b_filter_pcre" "pcre") target_link_libraries("f2b_filter_pcre" "pcre")
list(APPEND FILTERS "f2b_filter_pcre") list(APPEND FILTERS "f2b_filter_pcre")
endif () endif ()

2
src/filters/filter.h

@ -13,6 +13,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../strlcpy.h"
#define ID_MAX 32 #define ID_MAX 32
#define PATTERN_MAX 256 #define PATTERN_MAX 256
#define HOST_TOKEN "<HOST>" #define HOST_TOKEN "<HOST>"

8
src/filters/pcre.c

@ -34,7 +34,7 @@ create(const char *id) {
if ((cfg = calloc(1, sizeof(cfg_t))) == NULL) if ((cfg = calloc(1, sizeof(cfg_t))) == NULL)
return NULL; return NULL;
snprintf(cfg->id, sizeof(cfg->id), "%s", id); strlcpy(cfg->id, id, sizeof(cfg->id));
return cfg; return cfg;
} }
@ -85,8 +85,8 @@ append(cfg_t *cfg, const char *pattern) {
memset(buf, 0x0, bufsize); memset(buf, 0x0, bufsize);
memcpy(buf, pattern, token - pattern); memcpy(buf, pattern, token - pattern);
strcat(buf, HOST_REGEX); strlcat(buf, HOST_REGEX, bufsize);
strcat(buf, token + strlen(HOST_TOKEN)); strlcat(buf, token + strlen(HOST_TOKEN), bufsize);
if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL) if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL)
return false; return false;
@ -111,7 +111,7 @@ append(cfg_t *cfg, const char *pattern) {
regex->next = cfg->regexps; regex->next = cfg->regexps;
cfg->regexps = regex; cfg->regexps = regex;
snprintf(regex->pattern, sizeof(regex->pattern), "%s", pattern); strlcpy(regex->pattern, pattern, sizeof(regex->pattern));
return true; return true;
} }

8
src/filters/preg.c

@ -32,7 +32,7 @@ create(const char *id) {
if ((cfg = calloc(1, sizeof(cfg_t))) == NULL) if ((cfg = calloc(1, sizeof(cfg_t))) == NULL)
return NULL; return NULL;
snprintf(cfg->id, sizeof(cfg->id), "%s", id); strlcpy(cfg->id, id, sizeof(cfg->id));
return cfg; return cfg;
} }
@ -73,8 +73,8 @@ append(cfg_t *cfg, const char *pattern) {
memset(buf, 0x0, bufsize); memset(buf, 0x0, bufsize);
memcpy(buf, pattern, token - pattern); memcpy(buf, pattern, token - pattern);
strcat(buf, HOST_REGEX); strlcat(buf, HOST_REGEX, bufsize);
strcat(buf, token + strlen(HOST_TOKEN)); strlcat(buf, token + strlen(HOST_TOKEN), bufsize);
if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL) if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL)
return false; return false;
@ -82,7 +82,7 @@ append(cfg_t *cfg, const char *pattern) {
if (regcomp(&regex->regex, buf, flags) == 0) { if (regcomp(&regex->regex, buf, flags) == 0) {
regex->next = cfg->regexps; regex->next = cfg->regexps;
cfg->regexps = regex; cfg->regexps = regex;
snprintf(regex->pattern, sizeof(regex->pattern), "%s", pattern); strlcpy(regex->pattern, pattern, sizeof(regex->pattern));
return true; return true;
} }

4
t/CMakeLists.txt

@ -14,11 +14,11 @@ add_test("tests/f2b_matches_*" "t_matches")
add_test("tests/f2b_ipaddr_*" "t_ipaddr") add_test("tests/f2b_ipaddr_*" "t_ipaddr")
add_test("tests/f2b_config_param*" "t_config_param") add_test("tests/f2b_config_param*" "t_config_param")
add_executable("t_filter_preg" "t_filters.c" "${SRC_DIR}/filters/preg.c") add_executable("t_filter_preg" "t_filters.c" "${SRC_DIR}/filters/preg.c" "${SRC_DIR}/strlcpy.c")
add_test("tests/filter/preg" "t_filter_preg") add_test("tests/filter/preg" "t_filter_preg")
if (WITH_PCRE) if (WITH_PCRE)
add_test("tests/filter/pcre" "t_filter_pcre") add_test("tests/filter/pcre" "t_filter_pcre")
add_executable("t_filter_pcre" "t_filters.c" "${SRC_DIR}/filters/pcre.c") add_executable("t_filter_pcre" "t_filters.c" "${SRC_DIR}/filters/pcre.c" "${SRC_DIR}/strlcpy.c")
target_link_libraries("t_filter_pcre" "pcre") target_link_libraries("t_filter_pcre" "pcre")
endif () endif ()

Loading…
Cancel
Save