Skip to content

Commit

Permalink
Better memory management for regex in TS32.423 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon authored and AndersBroman committed Sep 28, 2023
1 parent b7acb29 commit 5c7d037
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions wiretap/nettrace_3gpp_32_423.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static const guchar c_sai_req[] = "gsm_map.v3.arg.opcode";
static const guchar c_sai_rsp[] = "gsm_map.v3.res.opcode";
static const guchar c_nas_eps[] = "nas-eps_plain";


#define RINGBUFFER_START_SIZE G_MAXINT
#define RINGBUFFER_CHUNK_SIZE 1024

Expand Down Expand Up @@ -125,6 +126,20 @@ nettrace_parse_address(char* curr_pos, char* next_pos, gboolean is_src_addr, exp
ws_in6_addr ip6_addr;
guint32 ip4_addr;
char *ptr; //for strtol function

static GRegex* aregex = NULL;
static GRegex* pregex = NULL;
static GRegex* tregex = NULL;

if (aregex == NULL)
aregex = g_regex_new ("^.*address\\s*=*\\s*\\[?((?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:[0-9a-f:]*))", G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0, NULL);

if (pregex == NULL)
pregex = g_regex_new ("^.*port\\s*=*\\s*(\\d*)", G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0, NULL);

if (tregex == NULL)
tregex = g_regex_new ("^.*transport\\s*=*\\s*(\\w*)", G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0, NULL);


/* curr_pos pointing to first char of address */

Expand All @@ -135,22 +150,28 @@ nettrace_parse_address(char* curr_pos, char* next_pos, gboolean is_src_addr, exp
* Address=198.142.204.199,Port=2123
*/

char **addressMatches = g_regex_split_simple("address\\s*=*\\s*\\[?((?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:[0-9a-f:]*))", curr_pos, G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0);
char **portMatches = g_regex_split_simple("port\\s*=*\\s*(\\d*)", curr_pos, G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0);
char **transportMatches = g_regex_split_simple("transport\\s*=*\\s*(\\w*)", curr_pos, G_REGEX_CASELESS | G_REGEX_FIRSTLINE, 0);
char **addressMatches = g_regex_split(aregex, curr_pos, 0);
char **portMatches = g_regex_split(pregex, curr_pos, 0);
char **transportMatches = g_regex_split(tregex, curr_pos, 0);


if (addressMatches[1] != NULL && addressMatches[2] != NULL) {
(void) g_strlcpy(ip_addr_str, addressMatches[1], strlen(addressMatches[1])+1);
}
if (portMatches[1] != NULL && portMatches[2] != NULL) {
port = (guint) strtol(portMatches[1], &ptr, 10);
//port = atoi(portMatches[1]);
}
if (transportMatches[1] != NULL && transportMatches[2] != NULL) {
(void) g_strlcpy(transp_str, transportMatches[1], strlen(transportMatches[1])+1);
}

g_free(addressMatches);
g_free(portMatches);
g_free(transportMatches);

g_regex_unref(aregex);
g_regex_unref(pregex);
g_regex_unref(tregex);

if (ws_inet_pton6(ip_addr_str, &ip6_addr)) {
if (is_src_addr) {
Expand Down

0 comments on commit 5c7d037

Please sign in to comment.