Skip to content

Commit

Permalink
check for first 3 characters, to avoid strncmp() cost
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Feb 25, 2019
1 parent dd3e38c commit 8a98de3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/modules/rlm_isc_dhcp/rlm_isc_dhcp.c
Expand Up @@ -46,7 +46,7 @@ extern fr_dict_attr_autoload_t rlm_isc_dhcp_dict_attr[];
fr_dict_attr_autoload_t rlm_isc_dhcp_dict_attr[] = {
{ .out = &attr_client_hardware_address, .name = "DHCP-Client-Hardware-Address", .type = FR_TYPE_ETHERNET, .dict = &dict_dhcpv4},
{ .out = &attr_your_ip_address, .name = "DHCP-Your-IP-Address", .type = FR_TYPE_IPV4_ADDR, .dict = &dict_dhcpv4},
{ .out = &attr_client_identifier, .name = "DHCP-Client-IDentifier", .type = FR_TYPE_OCTETS, .dict = &dict_dhcpv4},
{ .out = &attr_client_identifier, .name = "DHCP-Client-Identifier", .type = FR_TYPE_OCTETS, .dict = &dict_dhcpv4},
{ NULL }
};

Expand Down Expand Up @@ -1215,6 +1215,11 @@ static int match_keyword(rlm_isc_dhcp_info_t *parent, rlm_isc_dhcp_tokenizer_t *
end = num_tokens - 1;
half = -1;

/*
* There are no super-short commands.
*/
if (state->token_len < 4) goto unknown;

/*
* Walk over the input token, doing a binary search on
* the token list.
Expand All @@ -1224,11 +1229,18 @@ static int match_keyword(rlm_isc_dhcp_info_t *parent, rlm_isc_dhcp_tokenizer_t *

/*
* Skips a function call, and is better for 99%
* of the situations.
* of the situations. Since there are no 1 or 2
* character keywords, this always works.
*/
rcode = state->token[0] - tokens[half].name[0];
if (rcode != 0) goto recurse;

rcode = state->token[1] - tokens[half].name[1];
if (rcode != 0) goto recurse;

rcode = state->token[2] - tokens[half].name[2];
if (rcode != 0) goto recurse;

/*
* Compare all of the strings.
*/
Expand Down Expand Up @@ -1273,6 +1285,7 @@ static int match_keyword(rlm_isc_dhcp_info_t *parent, rlm_isc_dhcp_tokenizer_t *
* Nothing matched, it's a failure.
*/
if (!q) {
unknown:
fr_strerror_printf("unknown command '%.*s'", state->token_len, state->token);
return -1;
}
Expand Down

0 comments on commit 8a98de3

Please sign in to comment.