Skip to content

Commit

Permalink
Add check after call strtok_r
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsman1996 committed Mar 2, 2023
1 parent bcb107a commit df18c48
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/common/cidr.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ parse_cidr(tcpr_cidr_t ** cidrdata, char *cidrin, char *delim)

/* first iteration of input using strtok */
network = strtok_r(cidrin, delim, &token);
if (network == NULL)
return 0;

*cidrdata = cidr2cidr(network);
cidr_ptr = *cidrdata;
Expand Down Expand Up @@ -362,6 +364,8 @@ parse_endpoints(tcpr_cidrmap_t ** cidrmap1, tcpr_cidrmap_t ** cidrmap2, const ch
/* ipv4 mode */
memset(newmap, '\0', NEWMAP_LEN);
map = strtok_r(string, ":", &token);
if (map == NULL)
goto done;

strlcpy(newmap, "0.0.0.0/0:", NEWMAP_LEN);
strlcat(newmap, map, NEWMAP_LEN);
Expand Down
2 changes: 1 addition & 1 deletion src/common/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ parse_list(tcpr_list_t ** listdata, char *ourstr)
second = NULL;

/* regex test */
if (regexec(&preg, this, 0, NULL, 0) != 0) {
if (this == NULL || regexec(&preg, this, 0, NULL, 0) != 0) {
warnx("Unable to parse: %s", this);
regfree(&preg);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/common/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ macinstring(const char *macstring, const u_char *mac)
memset(&tempmac[0], 0, sizeof(tempmac));

tempstr = strtok_r(ourstring, ",", &tok);
if (strlen(tempstr)) {
if (tempstr != NULL && strlen(tempstr)) {
mac2hex(tempstr, tempmac, len);
if (memcmp(mac, tempmac, len) == 0) {
dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
Expand Down
2 changes: 2 additions & 0 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ read_hexstring(const char *l2string, u_char *hex, const int hexlen)

/* get the first byte */
l2byte = strtok_r(string, ",", &token);
if (l2byte == NULL)
err(-1, "Hex buffer must contain something");
sscanf(l2byte, "%x", &value);
if (value > 0xff)
errx(-1, "Invalid hex string byte: %s", l2byte);
Expand Down
2 changes: 1 addition & 1 deletion src/tcpedit/portmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ parse_portmap(tcpedit_portmap_t ** portmap, const char *ourstr)
/* first iteration of input */
substr = strtok_r(ourstrcpy, ",", &token);

if ((*portmap = ports2PORT(substr)) == NULL) {
if (substr == NULL || (*portmap = ports2PORT(substr)) == NULL) {
safe_free(ourstrcpy);
return 0;
}
Expand Down

0 comments on commit df18c48

Please sign in to comment.