You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by sf...@apache.org on 2011/11/21 23:24:12 UTC
svn commit: r1204730 - in /httpd/httpd/branches/2.4.x: include/ap_expr.h
include/ap_mmn.h server/util_expr_eval.c server/util_expr_parse.c
server/util_expr_parse.y
Author: sf
Date: Mon Nov 21 22:24:12 2011
New Revision: 1204730
URL: http://svn.apache.org/viewvc?rev=1204730&view=rev
Log:
Merge r1204087, 1204090:
Limit recursion in ap_expr evaluation to avoid unbounded stack usage
* evaluate chains of ||, &&, and string concatenation non-recursively
* limit other types of recursion to 20 levels
* avoid some string copies if concatenating more than 2 strings
Modified:
httpd/httpd/branches/2.4.x/include/ap_expr.h
httpd/httpd/branches/2.4.x/include/ap_mmn.h
httpd/httpd/branches/2.4.x/server/util_expr_eval.c
httpd/httpd/branches/2.4.x/server/util_expr_parse.c
httpd/httpd/branches/2.4.x/server/util_expr_parse.y
Modified: httpd/httpd/branches/2.4.x/include/ap_expr.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/include/ap_expr.h?rev=1204730&r1=1204729&r2=1204730&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/include/ap_expr.h (original)
+++ httpd/httpd/branches/2.4.x/include/ap_expr.h Mon Nov 21 22:24:12 2011
@@ -130,6 +130,8 @@ typedef struct {
const char **result_string;
/** Arbitrary context data provided by the caller for custom functions */
void *data;
+ /** The current recursion level */
+ int reclvl;
} ap_expr_eval_ctx_t;
/**
Modified: httpd/httpd/branches/2.4.x/include/ap_mmn.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/include/ap_mmn.h?rev=1204730&r1=1204729&r2=1204730&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/include/ap_mmn.h (original)
+++ httpd/httpd/branches/2.4.x/include/ap_mmn.h Mon Nov 21 22:24:12 2011
@@ -365,6 +365,7 @@
* 20111025.1 (2.3.15-dev) Add ap_escape_urlencoded(), ap_escape_urlencoded_buffer()
* and ap_unescape_urlencoded().
* 20111025.2 (2.3.15-dev) Add ap_lua_ssl_val to mod_lua
+ * 20111025.3 (2.4.0-dev) Add reclvl to ap_expr_eval_ctx_t
*/
#define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */
@@ -372,7 +373,7 @@
#ifndef MODULE_MAGIC_NUMBER_MAJOR
#define MODULE_MAGIC_NUMBER_MAJOR 20111025
#endif
-#define MODULE_MAGIC_NUMBER_MINOR 2 /* 0...n */
+#define MODULE_MAGIC_NUMBER_MINOR 3 /* 0...n */
/**
* Determine if the server's current MODULE_MAGIC_NUMBER is at least a
Modified: httpd/httpd/branches/2.4.x/server/util_expr_eval.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/server/util_expr_eval.c?rev=1204730&r1=1204729&r2=1204730&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/server/util_expr_eval.c (original)
+++ httpd/httpd/branches/2.4.x/server/util_expr_eval.c Mon Nov 21 22:24:12 2011
@@ -56,10 +56,30 @@ static void expr_dump_tree(const ap_expr
int loglevel, int indent);
#endif
+/*
+ * To reduce counting overhead, we only count calls to
+ * ap_expr_eval_word() and ap_expr_eval(). The max number of
+ * stack frames is larger by some factor.
+ */
+#define AP_EXPR_MAX_RECURSION 20
+static int inc_rec(ap_expr_eval_ctx_t *ctx)
+{
+ if (ctx->reclvl < AP_EXPR_MAX_RECURSION) {
+ ctx->reclvl++;
+ return 0;
+ }
+ *ctx->err = "Recursion limit reached";
+ /* short circuit further evaluation */
+ ctx->reclvl = INT_MAX;
+ return 1;
+}
+
static const char *ap_expr_eval_word(ap_expr_eval_ctx_t *ctx,
const ap_expr_t *node)
{
const char *result = "";
+ if (inc_rec(ctx))
+ return result;
switch (node->node_op) {
case op_Digit:
case op_String:
@@ -68,17 +88,41 @@ static const char *ap_expr_eval_word(ap_
case op_Var:
result = ap_expr_eval_var(ctx, node->node_arg1, node->node_arg2);
break;
- case op_Concat: {
- const char *s1 = ap_expr_eval_word(ctx, node->node_arg1);
- const char *s2 = ap_expr_eval_word(ctx, node->node_arg2);
- if (!*s1)
- result = s2;
- else if (!*s2)
- result = s1;
- else
- result = apr_pstrcat(ctx->p, s1, s2, NULL);
+ case op_Concat:
+ if (((ap_expr_t *)node->node_arg2)->node_op != op_Concat) {
+ const char *s1 = ap_expr_eval_word(ctx, node->node_arg1);
+ const char *s2 = ap_expr_eval_word(ctx, node->node_arg2);
+ if (!*s1)
+ result = s2;
+ else if (!*s2)
+ result = s1;
+ else
+ result = apr_pstrcat(ctx->p, s1, s2, NULL);
+ }
+ else {
+ const ap_expr_t *nodep = node;
+ int i = 1;
+ struct iovec *vec;
+ do {
+ nodep = nodep->node_arg2;
+ i++;
+ } while (nodep->node_op == op_Concat);
+ vec = apr_palloc(ctx->p, i * sizeof(struct iovec));
+ nodep = node;
+ i = 0;
+ do {
+ vec[i].iov_base = (void *)ap_expr_eval_word(ctx,
+ nodep->node_arg1);
+ vec[i].iov_len = strlen(vec[i].iov_base);
+ i++;
+ nodep = nodep->node_arg2;
+ } while (nodep->node_op == op_Concat);
+ vec[i].iov_base = (void *)ap_expr_eval_word(ctx, nodep);
+ vec[i].iov_len = strlen(vec[i].iov_base);
+ i++;
+ result = apr_pstrcatv(ctx->p, vec, i, NULL);
+ }
break;
- }
case op_StringFuncCall: {
const ap_expr_t *info = node->node_arg1;
const ap_expr_t *args = node->node_arg2;
@@ -96,6 +140,7 @@ static const char *ap_expr_eval_word(ap_
}
if (!result)
result = "";
+ ctx->reclvl--;
return result;
}
@@ -657,30 +702,79 @@ static int ap_expr_eval(ap_expr_eval_ctx
{
const ap_expr_t *e1 = node->node_arg1;
const ap_expr_t *e2 = node->node_arg2;
- switch (node->node_op) {
- case op_True:
- return 1;
- case op_False:
- return 0;
- case op_Not:
- return (!ap_expr_eval(ctx, e1));
- case op_Or:
- return (ap_expr_eval(ctx, e1) || ap_expr_eval(ctx, e2));
- case op_And:
- return (ap_expr_eval(ctx, e1) && ap_expr_eval(ctx, e2));
- case op_UnaryOpCall:
- return ap_expr_eval_unary_op(ctx, e1, e2);
- case op_BinaryOpCall:
- return ap_expr_eval_binary_op(ctx, e1, e2);
- case op_Comp:
- if (ctx->info->flags & AP_EXPR_FLAG_SSL_EXPR_COMPAT)
- return ssl_expr_eval_comp(ctx, e1);
- else
- return ap_expr_eval_comp(ctx, e1);
- default:
- *ctx->err = "Internal evaluation error: Unknown expression node";
- return FALSE;
+ int result = FALSE;
+ if (inc_rec(ctx))
+ return result;
+ while (1) {
+ switch (node->node_op) {
+ case op_True:
+ result ^= TRUE;
+ goto out;
+ case op_False:
+ result ^= FALSE;
+ goto out;
+ case op_Not:
+ result = !result;
+ node = e1;
+ break;
+ case op_Or:
+ do {
+ if (e1->node_op == op_Not) {
+ if (!ap_expr_eval(ctx, e1->node_arg1)) {
+ result ^= TRUE;
+ goto out;
+ }
+ }
+ else {
+ if (ap_expr_eval(ctx, e1)) {
+ result ^= TRUE;
+ goto out;
+ }
+ }
+ node = node->node_arg2;
+ e1 = node->node_arg1;
+ } while (node->node_op == op_Or);
+ break;
+ case op_And:
+ do {
+ if (e1->node_op == op_Not) {
+ if (ap_expr_eval(ctx, e1->node_arg1)) {
+ result ^= FALSE;
+ goto out;
+ }
+ }
+ else {
+ if (!ap_expr_eval(ctx, e1)) {
+ result ^= FALSE;
+ goto out;
+ }
+ }
+ node = node->node_arg2;
+ e1 = node->node_arg1;
+ } while (node->node_op == op_And);
+ break;
+ case op_UnaryOpCall:
+ result ^= ap_expr_eval_unary_op(ctx, e1, e2);
+ goto out;
+ case op_BinaryOpCall:
+ result ^= ap_expr_eval_binary_op(ctx, e1, e2);
+ goto out;
+ case op_Comp:
+ if (ctx->info->flags & AP_EXPR_FLAG_SSL_EXPR_COMPAT)
+ result ^= ssl_expr_eval_comp(ctx, e1);
+ else
+ result ^= ap_expr_eval_comp(ctx, e1);
+ goto out;
+ default:
+ *ctx->err = "Internal evaluation error: Unknown expression node";
+ goto out;
+ }
+ e1 = node->node_arg1;
+ e2 = node->node_arg2;
}
+out:
+ ctx->reclvl--;
+ return result;
}
AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *info,
@@ -704,6 +798,7 @@ AP_DECLARE(int) ap_expr_exec_ctx(ap_expr
AP_DEBUG_ASSERT(ctx->re_source != NULL);
AP_DEBUG_ASSERT(ctx->re_nmatch > 0);
}
+ ctx->reclvl = 0;
*ctx->err = NULL;
if (ctx->info->flags & AP_EXPR_FLAG_STRING_RESULT) {
Modified: httpd/httpd/branches/2.4.x/server/util_expr_parse.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/server/util_expr_parse.c?rev=1204730&r1=1204729&r2=1204730&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/server/util_expr_parse.c (original)
+++ httpd/httpd/branches/2.4.x/server/util_expr_parse.c Mon Nov 21 22:24:12 2011
@@ -591,9 +591,9 @@ static const yytype_int8 yypact[] =
25, -35, 79, -17, -35, -8, 60, 60, 43, 43,
43, 43, 43, 43, 43, 5, 5, 0, 43, 43,
43, 43, 43, 43, 43, -35, -27, -35, -35, 73,
- -35, 3, -35, 25, 25, 25, 25, 25, 25, 25,
+ -35, 86, 3, 25, 25, 25, 25, 25, 25, 25,
-35, -35, -35, -35, 23, 43, -35, -35, 25, 25,
- 25, 25, 25, 25, -35, -35, 106, 43, 85, 25,
+ 25, 25, 25, 25, 25, -35, 106, 43, 85, 25,
-35, -21, -35, 43, -35, 25
};
Modified: httpd/httpd/branches/2.4.x/server/util_expr_parse.y
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/server/util_expr_parse.y?rev=1204730&r1=1204729&r2=1204730&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/server/util_expr_parse.y (original)
+++ httpd/httpd/branches/2.4.x/server/util_expr_parse.y Mon Nov 21 22:24:12 2011
@@ -81,10 +81,10 @@
%token T_OP_AND
%token T_OP_NOT
-%left T_OP_OR
-%left T_OP_AND
-%left T_OP_NOT
-%left T_OP_CONCAT
+%right T_OP_OR
+%right T_OP_AND
+%right T_OP_NOT
+%right T_OP_CONCAT
%type <exVal> expr
%type <exVal> comparison
Re: svn commit: r1204730 - in /httpd/httpd/branches/2.4.x: include/ap_expr.h include/ap_mmn.h server/util_expr_eval.c server/util_expr_parse.c server/util_expr_parse.y
Posted by Stefan Fritsch <sf...@sfritsch.de>.
On Tuesday 22 November 2011, William A. Rowe Jr. wrote:
> > +/*
> > + * To reduce counting overhead, we only count calls to
> > + * ap_expr_eval_word() and ap_expr_eval(). The max number of
> > + * stack frames is larger by some factor.
> > + */
> > +#define AP_EXPR_MAX_RECURSION 20
> > +static int inc_rec(ap_expr_eval_ctx_t *ctx)
> > +{
> > + if (ctx->reclvl< AP_EXPR_MAX_RECURSION) {
> > + ctx->reclvl++;
> > + return 0;
> > + }
> > + *ctx->err = "Recursion limit reached";
> > + /* short circuit further evaluation */
> > + ctx->reclvl = INT_MAX;
>
> When did this project adopt Posix99? Not that I'm complaining
> but that isn't a K&R construct (and this is the first such breakage
> that I'm aware of.)
I assume you refer to the INT_MAX? That's C89 according to Google. And
yes, we want to support C89 (otherwise http_log.h could have been a
lot simpler). And no, we don't want to support anything older than
C89, IMNSHO.
Re: svn commit: r1204730 - in /httpd/httpd/branches/2.4.x: include/ap_expr.h
include/ap_mmn.h server/util_expr_eval.c server/util_expr_parse.c server/util_expr_parse.y
Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 11/21/2011 4:24 PM, sf@apache.org wrote:
> Author: sf
> Date: Mon Nov 21 22:24:12 2011
> New Revision: 1204730
>
> URL: http://svn.apache.org/viewvc?rev=1204730&view=rev
> Log:
> Merge r1204087, 1204090:
>
> Limit recursion in ap_expr evaluation to avoid unbounded stack usage
> * evaluate chains of ||,&&, and string concatenation non-recursively
> * limit other types of recursion to 20 levels
> * avoid some string copies if concatenating more than 2 strings
> URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/server/util_expr_eval.c?rev=1204730&r1=1204729&r2=1204730&view=diff
> ==============================================================================
> --- httpd/httpd/branches/2.4.x/server/util_expr_eval.c (original)
> +++ httpd/httpd/branches/2.4.x/server/util_expr_eval.c Mon Nov 21 22:24:12 2011
> @@ -56,10 +56,30 @@ static void expr_dump_tree(const ap_expr
> int loglevel, int indent);
> #endif
>
> +/*
> + * To reduce counting overhead, we only count calls to
> + * ap_expr_eval_word() and ap_expr_eval(). The max number of
> + * stack frames is larger by some factor.
> + */
> +#define AP_EXPR_MAX_RECURSION 20
> +static int inc_rec(ap_expr_eval_ctx_t *ctx)
> +{
> + if (ctx->reclvl< AP_EXPR_MAX_RECURSION) {
> + ctx->reclvl++;
> + return 0;
> + }
> + *ctx->err = "Recursion limit reached";
> + /* short circuit further evaluation */
> + ctx->reclvl = INT_MAX;
When did this project adopt Posix99? Not that I'm complaining
but that isn't a K&R construct (and this is the first such breakage
that I'm aware of.)