lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

#include "first.h"
#include "array.h"
#include "configfile.h"
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_PCRE_H
#include <pcre.h>
#ifndef PCRE_STUDY_JIT_COMPILE
#define PCRE_STUDY_JIT_COMPILE 0
#define pcre_free_study(x) pcre_free(x)
#endif
#endif
__attribute_cold__
static data_unset *data_config_copy(const data_unset *s) {
data_config *src = (data_config *)s;
data_config *ds = data_config_init();
ds->comp = src->comp;
[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
1 year ago
if (!buffer_is_unset(&src->key)) {
buffer_copy_buffer(&ds->key, &src->key);
ds->comp_key = ds->key.ptr + (src->comp_key - src->key.ptr);
}
buffer_copy_buffer(&ds->comp_tag, &src->comp_tag);
array_copy_array(ds->value, src->value);
return (data_unset *)ds;
}
__attribute_cold__
static void data_config_free(data_unset *d) {
data_config *ds = (data_config *)d;
free(ds->key.ptr);
free(ds->comp_tag.ptr);
array_free(ds->value);
vector_config_weak_clear(&ds->children);
free(ds->string.ptr);
#ifdef HAVE_PCRE_H
if (ds->regex) pcre_free(ds->regex);
if (ds->regex_study) pcre_free_study(ds->regex_study);
#endif
free(d);
}
data_config *data_config_init(void) {
static const struct data_methods fn = {
data_config_copy,
data_config_free,
NULL
};
data_config *ds;
ds = calloc(1, sizeof(*ds));
force_assert(ds);
ds->comp_key = "";
ds->value = array_init(4);
vector_config_weak_init(&ds->children);
ds->type = TYPE_CONFIG;
ds->fn = &fn;
return ds;
}
#include "log.h"
int data_config_pcre_compile(data_config * const dc, const int pcre_jit, log_error_st * const errh) {
#ifdef HAVE_PCRE_H
const char *errptr;
int erroff, captures;
dc->regex = pcre_compile(dc->string.ptr, 0, &errptr, &erroff, NULL);
if (NULL == dc->regex) {
log_error(errh, __FILE__, __LINE__,
"parsing regex failed: %s -> %s at offset %d\n",
dc->string.ptr, errptr, erroff);
return 0;
}
const int study_options = pcre_jit ? PCRE_STUDY_JIT_COMPILE : 0;
dc->regex_study = pcre_study(dc->regex, study_options, &errptr);
if (NULL == dc->regex_study && errptr != NULL) {
log_error(errh, __FILE__, __LINE__,
"studying regex failed: %s -> %s\n",
dc->string.ptr, errptr);
return 0;
}
erroff = pcre_fullinfo(dc->regex, dc->regex_study, PCRE_INFO_CAPTURECOUNT,
&captures);
if (0 != erroff) {
log_error(errh, __FILE__, __LINE__,
"getting capture count for regex failed: %s\n",
dc->string.ptr);
return 0;
}
else if (captures > 9) {
log_error(errh, __FILE__, __LINE__,
"Too many captures in regex, use (?:...) instead of (...): %s\n",
dc->string.ptr);
return 0;
}
return 1;
#else
UNUSED(pcre_jit);
log_error(errh, __FILE__, __LINE__,
"can't handle '%s' as you compiled without pcre support. \n"
"(perhaps just a missing pcre-devel package ?) \n",
dc->comp_key);
return 0;
#endif
}