diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2021-06-08 22:57:36 -0400 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2021-08-27 02:16:53 -0400 |
commit | af3df29ae8276f4380ed25262bcdf3d95446a9b1 (patch) | |
tree | 2606d798d156da68dbcfc3f68a3c8d03490dfa11 /src/mod_vhostdb_ldap.c | |
parent | 937d83b6cf8b732b2acae13919a8d944542acd9c (diff) | |
download | lighttpd-git-af3df29ae8276f4380ed25262bcdf3d95446a9b1.tar.gz |
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
Diffstat (limited to 'src/mod_vhostdb_ldap.c')
-rw-r--r-- | src/mod_vhostdb_ldap.c | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/src/mod_vhostdb_ldap.c b/src/mod_vhostdb_ldap.c index 3809b742..50e19ede 100644 --- a/src/mod_vhostdb_ldap.c +++ b/src/mod_vhostdb_ldap.c @@ -58,7 +58,7 @@ static void mod_vhostdb_dbconf_free (void *vdata) /*(copied from mod_authn_ldap.c)*/ static void mod_vhostdb_dbconf_add_scheme (server *srv, buffer *host) { - if (!buffer_string_is_empty(host)) { + if (!buffer_is_blank(host)) { /* reformat hostname(s) as LDAP URIs (scheme://host:port) */ static const char *schemes[] = { "ldap://", "ldaps://", "ldapi://", "cldap://" @@ -73,7 +73,7 @@ static void mod_vhostdb_dbconf_add_scheme (server *srv, buffer *host) e = b; while (*e!=' '&&*e!='\t'&&*e!='\r'&&*e!='\n'&&*e!=','&&*e!='\0') ++e; - if (!buffer_string_is_empty(tb)) + if (!buffer_is_blank(tb)) buffer_append_string_len(tb, CONST_STR_LEN(",")); for (j = 0; j < sizeof(schemes)/sizeof(char *); ++j) { if (buffer_eq_icase_ssn(b, schemes[j], strlen(schemes[j]))) { @@ -102,18 +102,18 @@ static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdat if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("filter"))) { filter = &ds->value; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("attr"))) { - if (!buffer_string_is_empty(&ds->value)) attr = ds->value.ptr; + if (!buffer_is_blank(&ds->value)) attr = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("host"))) { mod_vhostdb_dbconf_add_scheme(srv, &ds->value); host = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("base-dn"))) { - if (!buffer_string_is_empty(&ds->value)) basedn = ds->value.ptr; + if (!buffer_is_blank(&ds->value)) basedn = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("bind-dn"))) { - if (!buffer_string_is_empty(&ds->value)) binddn = ds->value.ptr; + if (!buffer_is_blank(&ds->value)) binddn = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("bind-pw"))) { bindpw = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("ca-file"))) { - if (!buffer_string_is_empty(&ds->value)) cafile = ds->value.ptr; + if (!buffer_is_blank(&ds->value)) cafile = ds->value.ptr; } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("starttls"))) { starttls = config_plugin_value_tobool((data_unset *)ds, 1); } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("timeout"))) { @@ -135,7 +135,8 @@ static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdat * - starttls */ - if (!buffer_string_is_empty(filter) && NULL != host && NULL != basedn) { + if (NULL != filter && !buffer_is_blank(filter) + && NULL != host && NULL != basedn) { vhostdb_config *dbconf; if (NULL == strchr(filter->ptr, '?')) { @@ -217,7 +218,7 @@ static void mod_authn_append_ldap_filter_escape(buffer * const filter, const buf * the UTF-8 encoding of the character to escape with a backslash character. */ const char * const b = raw->ptr; - const size_t rlen = buffer_string_length(raw); + const size_t rlen = buffer_clen(raw); for (size_t i = 0; i < rlen; ++i) { size_t len = i; char *f; @@ -417,7 +418,7 @@ static int mod_vhostdb_ldap_query(request_st * const r, void *p_d, buffer *docro buffer_append_string_len(filter, b, (size_t)(d - b)); mod_authn_append_ldap_filter_escape(filter, &r->uri.authority); } else { - d = template->ptr + buffer_string_length(template); + d = template->ptr + buffer_clen(template); buffer_append_string_len(filter, b, (size_t)(d - b)); break; } |