From eaff01a57f3c441846948fd8b2a62ba5b9f1442b Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 4 Dec 2013 10:01:54 -0600 Subject: [PATCH] Use the stack for temporary memory buffers. --- src/conf.c | 33 +++++++++++++++------------------ src/util-error.c | 1 + src/util-error.h | 1 + 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/conf.c b/src/conf.c index d747c0d5d2..6fd6805ea8 100644 --- a/src/conf.c +++ b/src/conf.c @@ -42,6 +42,9 @@ #include "util-debug.h" #include "util-path.h" +/** Maximum size of a complete domain name. */ +#define NODE_NAME_MAX 1024 + static ConfNode *root = NULL; static ConfNode *root_backup = NULL; @@ -63,17 +66,17 @@ ConfGetNodeOrCreate(char *name, int final) { ConfNode *parent = root; ConfNode *node = NULL; - char *tmpname; + char node_name[NODE_NAME_MAX]; char *key; char *next; - tmpname = SCStrdup(name); - if (unlikely(tmpname == NULL)) { - SCLogWarning(SC_ERR_MEM_ALLOC, - "Failed to allocate memory for configuration."); - goto end; + if (strlcpy(node_name, name, sizeof(node_name)) >= sizeof(node_name)) { + SCLogError(SC_ERR_CONF_NAME_TOO_LONG, + "Configuration name too long: %s", name); + return NULL; } - key = tmpname; + + key = node_name; do { if ((next = strchr(key, '.')) != NULL) @@ -95,9 +98,6 @@ ConfGetNodeOrCreate(char *name, int final) } while (next != NULL); end: - if (tmpname != NULL) - SCFree(tmpname); - return node; } @@ -174,18 +174,17 @@ ConfNode * ConfGetNode(char *name) { ConfNode *node = root; - char *tmpname; + char node_name[NODE_NAME_MAX]; char *key; char *next; - tmpname = SCStrdup(name); - if (unlikely(tmpname == NULL)) { - SCLogWarning(SC_ERR_MEM_ALLOC, - "Failed to allocate temp. memory while getting config node."); + if (strlcpy(node_name, name, sizeof(node_name)) >= sizeof(node_name)) { + SCLogError(SC_ERR_CONF_NAME_TOO_LONG, + "Configuration name too long: %s", name); return NULL; } - key = tmpname; + key = node_name; do { if ((next = strchr(key, '.')) != NULL) *next++ = '\0'; @@ -193,8 +192,6 @@ ConfGetNode(char *name) key = next; } while (next != NULL && node != NULL); - SCFree(tmpname); - return node; } diff --git a/src/util-error.c b/src/util-error.c index 95e9a06e3b..86575dc827 100644 --- a/src/util-error.c +++ b/src/util-error.c @@ -281,6 +281,7 @@ const char * SCErrorToString(SCError err) CASE_CODE (SC_ERR_THRESHOLD_SETUP); CASE_CODE (SC_ERR_DNS_CONFIG); CASE_CODE (SC_ERR_CONF_YAML_ERROR); + CASE_CODE (SC_ERR_CONF_NAME_TOO_LONG); } return "UNKNOWN_ERROR"; diff --git a/src/util-error.h b/src/util-error.h index 2bb93380c7..1f5ac34fd0 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -270,6 +270,7 @@ typedef enum { SC_ERR_THRESHOLD_SETUP, SC_ERR_DNS_CONFIG, SC_ERR_CONF_YAML_ERROR, + SC_ERR_CONF_NAME_TOO_LONG, } SCError; const char *SCErrorToString(SCError); -- 2.47.2